using System.Collections;
using UnityEngine;

public class PlayerHandsController : MonoBehaviour
{
    public static PlayerHandsController Instance;

    [Header("Visual Models")]
    public GameObject emptyBagModel;
    public GameObject fullBagModel;

    [Header("Broom Settings")]
    [SerializeField] private GameObject broomModel;
    [SerializeField] private float sweepAnimationDuration = 1.5f;
    [SerializeField] private float sweepAngle = 30f;

    [Header("Rag Settings")]
    [SerializeField] private GameObject ragModel; // Model for the rag in hand
    [SerializeField] private float wipeAnimationDuration = 1.0f;
    [SerializeField] private float wipeDistance = 0.1f;

    private Coroutine _animationCoroutine;

    private void Awake()
    {
        Instance = this;
    }

    private void Start()
    {
        HideHands(); // Hides all models at the start
    }

    public void ShowBag(bool isFull)
    {
        HideHands();
        if (emptyBagModel) emptyBagModel.SetActive(!isFull);
        if (fullBagModel) fullBagModel.SetActive(isFull);
    }

    public void ShowBroom(bool state)
    {
        HideHands();
        if (broomModel != null)
        {
            broomModel.SetActive(state);
        }
    }

    public void ShowRag(bool state)
    {
        HideHands();
        if (ragModel != null)
        {
            ragModel.SetActive(state);
        }
    }

    public void HideHands()
    {
        if (emptyBagModel) emptyBagModel.SetActive(false);
        if (fullBagModel) fullBagModel.SetActive(false);
        if (broomModel) broomModel.SetActive(false);
        if (ragModel) ragModel.SetActive(false);
    }

    public void PlaySweepAnimation()
    {
        if (broomModel != null && broomModel.activeInHierarchy)
        {
            if (_animationCoroutine != null) StopCoroutine(_animationCoroutine);
            _animationCoroutine = StartCoroutine(SweepRoutine());
        }
    }

    public void PlayWipeAnimation()
    {
        if (ragModel != null && ragModel.activeInHierarchy)
        {
            if (_animationCoroutine != null) StopCoroutine(_animationCoroutine);
            _animationCoroutine = StartCoroutine(WipeRoutine());
        }
    }

    private IEnumerator SweepRoutine()
    {
        Quaternion startRotation = broomModel.transform.localRotation;
        Quaternion forwardRotation = startRotation * Quaternion.Euler(sweepAngle, 0, 0);
        Quaternion backwardRotation = startRotation * Quaternion.Euler(-sweepAngle / 2, 0, 0);
        float halfDuration = sweepAnimationDuration / 2;
        float timer = 0f;

        while (timer < halfDuration)
        {
            timer += Time.deltaTime;
            broomModel.transform.localRotation = Quaternion.Slerp(startRotation, forwardRotation, timer / halfDuration);
            yield return null;
        }
        timer = 0f;
        while (timer < halfDuration)
        {
            timer += Time.deltaTime;
            broomModel.transform.localRotation = Quaternion.Slerp(forwardRotation, backwardRotation, timer / halfDuration);
            yield return null;
        }
        broomModel.transform.localRotation = startRotation;
        _animationCoroutine = null;
    }

    private IEnumerator WipeRoutine()
    {
        Vector3 startPosition = ragModel.transform.localPosition;
        Vector3 forwardPosition = startPosition + new Vector3(wipeDistance, 0, 0);
        Vector3 backwardPosition = startPosition - new Vector3(wipeDistance, 0, 0);
        float quarterDuration = wipeAnimationDuration / 4;
        float timer = 0f;

        // Wipe forward
        while (timer < quarterDuration)
        {
            timer += Time.deltaTime;
            ragModel.transform.localPosition = Vector3.Lerp(startPosition, forwardPosition, timer / quarterDuration);
            yield return null;
        }
        // Wipe back
        timer = 0f;
        while (timer < quarterDuration * 2)
        {
            timer += Time.deltaTime;
            ragModel.transform.localPosition = Vector3.Lerp(forwardPosition, backwardPosition, timer / (quarterDuration * 2));
            yield return null;
        }
        // Return to start
        timer = 0f;
        while (timer < quarterDuration)
        {
            timer += Time.deltaTime;
            ragModel.transform.localPosition = Vector3.Lerp(backwardPosition, startPosition, timer / quarterDuration);
            yield return null;
        }

        ragModel.transform.localPosition = startPosition;
        _animationCoroutine = null;
    }
}