using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public sealed class EquipmentUIFoldPanel : MonoBehaviour
{
    [SerializeField]
    private Text stantionName = null;
    [SerializeField]
    private Button backButton = null;

    [SerializeField]
    private Profile profile = null;
    [SerializeField]
    private CreditsUICounter creditsCounter = null;
    [SerializeField]
    private SpaceUICounter spaceCounter = null;

    [SerializeField]
    private EquipmentButton shipButton = null;

    [SerializeField]
    private EquipmentButton driveButton = null;
    [SerializeField]
    private EquipmentButton tankButton = null;
    [SerializeField]
    private EquipmentButton cargoModuleButton = null;
    [SerializeField]
    private EquipmentButton shieldButton = null;
    [SerializeField]
    private EquipmentButton weaponButton = null;
    [SerializeField]
    private EquipmentListPanel buyList = null;
    public Player Player => profile.Player;
    public StarShip StarShip => profile.Player.StarShip;

    public void Show(string name)
    {
        gameObject.SetActive(true);
        UpdateInfo();
        buyList.Show(null);
        stantionName.text = name ?? string.Empty;
    }

    public void Hide() => gameObject.SetActive(false);

    private void OnShipClick() => buyList.Show(Player.EconomyParams.Blueprints.Ships, StarShip.Equipment.Ship.Id, TryBuyShip);
    private void OnDriveClick() => buyList.Show(Player.EconomyParams.Blueprints.Drives, StarShip.Equipment.Drive.Id, TryBuyDrive);
    private void OnTankClick() => buyList.Show(Player.EconomyParams.Blueprints.Tanks, StarShip.Equipment.Tank.Id, TryBuyTank);
    private void OnCargoModuleClick() => buyList.Show(Player.EconomyParams.Blueprints.CargoModules, StarShip.Equipment.CargoModule.Id, TryBuyCargoModule);
    private void OnShieldClick() => buyList.Show(Player.EconomyParams.Blueprints.Shields, StarShip.Equipment.Shield.Id, TryBuyShield);
    private void OnWeaponClick() => buyList.Show(Player.EconomyParams.Blueprints.Weapons, StarShip.Equipment.Weapon.Id, TryBuyWeapon);

    private void TryBuyShip(int shipId)
    {
        var result = Player.TryBuyEquip(shipId, Player.EconomyParams.Blueprints.Ships[shipId], ShipEquipment.Type.Ship);
        if (result)
        {
            UpdateInfo();
            OnShipClick();
        }
        else
        {
            creditsCounter.ShowNoCreditsEffect();
        }
    }
    private void TryBuyDrive(int driveId)
    {
        var result = Player.TryBuyEquip(driveId, Player.EconomyParams.Blueprints.Drives[driveId], ShipEquipment.Type.Drive);
        if (result)
        {
            UpdateInfo();
            OnDriveClick();
        }
        else
        {
            creditsCounter.ShowNoCreditsEffect();
        }
    }
    private void TryBuyTank(int tankId)
    {
        var result = Player.TryBuyEquip(tankId, Player.EconomyParams.Blueprints.Tanks[tankId], ShipEquipment.Type.Tank);
        if (result)
        {
            UpdateInfo();
            OnTankClick();
        }
        else
        {
            creditsCounter.ShowNoCreditsEffect();
        }
    }
    private void TryBuyCargoModule(int cargoModuleId)
    {
        var result = Player.TryBuyEquip(cargoModuleId, Player.EconomyParams.Blueprints.CargoModules[cargoModuleId], ShipEquipment.Type.CargoModule);
        if (result)
        {
            UpdateInfo();
            OnCargoModuleClick();
        }
        else
        {
            creditsCounter.ShowNoCreditsEffect();
        }
    }
    private void TryBuyShield(int shieldId)
    {
        var result = Player.TryBuyEquip(shieldId, Player.EconomyParams.Blueprints.Shields[shieldId], ShipEquipment.Type.Shield);
        if (result)
        {
            UpdateInfo();
            OnShieldClick();
        }
        else
        {
            creditsCounter.ShowNoCreditsEffect();
        }
    }
    private void TryBuyWeapon(int weaponId)
    {
        var result = Player.TryBuyEquip(weaponId, Player.EconomyParams.Blueprints.Weapons[weaponId], ShipEquipment.Type.Weapon);
        if (result)
        {
            UpdateInfo();
            OnWeaponClick();
        }
        else
        {
            creditsCounter.ShowNoCreditsEffect();
        }
    }

    private void UpdateInfo()
    {
        creditsCounter.Show(Player.Credits);
        spaceCounter.Show(StarShip.GetCargoSpace(), StarShip.GetFullSpace());
        ShowEquipment(StarShip.Equipment);
    }

    private void ShowEquipment(ShipEquipment equipment)
    {
        shipButton.Show(equipment.Ship.Blueprint.Id, MergeParams(equipment.Ship.Blueprint), OnShipClick);
        driveButton.Show(equipment.Drive.Blueprint.Id, MergeParams(equipment.Drive.Blueprint), OnDriveClick);
        tankButton.Show(equipment.Tank.Blueprint.Id, MergeParams(equipment.Tank.Blueprint), OnTankClick);
        cargoModuleButton.Show(equipment.CargoModule.Blueprint.Id, MergeParams(equipment.CargoModule.Blueprint), OnCargoModuleClick);
        shieldButton.Show(equipment.Shield.Blueprint.Id, MergeParams(equipment.Shield.Blueprint), OnShieldClick);
        weaponButton.Show(equipment.Weapon.Blueprint.Id, MergeParams(equipment.Weapon.Blueprint), OnWeaponClick);
    }

    private string MergeParams(EquipmentBlueprintParams equip)
    {
        if (equip == null || equip.Params == null)
            return string.Empty;
        return string.Join(", ", equip.Params.Select(param => $"{param.Key}: {param.Value}"));
    }

    public void Start()
    {
        backButton.onClick.AddListener(Hide);
    }

    private void OnDestroy()
    {
        backButton.onClick.AddListener(Hide);
    }

    public void Update()
    {
        if (Input.GetKey(KeyCode.Escape))
        {
            Hide();
        }
    }
}
