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

namespace ReallifeGamemode.Server.Managers
{
    public class InteriorManager : Script
    {
        public static Dictionary<int, NetHandle> _interiorEnterTextLabels = new Dictionary<int, NetHandle>();
        public static Dictionary<int, NetHandle> _interiorExitTextLabels = new Dictionary<int, NetHandle>();
        public static Dictionary<int, NetHandle> _interiorEnterMarkers = new Dictionary<int, NetHandle>();
        public static Dictionary<int, NetHandle> _interiorExitMarkers = new Dictionary<int, NetHandle>();
        public static Dictionary<int, NetHandle> _interiorEnterColShapes = new Dictionary<int, NetHandle>();
        public static Dictionary<int, NetHandle> _interiorExitColShapes = new Dictionary<int, NetHandle>();

        public static Interior GetInteriorByName(string name, DatabaseContext dbContext = null)
        {
            if (dbContext == null)
            {
                using (dbContext = new DatabaseContext())
                {
                    return dbContext.Interiors.Where(i => i.Name.ToLower() == name.ToLower()).FirstOrDefault();
                }
            }
            else
            {
                return dbContext.Interiors.Where(i => i.Name.ToLower() == name.ToLower()).FirstOrDefault();
            }
        }

        public static Interior GetInteriorById(int id, DatabaseContext dbContext = null)
        {
            if (dbContext == null)
            {
                using (dbContext = new DatabaseContext())
                {
                    return dbContext.Interiors.Where(i => i.Id == id).FirstOrDefault();
                }
            }
            else
            {
                return dbContext.Interiors.Where(i => i.Id == id).FirstOrDefault();
            }
        }

        public static void LoadInteriors()
        {
            using (var dbContext = new DatabaseContext())
            {
                foreach (Interior interior in dbContext.Interiors)
                {
                    LoadInterior(interior);
                }
            }
        }

        public static void LoadInterior(Interior interior)
        {
            if (interior.EnterPosition != null)
            {
                _interiorEnterTextLabels[interior.Id] = NAPI.TextLabel.CreateTextLabel("~y~" + interior.Name + "\n~s~Eingang", interior.EnterPosition, 10f, 1f, 0, new Color(255, 255, 255));
                _interiorEnterMarkers[interior.Id] = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, interior.EnterPosition.Subtract(new Vector3(0, 0, 1.7)), new Vector3(), new Vector3(), 1.6f, new Color(255, 255, 255, 100));
                _interiorEnterColShapes[interior.Id] = NAPI.ColShape.CreateSphereColShape(interior.EnterPosition, 1.5f);
            }

            if (interior.ExitPosition != null)
            {
                _interiorExitTextLabels[interior.Id] = NAPI.TextLabel.CreateTextLabel("~y~" + interior.Name + "\n~s~Ausgang", interior.ExitPosition, 10f, 1f, 0, new Color(255, 255, 255));
                _interiorExitMarkers[interior.Id] = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, interior.ExitPosition.Subtract(new Vector3(0, 0, 1.7)), new Vector3(), new Vector3(), 1.6f, new Color(255, 255, 255, 100));
                _interiorExitColShapes[interior.Id] = NAPI.ColShape.CreateSphereColShape(interior.ExitPosition, 1.5f);
            }
        }

        public static void DeleteInterior(Interior interior)
        {
            TextLabel enT = GetInteriorEnterTextLabel(interior);
            TextLabel exT = GetInteriorExitTextLabel(interior);
            Marker enM = GetInteriorEnterMarker(interior);
            Marker exM = GetInteriorExitMarkers(interior);
            ColShape enC = GetInteriorEnterColShape(interior);
            ColShape exC = GetInteriorExitColShape(interior);

            if (enT != null) enT.Delete();
            if (exT != null) exT.Delete();
            if (enM != null) enM.Delete();
            if (exM != null) exM.Delete();
            if (enC != null) enC.Delete();
            if (exC != null) exC.Delete();

            _interiorEnterTextLabels.Remove(interior.Id);
            _interiorExitTextLabels.Remove(interior.Id);
            _interiorEnterMarkers.Remove(interior.Id);
            _interiorExitMarkers.Remove(interior.Id);
            _interiorEnterColShapes.Remove(interior.Id);
            _interiorExitColShapes.Remove(interior.Id);
        }

        public static TextLabel GetInteriorEnterTextLabel(Interior interior) => NAPI.Pools.GetAllTextLabels().Find(t => t.Handle.Value == _interiorEnterTextLabels.FirstOrDefault(x => x.Key == interior.Id).Value.Value);
        public static TextLabel GetInteriorExitTextLabel(Interior interior) => NAPI.Pools.GetAllTextLabels().Find(t => t.Handle.Value == _interiorExitTextLabels.FirstOrDefault(x => x.Key == interior.Id).Value.Value);

        public static Marker GetInteriorEnterMarker(Interior interior) => NAPI.Pools.GetAllMarkers().Find(t => t.Handle.Value == _interiorEnterMarkers.FirstOrDefault(x => x.Key == interior.Id).Value.Value);
        public static Marker GetInteriorExitMarkers(Interior interior) => NAPI.Pools.GetAllMarkers().Find(t => t.Handle.Value == _interiorExitMarkers.FirstOrDefault(x => x.Key == interior.Id).Value.Value);

        public static ColShape GetInteriorEnterColShape(Interior interior) => NAPI.Pools.GetAllColShapes().Find(t => t.Handle.Value == _interiorEnterColShapes.FirstOrDefault(x => x.Key == interior.Id).Value.Value);
        public static ColShape GetInteriorExitColShape(Interior interior) => NAPI.Pools.GetAllColShapes().Find(t => t.Handle.Value == _interiorExitColShapes.FirstOrDefault(x => x.Key == interior.Id).Value.Value);

        public static int GetInteriorIdFromEnterColShape(NetHandle handle) => _interiorEnterColShapes.FirstOrDefault(c => c.Value.Value == handle.Value).Key;
        public static int GetInteriorIdFromExitColShape(NetHandle handle) => _interiorExitColShapes.FirstOrDefault(c => c.Value.Value == handle.Value).Key;

        [ServerEvent(Event.PlayerEnterColshape)]
        public void InteriorManagerPlayerEnterColshapeEvent(ColShape colShape, Client player)
        {
            int enterId = GetInteriorIdFromEnterColShape(colShape);
            int exitId = GetInteriorIdFromExitColShape(colShape);
            if (enterId != 0)
            {
                Interior interior = GetInteriorById(enterId);
                player.TriggerEvent("InteriorManager_ShowHelpText", interior.Name, interior.Id, 0);
            }
            else if (exitId != 0)
            {
                Interior interior = GetInteriorById(exitId);
                player.TriggerEvent("InteriorManager_ShowHelpText", interior.Name, interior.Id, 1);
            }
        }

        [ServerEvent(Event.PlayerExitColshape)]
        public void InteriorManagerPlayerExitColshapeEvent(ColShape colShape, Client player)
        {
            if (GetInteriorIdFromEnterColShape(colShape) != 0 || GetInteriorIdFromExitColShape(colShape) != 0)
            {
                player.TriggerEvent("InteriorManager_ClearHelpText");
            }
        }

        [RemoteEvent("InteriorManager_UseTeleport")]
        public void InteriorManagerUseTeleportEvent(Client player, int id, int enterExit)
        {
            Interior interior = GetInteriorById(id);
            player.Position = enterExit == 0 ? interior.ExitPosition : interior.EnterPosition;
        }
    }
}
