using System.Collections;
using System.Collections.Generic;
using CodesmithWorkshop.Useful;
using UnityEngine;
using VirtuoseReality.Extension.AudioManager;

public class PlayerAttack : MonoBehaviour
{
    [SerializeField]
    InputStatus status;

    [SerializeField]
    Animation_Player anima;

    public LayerMask sheepLayer;

    private List<string> _whoosh = new List<string>();

    private void Awake()
    {
        _whoosh = new List<string>()
        {
            SoundDataIDStatic.WOOSH01,
            SoundDataIDStatic.WOOSH02,
            SoundDataIDStatic.WOOSH03,
            SoundDataIDStatic.WOOSH06,
            SoundDataIDStatic.WOOSH07,
            SoundDataIDStatic.WOOSH08,
        };
    }

    public void Strike()
    {
        anima.Strike(); // Play Strike Animation
        SetHit(); // Late Set Hit Flag
        status.state = 1; // Set Status "NowStrike"
        Invoke("ResetState", 0.4f); // Late Reset Status "NowStrike" �� "Normal"
    }

    private void SetHit()
    {
        Collider[] hitColliders = Physics.OverlapBox(
            transform.position + Vector3.forward + Vector3.up,
            Vector3.one,
            Quaternion.identity,
            sheepLayer
        );

        if (hitColliders.Length > 0)
        {
            Collider collider = hitColliders[0];
            Debug.Log(collider.gameObject);

            if (collider.gameObject.TryGetComponent<Sheep>(out Sheep sheep))
                sheep.Strike(WeaponType.SquareRoot);
            else if (collider.gameObject.TryGetComponent<RestartBell>(out RestartBell restartBell))
                restartBell.Strike();
        }
    }

    private void PlayRandomWhoosh()
    {
        string rndID = UtilsClass.PickRandomInList<string>(_whoosh);
        SoundManager.PlaySound(rndID);
    }

    private void ResetState()
    {
        status.state = 0;
    }
}
