/**
* @overview Life of German Reallife - Managers Interaction (InteractionManager.cs)
* @author MichaPlays
* @copyright (c) 2008 - 2021 Life of German
*/
using GTANetworkAPI;
using Newtonsoft.Json;
using ReallifeGamemode.Database.Models;
using ReallifeGamemode.Server.Extensions;
using ReallifeGamemode.Server.Finance;
using ReallifeGamemode.Services;
using System;


namespace ReallifeGamemode.Server.Bank
{
  class bank : Script
  {
    private static TextLabel informationLabel;
    private static TextLabel factionInformationLabel;
    private static Marker marker;
    private static Marker factionMarker;
    private static ColShape _colShape;
    private static ColShape _factioncolShape;
    public static Vector3 Position { get; }


    public static void Setup()
    {
      informationLabel = NAPI.TextLabel.CreateTextLabel("Bank", new Vector3(246.48, 223.01, 106.28), 20.0f, 1.3f, 0, new Color(255, 255, 255));
      marker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, new Vector3(246.48, 223.01, 105.28), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));

      factionInformationLabel = NAPI.TextLabel.CreateTextLabel("Fraktionskasse", new Vector3(251.61, 221.32, 106.28), 20.0f, 1.3f, 0, new Color(255, 255, 255));
      factionMarker = NAPI.Marker.CreateMarker(MarkerType.VerticalCylinder, new Vector3(251.61, 221.32, 105.28), new Vector3(), new Vector3(), 1f, new Color(255, 255, 255));

      _colShape = NAPI.ColShape.CreateSphereColShape(new Vector3(246.48, 223.01, 105.28), 2f);
      _colShape.OnEntityEnterColShape += EntityEnterBankColShape;
      _colShape.OnEntityExitColShape += EntityExitBankColShape;

      _factioncolShape = NAPI.ColShape.CreateSphereColShape(new Vector3(251.61, 221.32, 105.28), 2f);
      _factioncolShape.OnEntityEnterColShape += EntityEnterFactionBankColShape;
      _factioncolShape.OnEntityExitColShape += EntityExitFactionBankColShape;

