using System;
using UnityEngine;
using UnityEngine.UI;

public class BattleUI : MonoBehaviour
{
    [SerializeField]
    private Button escapeButton = null;
    [SerializeField]
    private Button fireButton = null;
    [SerializeField]
    private Button surrenderButton = null;
    [SerializeField]
    private Text playerStatus = null;
    [SerializeField]
    private Text enemyStatus = null;

    public event Action escapeClick = null;
    public event Action fireClick = null;
    public event Action surrenderClick = null;

    public void Hide()
    {
        gameObject.SetActive(false);
    }

    public void Show(StarShip player, StarShip enemy)
    {
        gameObject.SetActive(true);
        playerStatus.text = $"Player\nArmor: {player.Armor} / {player.MaxArmor}\nShield: {player.Shield} / {player.MaxShield}";
        enemyStatus.text = $"Pirate\nArmor: {enemy.Armor} / {enemy.MaxArmor}\nShield: {enemy.Shield} / {enemy.MaxShield}";
    }

    public void UpdatePlayer(StarShip player)
    {
        playerStatus.text = $"Player\nArmor: {player.Armor} / {player.MaxArmor}\nShield: {player.Shield} / {player.MaxShield}";
    }

    public void UpdateEnemy(StarShip enemy)
    {
        enemyStatus.text = $"Pirate\nArmor: {enemy.Armor} / {enemy.MaxArmor}\nShield: {enemy.Shield} / {enemy.MaxShield}";
    }

    public void LockControl(bool isLock)
    {
        escapeButton.interactable = !isLock;
        fireButton.interactable = !isLock;
        surrenderButton.interactable = !isLock;
    }

    private void OnEscapeClick()
    {
        escapeClick?.Invoke();
    }

    private void OnFireClick()
    {
        fireClick?.Invoke();
    }

    private void OnSurrenderClick()
    {
        surrenderClick?.Invoke();
    }

    private void Start()
    {
        escapeButton.onClick.AddListener(OnEscapeClick);
        fireButton.onClick.AddListener(OnFireClick);
        surrenderButton.onClick.AddListener(OnSurrenderClick);
    }
    private void OnDestroy()
    {
        escapeButton.onClick.RemoveListener(OnEscapeClick);
        fireButton.onClick.RemoveListener(OnFireClick);
        surrenderButton.onClick.RemoveListener(OnSurrenderClick);
    }
}
