using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.InputSystem;

[System.Serializable]
public class WeaponStat
{
    public WeaponData data; // 원본 데이터

    public WeaponStat(WeaponData data)
    {
        this.data = data;
    }
}

public class PlayerAttacks : MonoBehaviour
{
    [Header("Nano Weapons")]
    [SerializeField] private List<WeaponData> nanoWeaponList;

    [Header("Tera Weapons")]
    [SerializeField] private List<WeaponData> teraWeaponList;

    [SerializeField] private WeaponHitbox Hitbox;
    //[SerializeField] private WeaponHitbox nanoHitbox;
    //[SerializeField] private WeaponHitbox teraHitbox;

    // 런타임에서 계산된 스탯을 담아둘 리스트
    private List<WeaponStat> nanoStats = new List<WeaponStat>();
    private List<WeaponStat> teraStats = new List<WeaponStat>();

    private Animator animator;
    public bool IsAnyWeaponInUse = false;  // 공격중인지 확인
    PlayerController playerController;

    [SerializeField] private GameObject nano;
    [SerializeField] private GameObject tera;

    public enum Types {scissor, needle, lighter};

    void Start()
    {
        //Animator 가져오기
        playerController = GetComponent<PlayerController>();

        foreach (var data in nanoWeaponList)
            nanoStats.Add(new WeaponStat(data));

        foreach (var data in teraWeaponList)
            teraStats.Add(new WeaponStat(data));
    }

    void Update()
    {

    }

    public IEnumerator ableAtk(int weaponIndex, string atkType, bool isnano)
    {

        // 현재 캐릭터에 맞는 스탯과 히트박스 설정
        List<WeaponStat> currentList = isnano ? nanoStats : teraStats;
        WeaponHitbox currentHitbox = Hitbox;

        /*if (weaponIndex >= currentList.Count)
        {
            Debug.LogError($"{weaponIndex}번 무기 없");
            IsAnyWeaponInUse = false;
            yield break;
        }*/

        WeaponStat currentStat = currentList[weaponIndex];

        Debug.Log($"공격 대기 시작: {currentStat.data.frontdelaytime}초");
        yield return new WaitForSeconds(currentStat.data.frontdelaytime);

        animator = (isnano) ? nano.GetComponent<Animator>() : tera.GetComponent<Animator>();
        Debug.Log(atkType);
        animator.SetTrigger(atkType);

        currentHitbox.Setup(currentStat, atkType);

        currentHitbox.gameObject.SetActive(true);

        yield return new WaitForSeconds(currentStat.data.atkDuration);
        currentHitbox.gameObject.SetActive(false);

        Debug.Log($"후딜레이 대기 시작: {currentStat.data.afterdelaytime}초");
        yield return new WaitForSeconds(currentStat.data.frontdelaytime); // 후 딜레이

        IsAnyWeaponInUse = false;
    }

}