      NAPI.Blip.CreateBlip(207, new Vector3(231.35, 214.98, 106.27), 1.0f, 2, "Bank", shortRange: true);
    }

    private static void EntityEnterBankColShape(ColShape colShape, Player client)
    {
      if (client.IsInVehicle || !client.IsLoggedIn()) return;

      client.TriggerEvent("showBankMenu");
    }

    private static void EntityExitBankColShape(ColShape colShape, Player client)
    {
      client.TriggerEvent("removeBankmenu");
    }

    private static void EntityEnterFactionBankColShape(ColShape colShape, Player client)
    {      
      if (client.IsInVehicle || !client.IsLoggedIn() || client.GetUser().FactionId is null) return;

      if (client.GetUser().FactionLeader == true)
      {
        client.TriggerEvent("showFactionBankMenuLeader", client.GetUser().Faction.BankAccount.Balance.ToString());
      }
      client.TriggerEvent("showFactionBankMenu", client.GetUser().Faction.BankAccount.Balance.ToString());
    }

    private static void EntityExitFactionBankColShape(ColShape colShape, Player client)
    {
      client.TriggerEvent("removeFactionBankmenu");
    }

    [RemoteEvent("CLIENT:payIn")]
    public void bank_payIn(Player player, string stringAmount)
    {
      int amount = Int32.Parse(stringAmount);


      using (var dbContext = new DatabaseContext())
      {

        if (amount <= 0 || amount > player.GetUser(dbContext).Handmoney)
        {
          player.SendNotification($"~r~Dieser Betrag ist ungültig.");
          return;
        }
        else
        {
          player.SendNotification($"~w~Du hast $~g~{amount} ~w~eingezahlt.");
          player.GetUser(dbContext).Handmoney -= amount;
          player.GetUser(dbContext).BankAccount.Balance += amount;
          dbContext.SaveChanges();
        }
      }
    }
    [RemoteEvent("CLIENT:payOut")]
    public void bank_payOut(Player player, string stringAmount)
    {
      int amount = Int32.Parse(stringAmount);


      using (var dbContext = new DatabaseContext())
      {

        if (amount <= 0 || amount > player.GetUser(dbContext).BankAccount.Balance)
        {
          player.SendNotification($"~r~Dieser Betrag ist ungültig.");
          return;
        }
        else
        {
          player.SendNotification($"~w~Du hast $~g~{amount} ~w~abgehoben.");
          player.GetUser(dbContext).Handmoney += amount;
          player.GetUser(dbContext).BankAccount.Balance -= amount;
          dbContext.SaveChanges();
        }
      }
    }

    [RemoteEvent("CLIENT:payment")]
    public void bank_payment(Player player, string jsonNameOrId, string stringAmount)
    {
      string nameOrId = (string)JsonConvert.DeserializeObject(jsonNameOrId);
      int amount = Int32.Parse(stringAmount);
      Player target = PlayerService.GetPlayerByNameOrId(nameOrId);

      using (var dbContext = new DatabaseContext())
      {
        if (player.GetUser(dbContext) == target.GetUser(dbContext))
        {
          player.SendNotification($"~r~Du kannst dir selber kein Geld überweisen.");
          return;
        }
        else if (!target.IsLoggedIn())
        {
          player.SendNotification($"~r~Dieser Spieler ist nicht Online.");

        }
        else if (amount <= 0 || (int)(amount * 1.05) > player.GetUser(dbContext).BankAccount.Balance)
        {
          player.SendNotification($"~r~Dieser Betrag kann nicht überwiesen werden.");
          return;
        }
        else
        {
          player.SendNotification($"~w~Du hast {target.Name} $~g~{amount} ~w~Überwiesen.");
          player.GetUser(dbContext).BankAccount.Balance -= (int)(amount * 1.05);
          target.GetUser(dbContext).otheramount = amount;
          dbContext.SaveChanges();
        }
      }
    }
    [RemoteEvent("CLIENT:factionPayIn")]
    public void bank_factionPayIn(Player player, string stringAmount)
    {
      int amount = Int32.Parse(stringAmount);


      using (var dbContext = new DatabaseContext())
      {

        if (amount <= 0 || amount > player.GetUser(dbContext).BankAccount.Balance)
        {
          player.SendNotification($"~r~Dieser Betrag ist ungültig.");
          return;
        }
        else
        {
          player.SendNotification($"~w~Du hast $~g~{amount}~w~ in die Fraktionskasse eingezahlt.");
          player.GetUser(dbContext).BankAccount.Balance -= amount;
          player.GetUser(dbContext).Faction.BankAccount.Balance += amount;
          dbContext.SaveChanges();
        }
      }
    }

    [RemoteEvent("CLIENT:factionPayment")]
    public void bank_factionPayment(Player player, string jsonNameOrId, string stringAmount)
    {
      string nameOrId = (string)JsonConvert.DeserializeObject(jsonNameOrId);
      int amount = Int32.Parse(stringAmount);
      Player target = PlayerService.GetPlayerByNameOrId(nameOrId);

      using (var dbContext = new DatabaseContext())
      {
        if (!target.IsLoggedIn())
        {
          player.SendNotification($"~r~Dieser Spieler ist nicht Online.");

        }
        else if (amount <= 0 || (int)(amount * 1.05) > player.GetUser(dbContext).Faction.BankAccount.Balance)
        {
          player.SendNotification($"~r~Dieser Betrag kann nicht überwiesen werden.");
          return;
        }
        else
        {
          player.SendNotification($"~w~Du hast {target.Name}  $~g~{amount} ~w~Überwiesen.");
          player.GetUser(dbContext).Faction.BankAccount.Balance -= (int)(amount * 1.05);
          target.GetUser(dbContext).otheramount = amount;
          dbContext.SaveChanges();
        }
      }
    }
  }

}
