using System;
using System.Collections.Generic;
using System.Text;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Util;
using ReallifeGamemode.Server.Services;
using System.Linq;
using GTANetworkAPI;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Managers;

namespace ReallifeGamemode.Server.DrivingSchool
{
  class PlaneSchool : Script
  {
    private static TextLabel informationLabel1;
    private static Marker marker1;
    private static ColShape _colShape1;
    public static Vector3 Position { get; }

    private readonly IReadOnlyCollection<Vector3> planeRoute = new List<Vector3>
      {
       new Vector3(-1114.39, -2333.09, 14.87),
       new Vector3(-1133.06, -2287.62, 14.89),
       new Vector3(-1185.63, -2303.45, 14.86),
       new Vector3(-1168.78, -2349.79, 14.88),
       new Vector3(-1118.74, -2340.85, 14.87),
       new Vector3(-1219.9, -2239.5, 14.87),
       new Vector3(-1355.84, -2245.06, 14.87),
       new Vector3(-1427.14, -2368.96, 14.87),
       new Vector3(-872.5, -1820.64, 172.57),
       new Vector3(786.55, 2309.85, 426.1),
       new Vector3(1261.85, 3950.07, 301.3),
       new Vector3(2133.83, 4803.55, 41.99),
       new Vector3(2134.13, 4782.7, 41.9),
       new Vector3(2098.9, 4792.94, 41.98),
       new Vector3(-869.77, -1424.05, 133),
  }.AsReadOnly();


    private readonly IReadOnlyCollection<Vector3> planeRouteEnd = new List<Vector3>
      {
       new Vector3(-1164.29, -2351.52, 14.88),
  }.AsReadOnly();


    public static void Setup()
    {

      informationLabel1 = NAPI.TextLabel.CreateTextLabel("Flugschule\n~y~Flugschein ~s~- $~g~5000", new Vector3(-1083.96, -2476.96, 14.07), 20.0f, 1.3f, 0, new Color(255, 255, 255));
      marker1 = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, new Vector3(-1083.96, -2476.96, 13.07), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));

      _colShape1 = NAPI.ColShape.CreateSphereColShape(new Vector3(-1083.96, -2476.96, 14.07), 3f);
      _colShape1.OnEntityEnterColShape += EntityEnterBusinessColShape;
      _colShape1.OnEntityExitColShape += EntityExitBusinessColShape;
      NAPI.Blip.CreateBlip(90, new Vector3(-1083.96, -2476.96, 14.07), 1.0f, 4, "Flugschule", shortRange: true);
    }
    private static void EntityEnterBusinessColShape(ColShape colShape, Player client)
    {
      if (client.IsInVehicle || !client.IsLoggedIn()) return;

      client.TriggerEvent("planeSchoolMenu");
    }

    private static void EntityExitBusinessColShape(ColShape colShape, Player client)
    {
      client.TriggerEvent("removeplaneSchoolMenu");
    }

    [RemoteEvent("startplaneSchool")]
    public void StartplaneSchool(Player client)
    {
      User user = client.GetUser();

      if (user.FlyingLicensePlane || client.HasData("ActiveSchool"))
        return;

      if (user.Handmoney < 5000)
      {
        client.SendNotification("~r~Du brauchst ~g~$5000~r~ auf der Hand um die Prüfung zu starten.", true);
        return;
      }
      using (var dbContext = new DatabaseContext())
      {
        User payer = client.GetUser(dbContext);
        payer.Handmoney -= 5000;
        client.TriggerEvent("SERVER:SET_HANDMONEY", payer.Handmoney);
        dbContext.SaveChanges();
      }

      client.SetData("ActiveSchool", 1);
      client.TriggerEvent("renderTextOnScreen", "Du hast die Flugscheinprüfung gestartet. Steige nun in eines der Flugzeuge ein.");
      client.TriggerEvent("waitPlayerEntersVehicle1");
    }

    [RemoteEvent("timerCheckVehicle1")]
    public void TimerCheckVehicle1(Player user)
    {
      Vehicle veh = user.Vehicle;

      if (veh.GetServerVehicle() is SchoolVehicle schoolVehicle)
      {
        if (schoolVehicle.SchoolId == 1)
        {
          user.TriggerEvent("renderTextOnScreen", "Fahren Sie langsam und sicher zur Roten Markierung.");
          user.TriggerEvent("stopTimer");
          CheckPointHandle.StartCheckPointRoute(user, planeRoute, 0, 6, 12, "planeSchoolEvent");
        }
      }
    }

    [RemoteEvent("planeSchoolEventEnd")]
    public void planeSchoolEventEnd(Player user, int checkpoint)
    {
      Vehicle veh = user.Vehicle;
      user.WarpOutOfVehicle();
      ServerVehicle sVeh = VehicleManager.GetServerVehicleFromVehicle(veh);
      VehicleStreaming.SetEngineState(veh, false);
      VehicleStreaming.SetLockStatus(veh, false);
      NAPI.Entity.SetEntityVelocity(veh.Handle, new Vector3(0, 0, 0));
      veh.Position = sVeh.Position;
      veh.Rotation = new Vector3(0, 0, sVeh.Heading);

      user.TriggerEvent("renderTextOnScreen", "Du hast deinen Flugschein bestanden!");
      using (var dbContext = new DatabaseContext())
      {
        User drivingUser = user.GetUser(dbContext);
        drivingUser.FlyingLicensePlane = true;
        dbContext.SaveChanges();
      }
      user.ResetData("ActiveSchool");
      user.Position = new Vector3(-1083.96, -2476.96, 13.07);
    }
    [RemoteEvent("planeSchoolEvent")]
    public void planeSchoolEvent(Player user, int checkpoint)
    {
      switch (checkpoint)
      {
        case 0:
          user.TriggerEvent("renderTextOnScreen", "Achten Sie darauf dass Sie mittig durch jeder Markierung fahren/fliegen.");
          break;

        case 2:
          user.TriggerEvent("renderTextOnScreen", "Falls Sie aus dem Flugzeug aussteigen sind Sie aus der Prüfung durchgefallen.");
          break;

        case 3:
          user.TriggerEvent("renderTextOnScreen", "Achten Sie darauf dass Sie nicht stark abbremsen in der Luft sonst geht der Motor aus.");
          break;

        case 4:
          user.TriggerEvent("renderTextOnScreen", "Falls Sie eine Landung durchführen fliegen Sie mit max. 150km/h.");
          break;

        case 5:
          user.TriggerEvent("renderTextOnScreen", "Der Pfeil auf dem gelben Punkt zeigt ihnen ob Sie über oder unter dem Punkt seid.");
          break;

        case 6:
          user.TriggerEvent("renderTextOnScreen", "Beschleunige nun auf mind. 80km/h um abzuheben.");
          break;

        case 13:
          user.TriggerEvent("renderTextOnScreen", "Beschleunige nun auf mind. 80km/h um abzuheben.");
          break;

        case 7:
          user.TriggerEvent("renderTextOnScreen", "Sobald Sie sicher in der Luft sind drücken Sie 'G' um ihr Fahrwerk einzufahren.");
          break;

        case 10:
          user.TriggerEvent("renderTextOnScreen", "Setze zum landeflug an. Drücke 'G' um ihr Fahrwerk auszufahren.");
          break;

        case 14:
          user.TriggerEvent("renderTextOnScreen", "Setze zum landeflug an. Drücke 'G' um ihr Fahrwerk auszufahren.");
          CheckPointHandle.StartCheckPointRoute(user, planeRouteEnd, 5000, 6, 12, "planeSchoolEventEnd");
          break;


      }
    }


  }
}
