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

/**
* @overview Life of German Reallife - Managers ATMManager (ATMManager.cs)
* @author VegaZ
* @copyright (c) 2008 - 2018 Life of German
*/

namespace ReallifeGamemode.Server.Managers
{
    public class ATMManager : Script
    {
        public static List<ColShape> ATMColShapes = new List<ColShape>();

        public static void InitATMs()
        {
            var addedATMs = 0;
            using (var dbContext = new DatabaseContext())
            {
                foreach (var currentATM in dbContext.Blips)
                {
                    if (currentATM.Sprite == 500)
                    {
                        if (dbContext.ATMs.FirstOrDefault(a => a.Id == currentATM.Id) == null)
                        {
                            var dataSet = new ATM
                            {
                                Id = currentATM.Id,
                                X = currentATM.PositionX,
                                Y = currentATM.PositionY,
                                Z = currentATM.PositionZ
                            };
                            dbContext.Add(dataSet);
                            addedATMs++;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                if (addedATMs > 0)
                {
                    NAPI.Util.ConsoleOutput(addedATMs + " Geldautomaten hinzugefügt");
                }
                else
                {
                    NAPI.Util.ConsoleOutput("Keine Geldautomaten hinzugefügt");
                }

                dbContext.SaveChanges();
                LoadATMs();
            }
        }
        public static void LoadATMs()
        {
            using (var dbContext = new DatabaseContext())
            {
                foreach (var currentATM in dbContext.ATMs)
                {
                    var currentColShape = NAPI.ColShape.CreateCylinderColShape(new Vector3(currentATM.X, currentATM.Y, currentATM.Z), 2.5f, 3, 0);
                    currentColShape.OnEntityEnterColShape += EnterATMRange;
                    currentColShape.OnEntityExitColShape += ExitATMRange;
                    ATMColShapes.Add(currentColShape);
                    currentColShape.SetData("id", currentATM.Id);
                }
            }
        }
        public static void EnterATMRange(ColShape colShape, Client client)
        {
            client.SetData("nearATM", colShape.GetData("id"));
        }
        public static void ExitATMRange(ColShape colShape, Client client)
        {
            client.ResetData("nearATM");
        }
        public static void ShowAtmUi(Client player, int atmId)
        {
            player.TriggerEvent("SERVER:ShowAtmUi", atmId);
        }

        [RemoteEvent("CLIENT:ATM_MANAGER:ATM_ACTION")]
        public void AtmAction(Client client, int site, int inputField1, int inputField2)
        {
            var user = client.GetUser();
            using (var dbContext = new DatabaseContext())
            {
                int nearATM = client.GetData("nearATM");
                //SITE //0 Geld einzahlen //1 Geld auszahlen //2 Geld überweisen
                switch (site)
                {
                    //GELD EINZAHLEN in1
                    case 0:
                        var checkPlayer = dbContext.Users.FirstOrDefault(u => u.Id == user.Id);
                        
                        if (checkPlayer.Handmoney < inputField1)
                        {
                            //TODO im CEFBrowser anzeigen
                            //client.TriggerEvent("SERVER:WORLD_INTERACTION:ATM_ERROR", 0, checkATM.Balance);
                            client.SendNotification("~r~Nicht genügend Geld auf der Hand!");
                        }
                        else
                        {
                            var updateHandMoneyIn = dbContext.Users.FirstOrDefault(u => u.Id == user.Id);
                            var updateBankMoneyIn = dbContext.UserBankAccounts.FirstOrDefault(b => b.UserId == user.Id);
                            var updateATMBalanceIn = dbContext.ATMs.FirstOrDefault(a => a.Id == nearATM);
                            updateHandMoneyIn.Handmoney -= inputField1;
                            updateBankMoneyIn.Balance += inputField1;
                            updateATMBalanceIn.Balance += inputField1;
                            client.TriggerEvent("SERVER:SET_HANDMONEY", updateHandMoneyIn.Handmoney);
                        }                       
                        break;

                    //GELD AUSZAHLEN in1
                    case 1:
                        var checkATM = dbContext.ATMs.FirstOrDefault(a => a.Id == nearATM);
                        var checkBankAccount = dbContext.UserBankAccounts.FirstOrDefault(b => b.UserId == user.Id);

                        if (checkBankAccount.Balance < inputField1)
                        {
                            client.SendNotification("~r~Nicht genügend Geld auf dem Bankkonto!"); //TODO Im Automaten anzeigen lassen
                        }
                        else if (checkATM.Balance < inputField1)
                        {
                            client.SendNotification("~r~Nicht genügend Geld im Automaten vorhanden!"); //TODO Im Automaten anzeigen lassen |||| oder OUT OF ORDER anzeigen wenn leer
                        }
                        else
                        {
                            var updateHandMoneyOut = dbContext.Users.FirstOrDefault(u => u.Id == user.Id);
                            var updateBankMoneyOut = dbContext.UserBankAccounts.FirstOrDefault(b => b.UserId == user.Id);
                            var updateATMBalanceOut = dbContext.ATMs.FirstOrDefault(a => a.Id == nearATM);
                            updateHandMoneyOut.Handmoney += inputField1;
                            updateBankMoneyOut.Balance -= inputField1;
                            updateATMBalanceOut.Balance -= inputField1;
                            client.TriggerEvent("SERVER:SET_HANDMONEY", updateHandMoneyOut.Handmoney);
                        }
                        break;

                    //GELD ÜBERWEISEN in1 = Kontonr // in2 = Betrag
                    case 2:

                        break;
                }
                dbContext.SaveChanges();
            }
        }
    }
}
