using System;
using System.Collections.Generic;
using GTANetworkAPI;
using Newtonsoft.Json;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Services;
using ReallifeGamemode.Server.Managers;
using ReallifeGamemode.Server.Util;

namespace ReallifeGamemode.Server.Job
{
  internal class PilotJob : JobBase
  {
    public override int Id => 3;

    public override string Name => "Pilot";

    public override bool NeedVehicleToStart => false;

    private readonly IReadOnlyCollection<Vector3> Skill1Route1 = new List<Vector3>//Pilot_Skill_1_Route_1
    {
      new Vector3(1645.9910888671875, 3236.744873046875, 40.98052978515625),
      new Vector3(302.6805114746094, 2867.93896484375, 204.81182861328125),
      new Vector3(-1093.6722412109375, 2737.1376953125, 257.575439453125),
      new Vector3(-2286.014892578125, 3019.12841796875, 33.4510498046875),
      new Vector3(-2471.854248046875, 3271.08935546875, 33.47834777832031),
      new Vector3(-623.9515380859375, 5313.12158203125, 181.6047821044922),
      new Vector3(1896.899658203125, 4824.203125, 208.77737426757812),
      new Vector3(2281.790283203125, 3606.98681640625, 132.3915252685547),
      new Vector3(1610.4461669921875, 3225.36376953125, 41.05073928833008),
    }.AsReadOnly();

    private readonly IReadOnlyCollection<Vector3> Skill1Route2 = new List<Vector3>//Pilot_Skill_1_Route_2
    {
      new Vector3(1593.927490234375, 3193.30908203125, 41.170997619628906),
      new Vector3(961.5843505859375, 2278.5966796875, 251.17218017578125),
      new Vector3(924.3920288085938, 93.95357513427734, 282.3033142089844),
      new Vector3(963.6002197265625, -3101.551513671875, 177.8121337890625),
      new Vector3(-1265.1163330078125, -2990.142578125, 14.595105171203613),
      new Vector3(-1260.78662109375, -2806.293212890625, 14.587812423706055),
      new Vector3(-894.3707885742188, 82.7993392944336, 385.011962890625),
      new Vector3(919.4126586914062, 2553.385986328125, 135.39154052734375),
      new Vector3(1592.840087890625, 3192.8017578125, 41.169837951660156),
    }.AsReadOnly();

    private readonly IReadOnlyCollection<Vector3> Skill1Route3 = new List<Vector3>//Pilot_Skill_1_Route_3
    {
      new Vector3(-850.61, 2181.98, 429.67),
    }.AsReadOnly();

    private readonly IReadOnlyCollection<Vector3> Skill2Route1 = new List<Vector3>//Pilot_Skill_2_Route_1
    {
      new Vector3(-1534.98828125, -3028.916748046875, 14.451109886169434),
      new Vector3(-1182.960205078125, -3236.542724609375, 67.93492126464844),
      new Vector3(942.8041381835938, -2997.95751953125, 323.1705322265625),
      new Vector3(2459.114501953125, -525.8928833007812, 409.09002685546875),
      new Vector3(2764.611572265625, 1482.853271484375, 457.73944091796875),
      new Vector3(3485.380615234375, 3652.9619140625, 343.3283996582031),
      new Vector3(2652.611572265625, 4737.7958984375, 206.23399353027344),
      new Vector3(2043.8521728515625, 4769.595703125, 42.007118225097656),
      new Vector3(2134.332763671875, 4781.857421875, 41.900352478027344),
      new Vector3(1943.1544189453125, 4721.03125, 57.76427459716797),
      new Vector3(1749.4105224609375, 3790.8955078125, 194.40440368652344),
      new Vector3(1203.5526123046875, 2707.182373046875, 342.80145263671875),
      new Vector3(-377.686279296875, 1256.92578125, 532.7467651367188),
      new Vector3(-820.6907958984375, -412.96466064453125, 311.0726013183594),
      new Vector3(-1073.534912109375, -1704.798583984375, 94.05522155761719),
      new Vector3(-1686.30126953125, -2815.478759765625, 14.87234115600586),
      new Vector3(-1617.498291015625, -2982.41259765625, 14.883989334106445),
    }.AsReadOnly();

