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

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

    protected override AdminLevel AdminLevel => AdminLevel.HEADADMIN;

    public override string HelpText => "[add / remove / price / type / reloadhouses]";

    public void Handle(IPlayer player, string option1, string option2 = null)
    {
      using var dbContext = GetDbContext();

      option1 = option1?.ToLower();

      var houseManager = Main.GetScript<HouseManager>();

      if (option1 == "add")
      {
        House nearHouse = houseManager.GetNearHouse(player.Position, dbContext);
        if (nearHouse != null)
        {
          player.SendMessage("In der Nähe ist schon ein Haus", ChatPrefix.Error);
          return;
        }

        House house = new House()
        {
          Price = 0,
          Type = "Haus",
          X = (float)player.Position.X,
          Y = (float)player.Position.Y,
          Z = (float)player.Position.Z
        };

        dbContext.Houses.Add(house);
        dbContext.SaveChanges();

        houseManager.LoadHouse(house);

        player.SendNotification("Das Haus wurde erstellt");

        return;
      }
      else if (option1 == "remove")
      {
        House nearHouse = houseManager.GetNearHouse(player.Position, dbContext);
        if (nearHouse == null)
        {
          player.SendMessage("In deiner Nähe befindet sich kein Haus", ChatPrefix.Error);
          return;
        }

        if (nearHouse.OwnerId != null)
        {
          dbContext.Users.Where(u => u.Id == nearHouse.OwnerId).First().HouseId = null;
        }

        foreach (HouseRental rental in dbContext.HouseRentals.Include(r => r.User).Where(r => r.HouseId == nearHouse.Id))
        {
          rental.User.NewPlayer.SendRawMessage("!{#81F7BE}* Dein Mietvertrag wurde administrativ aufgelöst!");
          dbContext.HouseRentals.Remove(rental);
        }

        dbContext.Houses.Remove(nearHouse);
        dbContext.SaveChanges();

        houseManager.RemoveHouse(nearHouse);

        player.SendNotification("Das Haus wurde gelöscht");
        return;
      }
      else if (option1 == "price")
      {
        if (!int.TryParse(option2, out int price))
        {
          player.SendMessage("/house price [Price]", ChatPrefix.Usage);
          return;
        }

        House nearHouse = houseManager.GetNearHouse(player.Position, dbContext);
        if (nearHouse == null)
        {
          player.SendMessage("In deiner Nähe befindet sich kein Haus", ChatPrefix.Error);
          return;
        }

        nearHouse.Price = price;

        houseManager.RemoveHouse(nearHouse);
        houseManager.LoadHouse(nearHouse);

        dbContext.SaveChanges();

        player.SendNotification("Der Hauspreis wurde gesetzt");
        return;
      }
      else if (option1 == "type")
      {
        if (option2 == null)
        {
          player.SendMessage("/house type [Type]", ChatPrefix.Usage);
          return;
        }

        House nearHouse = houseManager.GetNearHouse(player.Position, dbContext);
        if (nearHouse == null)
        {
          player.SendMessage("In deiner Nähe befindet sich kein Haus", ChatPrefix.Error);
          return;
        }

        nearHouse.Type = option2;

        houseManager.RemoveHouse(nearHouse);
        houseManager.LoadHouse(nearHouse);

        dbContext.SaveChanges();

        player.SendNotification("Der Haustyp wurde gesetzt");

        return;
      }
      else if (option1 == "reloadhouses")
      {
        houseManager.ReloadAllHouses();
        player.SendNotification("Alle Häuser wurden neu geladen");
        return;
      }

      player.SendMessage("/house [add / remove / price / type / reloadhouses]", ChatPrefix.Usage);
    }
  }
}
