using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

// 백그라운드 + 버튼 + 텍스트 로 구성된 오브젝트
public class Button_Action : MonoBehaviour
{
    private Button _attackButton;
    private Button[] _skillButtons = new Button[3];

    public event Action OnAttackButtonClicked;
    public event Action<int> OnSkillButtonClicked;
    
    private float _mainButtonSize = 180f; // 조이스틱 크기(300f)의 0.6배
    private float _subButtonSize = 120f;
    private float _subButtonDistance = 200f; // 메인과 서브의 거리

    private void Awake()
    {
        _skillButtons = new Button[3];
        CreateAttackButtons();
        CreateSkillButtons();
    }

    private void Start()
    {
        // Manager.Input.Buttons = this;
        // CreateAttackButtons();

    }
    

    void CreateAttackButtons()
    {
        GameObject ButtonObj = Manager.Resource.Instantiate("Prefabs/UI/Button_Action", transform);
        ButtonObj.name = "AttackButton";
        _attackButton = ButtonObj.GetComponent<Button>();

        RectTransform rectTransform = _attackButton.GetComponent<RectTransform>();
        rectTransform.sizeDelta = new Vector2(_mainButtonSize, _mainButtonSize);
        
        rectTransform.anchorMin = new Vector2(1, 0);
        rectTransform.anchorMax = new Vector2(1, 0);
        rectTransform.pivot = new Vector2(0.5f, 0.5f);
        
        rectTransform.anchoredPosition = new Vector2(-100f - _mainButtonSize / 2, 100f + _mainButtonSize / 2);
        _attackButton.onClick.AddListener(ClickedAttackButton);
        
    }

    void CreateSkillButtons()
    {
        float[] angles = { 90f, 135f, 180f};
        for (int i = 0; i < 3; i++)
        {
            GameObject ButtonObj = Manager.Resource.Instantiate("Prefabs/UI/Button_Action", transform);
            ButtonObj.name = $"SkillButton_{i}";
            _skillButtons[i] = ButtonObj.GetComponent<Button>();
            
            RectTransform rectTransform = _skillButtons[i].GetComponent<RectTransform>();
            rectTransform.sizeDelta = new Vector2(_subButtonSize, _subButtonSize);
            
            rectTransform.anchorMin = new Vector2(1, 0);
            rectTransform.anchorMax = new Vector2(1, 0);
            rectTransform.pivot = new Vector2(0.5f, 0.5f);
            
            float angle = angles[i] * Mathf.Deg2Rad;
            Vector2 mainPos = _attackButton.GetComponent<RectTransform>().anchoredPosition;
            Vector2 skillPos = new Vector2(
                mainPos.x + _subButtonDistance * Mathf.Cos(angle),
                mainPos.y + _subButtonDistance * Mathf.Sin(angle));
            
            rectTransform.anchoredPosition = skillPos;
            
            int idx = i;
            _skillButtons[i].onClick.AddListener(() => ClickedSkillButton(idx));
        }
    }
    
    void ClickedAttackButton()
    {
        OnAttackButtonClicked?.Invoke();
    }

    void ClickedSkillButton(int index)
    {
        OnSkillButtonClicked?.Invoke(index);
    }
    
    public void SetSkillButtonEvent(int index, UnityEngine.Events.UnityAction action)
    {
        if (index >= 0 && index < _skillButtons.Length)
        {
            _skillButtons[index].onClick.AddListener(action);
        }
    }
}