    private readonly IReadOnlyCollection<Vector3> Skill2Route2 = new List<Vector3>//Pilot_Skill_2_Route_2
    {
      new Vector3(-1463.965576171875, -2875.571533203125, 14.882678031921387),
      new Vector3(-981.0889282226562, -3122.533203125, 56.10537338256836),
      new Vector3(-341.6023254394531, -2016.779541015625, 262.1709899902344),
      new Vector3(-1446.6815185546875, -894.4887084960938, 321.3404846191406),
      new Vector3(-3151.909912109375, 979.1404418945312, 335.3139953613281),
      new Vector3(-1309.160888671875, 4903.9140625, 388.6031494140625),
      new Vector3(31.35175895690918, 3690.413330078125, 333.4454345703125),
      new Vector3(-948.346923828125, 2766.284912109375, 239.0842742919922),
      new Vector3(-2312.5068359375, 3033.903076171875, 33.7471923828125),
      new Vector3(-2132.787109375, 3265.840087890625, 33.74031448364258),
      new Vector3(-2331.76611328125, 3148.581298828125, 33.7597541809082),
      new Vector3(-2564.848876953125, 2369.27783203125, 182.68736267089844),
      new Vector3(-1833.041259765625, 832.1589965820312, 398.28863525390625),
      new Vector3(-878.19384765625, -904.84521484375, 284.2839050292969),
      new Vector3(-1124.624755859375, -1821.9208984375, 74.77794647216797),
      new Vector3(-1631.8602294921875, -2720.09814453125, 14.873514175415039),
      new Vector3(-1543.793701171875, -2829.399169921875, 14.899491310119629),
    }.AsReadOnly();

    private readonly IReadOnlyCollection<Vector3> Skill2Route3 = new List<Vector3>//Pilot_Skill_2_Route_3
    {
      new Vector3(-710.89, -1836.81, 355.46),
    }.AsReadOnly();

    public PilotJob()
    {
      JobStart += PilotJob_JobStart;
    }

    private void PilotJob_JobStart(Player player)
    {
      /*List<String> listRouteTexts = new List<string>();
      List<String> listRouteTexts2 = new List<string>();

      Vehicle veh = player.Vehicle;

      if ((VehicleHash)veh.Model == VehicleHash.Velum || (VehicleHash)veh.Model == VehicleHash.Velum2)
      {
        listRouteTexts2.Add("Route 4");
        listRouteTexts2.Add("Route 5");
        listRouteTexts2.Add("Route 6");

        player.TriggerEvent("showPilotRouteMenu", JsonConvert.SerializeObject(listRouteTexts2));
      }
      else
      {
        listRouteTexts.Add("Route 1");
        listRouteTexts.Add("Route 2");
        listRouteTexts.Add("Route 3");

        player.TriggerEvent("showPilotRouteMenu", JsonConvert.SerializeObject(listRouteTexts));
      }*/
      /*Random rnd = new Random();
      int rroute = rnd.Next(1, 6);
      switch (rroute)
      {
        case 1:
          {
            StartPilotRoute(player, "Route 1");
            break;
          }
        case 2:
          {
            StartPilotRoute(player, "Route 2");
            break;
          }
        case 3:
          {
            StartPilotRoute(player, "Route 3");
            break;
          }
        case 4:
          {
            StartPilotRoute(player, "Route 4");
            break;
          }
        case 5:
          {
            StartPilotRoute(player, "Route 5");
            break;
          }
      }*/
    }

