using System.Collections.Generic;
using System.Linq;
using GTANetworkAPI;
using ReallifeGamemode.Server.Entities;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Models;
using ReallifeGamemode.Server.Util;
/**
* @overview Life of German Reallife - Managers BankManager (BankManager.cs)
* @author VegaZ
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Managers
{
    public class DoorManager : Script
    {
        private static Dictionary<int, NetHandle> _doorColShapes = new Dictionary<int, NetHandle>();

        public static void LoadDoors()
        {
            using (var dbContext = new DatabaseContext())
            {
                foreach (Door door in dbContext.Doors)
                {
                    _doorColShapes[door.Id] = NAPI.ColShape.CreateSphereColShape(door.Position, 30f);
                }
            }
        }

        public static void ReloadDoors()
        {
            foreach (var doorPair in _doorColShapes)
            {
                doorPair.Value.Entity<ColShape>().Delete();
            }
            _doorColShapes.Clear();
            LoadDoors();
        }

        public static void ChangeDoorState(Client player)
        {
            var user = player.GetUser();

            using (var dbContext = new DatabaseContext())
            {
                IQueryable<Door> NearDoors = dbContext.Doors.Where(d => d.Position.DistanceTo(player.Position) <= d.Radius);
                foreach (Door d in NearDoors)
                {
                    if (!user.IsAdmin(AdminLevel.ADMIN) && (d.FactionId != user.FactionId || d.FactionId == null))
                    {
                        string lockState = "~r~Du hast kein Recht diese T\u00fcr " + (d.Locked == true ? "auf" : "ab") + "zuschlie\u00dfen!";
                        player.SendNotification(lockState, true);
                        continue;
                    }

                    d.Locked = !d.Locked;

                    string notStr = d.Name + " " + (d.Locked == false ? "~g~auf" : "~r~ab") + "geschlossen";

                    player.SendNotification(notStr, true);

                    NAPI.Pools.GetAllPlayers().ForEach(p => p.TriggerEvent("changeDoorState", d.Model, d.X, d.Y, d.Z, (d.Locked ? 1 : 0), 0.0f, 0.0f, 0.0f));
                }
                dbContext.SaveChanges();
            }
        }

        [ServerEvent(Event.PlayerEnterColshape)]
        public void DoorManagerPlayerEnterColShapeEvent(ColShape colShape, Client player)
        {
            if (_doorColShapes.ContainsValue(colShape.Handle))
            {
                int doorId = _doorColShapes.Where(d => d.Value.Value == colShape.Handle.Value).FirstOrDefault().Key;

                using (var dbContext = new DatabaseContext())
                {
                    Door door = dbContext.Doors.Where(d => d.Id == doorId).First();
                    player.TriggerEvent("changeDoorState", door.Model, door.X, door.Y, door.Z, (door.Locked ? 1 : 0), 0.0f, 0.0f, 0.0f);
                }
            }
        }
    }
}
