using GTANetworkAPI;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Models;
using System.Collections.Generic;
using System.Linq;

namespace ReallifeGamemode.Server.Managers
{
    class TuningManager : Script
    {
        private static List<ColShape> tuningGarages = new List<ColShape>();

        public static void LoadTuningGarages()
        {
            using (var dbContext = new DatabaseContext())
            {
                foreach (TuningGarage garage in dbContext.TuningGarages)
                {
                    AddTuningGarage(garage.Position);
                }
            }
        }
        /// <summary>
        /// Fügt eine Tuning-Garage zum Spiel hinzu
        /// </summary>
        /// <param name="pos">Die Position der Garage</param>
        public static void AddTuningGarage(Vector3 pos)
        {
            ColShape colShape = NAPI.ColShape.CreateSphereColShape(pos, 5, 0);

            colShape.OnEntityEnterColShape += (cs, c) =>
            {
                if (c.IsInVehicle)
                {
                    Vehicle v = c.Vehicle;
                    if (v.GetServerVehicle() is FactionVehicle fV && fV.GetFaction().StateOwned)
                    {
                        return;
                    }
                    c.TriggerEvent("showTuningInfo");
                }
            };

            colShape.OnEntityExitColShape += (cs, c) =>
            {
                c.TriggerEvent("hideTuningInfo", true);
            };

            tuningGarages.Add(colShape);
        }

        public static void ApplyTuningToServerVehicle(ServerVehicle sVeh)
        {
            Vehicle veh = VehicleManager.GetVehicleFromServerVehicle(sVeh);
            if (veh == null) return;


            veh.SetSharedData("mod18", false);

            using (var dbContext = new DatabaseContext())
            {
                foreach (VehicleMod vMod in dbContext.VehicleMods.ToList().FindAll(vM => vM.ServerVehicleId == sVeh.Id))
                {
                    if (vMod.Slot == 18)
                    {
                        veh.SetSharedData("mod" + vMod.Slot, true);
                    }
                    else if (vMod.Slot == 22)
                    {
                        int color = vMod.ModId - 2;
                        if (vMod.ModId == 0) color = -1;
                        if (vMod.ModId == 1) color = 13;
                        veh.SetSharedData("headlightColor", color);
                    }
                    else veh.SetMod(vMod.Slot, vMod.ModId - 1);
                }
            }
        }

        [RemoteEvent("startPlayerTuning")]
        public void StartPlayerTuning(Client player)
        {
            if (!player.IsInVehicle) return;

            player.TriggerEvent("showTuningMenu");
        }

        [RemoteEvent("repairVehicle")]
        public void RepairVehicle(Client player)
        {
            if (!player.IsInVehicle) return;
            player.Vehicle.Repair();
        }

        [RemoteEvent("setVehicleMod")]
        public void SetVehicleMod(Client player, int slot, int index)
        {
            Vehicle pV = player.Vehicle;
            if (index == 0) index--;

            if (slot != 18)
            {
                if (slot == 22)
                {
                    int color = index - 2;
                    if (index == 0) color = -1;
                    if (index == 1) color = 13;
                    pV.SetSharedData("headlightColor", color);
                }
                else
                {
                    pV.SetMod(slot, index - 1);
                }
            }
            else
            {
                bool newVal = index == -1 ? false : true;
                pV.SetSharedData("mod" + slot, newVal);
                NAPI.ClientEvent.TriggerClientEventForAll("vehicleToggleMod", pV, slot, newVal);
            }

            ServerVehicle sV = player.Vehicle.GetServerVehicle();
            if (sV == null) return;

            using (var dbContext = new DatabaseContext())
            {
                VehicleMod vMod = dbContext.VehicleMods.FirstOrDefault(m => m.ServerVehicleId == sV.Id && m.Slot == slot);
                if (vMod == null && index != -1)
                {
                    vMod = new VehicleMod
                    {
                        ServerVehicleId = sV.Id,
                        Slot = slot,
                        ModId = index
                    };
                    dbContext.VehicleMods.Add(vMod);
                }
                else if (vMod != null && index == -1)
                {
                    dbContext.VehicleMods.Remove(vMod);
                }
                else
                {
                    if (vMod == null)
                    {
                        vMod = new VehicleMod
                        {
                            ServerVehicleId = sV.Id,
                            Slot = slot
                        };
                        dbContext.VehicleMods.Add(vMod);
                    }
                    vMod.ModId = index;
                }
                dbContext.SaveChanges();
            }
        }
    }
}