    [RemoteEvent("startPilotRoute")]
    public void StartPilotRoute(Player player, string type)
    {
      Vehicle veh = player.Vehicle;
      if (player.VehicleSeat == 0)
      {
        if (type == "RouteStart")
        {
          if (player.HasData("PilotenBase") && player.GetData<int>("PilotenBase") == 1)
          {
            Random rnd = new Random();
            int rroute = rnd.Next(1, 3);
            switch (rroute)
            {
              case 1:
                {
                  CheckPointHandle.StartCheckPointRoute(player, Skill1Route1, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
                  player.SendChatMessage("~y~[JOB] ~w~Route wurde gesetzt.: ~g~Route 1");
                  player.SetData("Route", "Skill1Route1");
                  break;
                }
              case 2:
                {
                  CheckPointHandle.StartCheckPointRoute(player, Skill1Route2, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
                  player.SendChatMessage("~y~[JOB] ~w~Route wurde gesetzt.: ~g~Route 2");
                  player.SetData("Route", "Skill1Route2");
                  break;
                }
              /*case 3:
                {
                  CheckPointHandle.StartCheckPointRoute(player, Skill1Route3, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
                  player.SendChatMessage("~y~[JOB] ~w~Route wurde gesetzt.: ~g~Route 3");
                  player.SetData("Route", "Skill1Route3");
                  break;
                }*/
            }
          }
          if (player.HasData("PilotenBase") && player.GetData<int>("PilotenBase") == 2)
          {
            Random rnd = new Random();
            int rroute = rnd.Next(1, 3);
            switch (rroute)
            {
              case 1:
                {
                  CheckPointHandle.StartCheckPointRoute(player, Skill2Route1, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
                  player.SendChatMessage("~y~[JOB] ~w~Route wurde gesetzt.: ~g~Route 1");
                  player.SetData("Route", "Skill2Route1");
                  break;
                }
              case 2:
                {
                  CheckPointHandle.StartCheckPointRoute(player, Skill2Route2, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
                  player.SendChatMessage("~y~[JOB] ~w~Route wurde gesetzt.: ~g~Route 2");
                  player.SetData("Route", "Skill2Route2");
                  break;
                }
              /*case 3:
                {
                  CheckPointHandle.StartCheckPointRoute(player, Skill2Route3, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
                  player.SendChatMessage("~y~[JOB] ~w~Route wurde gesetzt.: ~g~Route 3");
                  player.SetData("Route", "Skill2Route3");
                  break;
                }*/
            }
          }
        }
        /*if (type == "Route 1")
        {
          CheckPointHandle.StartCheckPointRoute(player, Route1, 0, 6, 40, 10, true, "pilotJob"); //6, 23, 25, 26, 27, 28
        }
        else if (type == "Route 2")
        {
          CheckPointHandle.StartCheckPointRoute(player, Route2, 0, 6, 40, 10, true, "pilotJob");
        }
        else if (type == "Route 3")
        {
          CheckPointHandle.StartCheckPointRoute(player, Route3, 0, 6, 40, 10, true, "pilotJob");
        }
        else if (type == "Route 4")
        {
          CheckPointHandle.StartCheckPointRoute(player, Route4, 0, 6, 40, 10, true, "pilotJob");
        }
        else if (type == "Route 5")
        {
          CheckPointHandle.StartCheckPointRoute(player, Route5, 0, 6, 40, 10, true, "pilotJob");
        }
        else if (type == "Route 6")
        {
          CheckPointHandle.StartCheckPointRoute(player, Route6, 0, 6, 40, 10, true, "pilotJob");
        }*/
      }
    }
    public static void payWage(Player jobber, int wage)
    {
      using (var dbContext = new DatabaseContext())
      {
        if (jobber.VehicleSeat == 0)
        {
          jobber.GetUser(dbContext).Wage += wage;
          //jobber.SendNotification($"~y~[JOB] ~g~+{wage}$");
          jobber.GetUser(dbContext).PilotSkill++;
          dbContext.SaveChanges();
          if (jobber.GetUser(dbContext).PilotSkill == 300)
          {
            jobber.SendChatMessage($"~y~[JOB] ~w~Du hast nun ~g~Skill-Level 2 ~w~erreicht!");
            jobber.SendChatMessage($"~y~[JOB] ~w~Du kannst jetzt auch mit dem Velum Flugzeug (LS Airport) fliegen!");
          }
        }
        else
        {
          JobBase job = JobManager.GetJob(jobber.GetUser().JobId ?? -1);
          job.StopJob(jobber);
          CheckPointHandle.DeleteCheckpoints(jobber);
          ChatService.SendMessage(jobber, $"~y~[JOB]~s~ Du hast deinen Job ~o~{job.Name}~s~ beendet.");
        }
      }
    }
    [RemoteEvent("PilotLetzterCheckpoint")]
    public static void PilotLetzterCheckpoint(Player player)
    {
      PilotJob c = new PilotJob();
      Vehicle v = player.Vehicle;
      c.StartPilotRoute(player, "RouteStart");
      /*if (player.HasData("PilotenBase") && player.GetData<int>("PilotenBase") == 1)
      {
        Random rnd = new Random();
        int rroute = rnd.Next(1, 4);
        switch (rroute)
        {
          case 1:
            {
              c.StartPilotRoute(player, "Route 1");
              player.SendChatMessage("~y~[JOB] ~w~Eine neue Route wurde gesetzt.: ~g~Route 1");
              break;
            }
          case 2:
            {
              c.StartPilotRoute(player, "Route 2");
              player.SendChatMessage("~y~[JOB] ~w~Eine neue Route wurde gesetzt.: ~g~Route 2");
              break;
            }
          case 3:
            {
              c.StartPilotRoute(player, "Route 3");
              player.SendChatMessage("~y~[JOB] ~w~Eine neue Route wurde gesetzt.: ~g~Route 2");
              break;
            }
        }
      }
      if (player.HasData("PilotenBase") && player.GetData<int>("PilotenBase") == 2)
      {
        Random rnd = new Random();
        int rroute = rnd.Next(1, 4);
        switch (rroute)
        {
          case 1:
            {
              c.StartPilotRoute(player, "Route 4");
              player.SendChatMessage("~y~[JOB] ~w~Eine neue Route wurde gesetzt.: ~g~Route 4");
              break;
            }
          case 2:
            {
              c.StartPilotRoute(player, "Route 5");
              player.SendChatMessage("~y~[JOB] ~w~Eine neue Route wurde gesetzt.: ~g~Route 5");
              break;
            }
          case 3:
            {
              c.StartPilotRoute(player, "Route 6");
              player.SendChatMessage("~y~[JOB] ~w~Eine neue Route wurde gesetzt.: ~g~Route 6");
              break;
            }
        }
      }*/
    }
  }
}
