using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReallifeGamemode.Database.Entities;
using ReallifeGamemode.Server.Core.API;
using ReallifeGamemode.Server.Core.Managers;
using ReallifeGamemode.Server.Types;

namespace ReallifeGamemode.Server.Core.Commands.Admin
{
  class InteriorCommand : AdminCommand
  {
    public override string CommandName => "interior";

    public override string HelpText => "[Add / Remove / SetEnter / SetExit/ List] [Name / ID]";

    protected override AdminLevel AdminLevel => AdminLevel.HEADADMIN;

    public void Handle(IPlayer player, string option1, string option2 = null)
    {
      option1 = option1.ToString().ToLower();

      if (option1 != "add" && option1 != "remove" && option1 != "setenter" && option1 != "setexit" && option1 != "list")
      {
        player.SendMessage("/interior [Add / Remove / SetEnter / SetExit / List] [Name / ID]", ChatPrefix.Usage);
        return;
      }

      var interiorManager = Main.GetScript<InteriorManager>();

      switch (option1)
      {
        case "add":
          using (var dbContext = GetDbContext())
          {
            Interior interiorAdd = new Interior
            {
              Name = option2
            };
            dbContext.Interiors.Add(interiorAdd);
            dbContext.SaveChanges();
            player.SendMessage("Das Interior ~y~" + option2 + "~s~ wurde erstellt. ~m~ID: " + interiorAdd.Id, ChatPrefix.Admin);
          }
          break;
        case "remove":
          using (var dbContext = GetDbContext())
          {
            if (!int.TryParse(option2, out int intId))
            {
              player.SendMessage("Es muss eine Nummer angegeben werden", ChatPrefix.Error);
              return;
            }
            Interior interiorRemove = interiorManager.GetInteriorById(intId, dbContext);
            if (interiorRemove == null)
            {
              player.SendMessage("Dieses Interior existiert nicht", ChatPrefix.Error);
              return;
            }

            interiorManager.DeleteInterior(interiorRemove);

            dbContext.Interiors.Remove(interiorRemove);
            dbContext.SaveChanges();
          }
          player.SendMessage("Das Interior wurde gelöscht", ChatPrefix.Admin);
          break;
        case "setenter":
          using (var dbContext = GetDbContext())
          {
            if (!int.TryParse(option2, out int intIdEnter))
            {
              player.SendMessage("Es muss eine Nummer angegeben werden", ChatPrefix.Error);
              return;
            }
            Interior interior = interiorManager.GetInteriorById(intIdEnter, dbContext);
            if (interior == null)
            {
              player.SendMessage("Dieses Interior existiert nicht", ChatPrefix.Error);
              return;
            }
            interior.EnterPosition = player.Position;
            dbContext.SaveChanges();
            interiorManager.DeleteInterior(interior);
            interiorManager.LoadInterior(interior);
            player.SendMessage("Die Eingangs-Position vom Interior ~y~" + interior.Name + "~s~ wurde gesetzt", ChatPrefix.Admin);
            interiorManager.LoadInteriors();
          }
          break;
        case "setexit":
          using (var dbContext = GetDbContext())
          {
            if (!int.TryParse(option2, out int intIdExit))
            {
              player.SendMessage("Es muss eine Nummer angegeben werden", ChatPrefix.Error);
              return;
            }
            Interior interior = interiorManager.GetInteriorById(intIdExit, dbContext);
            if (interior == null)
            {
              player.SendMessage("Dieses Interior existiert nicht", ChatPrefix.Error);
              return;
            }
            interior.ExitPosition = player.Position;
            dbContext.SaveChanges();
            interiorManager.DeleteInterior(interior);
            interiorManager.LoadInterior(interior);
            player.SendMessage("Die Eingangs-Position vom Interior ~y~" + interior.Name + "~s~ wurde gesetzt", ChatPrefix.Admin);
            interiorManager.LoadInteriors();
          }
          break;

        case "list":
          var interiors = interiorManager.GetInteriors();
          player.SendMessage(string.Join(", ", interiors.Select(i => i.Item1 + " (" + i.Item2 + ")")), ChatPrefix.Admin);
          break;
      }
    }
  }
}
