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

namespace ReallifeGamemode.Server.Wanted
{
    public class Jail
    {
        public static Dictionary<int, int> jailtime { get; set; } = new Dictionary<int, int>(); //time in seconds


        public static void Check_PutBehindBars(Client client)
        {
            User user = client.GetUser();
            if (user.JailTime > 0)
            {
                Random rnd = new Random();
                int rndInt = rnd.Next(1, 3);
                if (rndInt == 1)
                    client.Position = new Vector3(458.9842, -997.2126, 24.91485); //send client to jail
                if (rndInt == 2)
                    client.Position = new Vector3(459.696, -994.3766, 24.91486); //send client to jail
                if (rndInt == 3)
                    client.Position = new Vector3(458.3372, -1001.258, 24.91485); //send client to jail

                jailtime[user.Id] = user.JailTime; // 54 sec for each wanted star -> in total 45min for 50 Wanteds
                return;
            }
            if (user.Wanteds <= 0)
                return;
            foreach (var copClient in NAPI.Pools.GetAllPlayers())
            {
                User cop = copClient.GetUser();
                if (cop?.FactionId == 1 || cop?.FactionId == 3)
                {
                    if (cop.GetData<bool>("duty") && copClient.Position.DistanceTo2D(copClient.Position) <= 200)
                    {
                        Random rnd = new Random();
                        int rndInt = rnd.Next(1, 3);
                        if (rndInt == 1)
                            client.Position = new Vector3(458.9842, -997.2126, 24.91485); //send client to jail
                        if (rndInt == 2)
                            client.Position = new Vector3(459.696, -994.3766, 24.91486); //send client to jail
                        if (rndInt == 3)
                            client.Position = new Vector3(458.3372, -1001.258, 24.91485); //send client to jail


                        using (var dbContext = new DatabaseContext())
                        {
                            client.GetUser(dbContext).JailTime = user.Wanteds * 54; // 54 sec for each wanted star -> in total 45min for 50 Wanteds
                            client.GetUser(dbContext).Wanteds = 0;
                            dbContext.SaveChanges();
                        }

                        jailtime[user.Id] = user.Wanteds * 54; // 54 sec for each wanted star -> in total 45min for 50 Wanteds
                    }
                }
            }

        }

        public static void JailTimer()
        {
            System.Timers.Timer timer = new System.Timers.Timer(60000);
            System.Timers.Timer brakeOut = new System.Timers.Timer(10000);
            System.Timers.Timer jailIn = new System.Timers.Timer(2500);
            timer.Start();
            brakeOut.Start();
            jailIn.Start();
            timer.Elapsed += Timer_Elapsed;
            brakeOut.Elapsed += brakeOut_Elapsed;
            jailIn.Elapsed += jailIn_Elapsed;
        }

        private static void jailIn_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var player in NAPI.Pools.GetAllPlayers())
            {
                User user = player.GetUser();
                if (jailtime.ContainsKey(user.Id))
                    return;
                foreach(var copPlayer in NAPI.Pools.GetAllPlayers())
                {
                    User cop = copPlayer.GetUser();
                    if (player.Position.DistanceTo2D(new Vector3(458.9842, -997.2126, 24.91485)) <= 7)
                    {
                        if (player.Position.DistanceTo2D(copPlayer.Position) < 5)
                        {
                            Check_PutBehindBars(player);
                        }
                    }                  
                }
            }
        }


            private static void brakeOut_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var player in NAPI.Pools.GetAllPlayers())
            {
                User user = player.GetUser();
                if (!jailtime.ContainsKey(user.Id))
                    return;
                if (player.Position.DistanceTo2D(new Vector3(458.9842, -997.2126, 24.91485)) > 7)
                {
                    using (var dbContext = new DatabaseContext())
                    {
                        player.GetUser(dbContext).Wanteds = 50;
                        player.GetUser(dbContext).JailTime = 0;
                        dbContext.SaveChanges();
                        jailtime.Remove(user.Id);
                    }

                }
            }
        }

        private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            foreach (var player in NAPI.Pools.GetAllPlayers())
            {
                User user = player.GetUser();             
                if (jailtime.ContainsKey(user.Id))
                {
                    
                    if (user.JailTime <= 0)
                    {
                        Random rnd = new Random();
                        int rndInt = rnd.Next(1, 5);
                        switch (rndInt)
                        {
                            case 1:
                                player.Position = new Vector3(462.0074, -991.5361, 24.91487);
                                break;
                            case 2:
                                player.Position = new Vector3(462.2939, -989.9335, 24.91487);
                                break;
                            case 3:
                                player.Position = new Vector3(461.7256, -988.6035, 24.91487);
                                break;
                            case 4:
                                player.Position = new Vector3(462.5977, -989.9182, 24.91487);
                                break;
                            case 5:
                                player.Position = new Vector3(462.4364, -991.4973, 24.91487);
                                break;
                        }
                        jailtime.Remove(user.Id);
                        return;
                    }
                    if (user.JailTime > 0)
                    {
                        using (var dbContext = new DatabaseContext())
                        {
                            player.GetUser(dbContext).JailTime -= 60;
                            dbContext.SaveChanges();
                        }
                    }
                }
                
            }
        }

    }
}
