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

namespace ReallifeGamemode.Server.Managers
{

  public class SVarManager : Script
  {
    public static List<ServerVariable> SVars = new List<ServerVariable>(); 

    public static void LoadSVars()
    {
      using (var dbContext = new DatabaseContext())
      {
        SVars = dbContext.ServerVariables.Select(row => row).ToList();
        foreach(var svar in SVars)
        {
          System.Console.WriteLine("[SVAR][" + svar.Id + "] " + svar.Variable + " = " + svar.Value);
        }
        System.Console.WriteLine("Geladene Server Variablen [" + SVars.Count() + "]");
      }
    }

    public static void ChangeSVarValue(Player player, int varId, int varValue)
    {
      using (var dbContext = new DatabaseContext())
      {
        var changeValue = dbContext.ServerVariables.FirstOrDefault(v => v.Id == varId);
        var oldValue = changeValue.Value;
        SVars.FirstOrDefault(v => v.Id == varId).Value = varValue;
        changeValue.Value = varValue;
        dbContext.SaveChanges();

        ChatService.SendMessage(player, "~r~Wert von Server Variable ~o~" + changeValue.Variable + "~r~ in ~g~ " + varValue + "~r~geändert. Alter Wert: ~y~" + oldValue);

      }
    }
  }    
}
