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

public abstract class PlayerUIManager : MonoBehaviour
{

    [SerializeField]
    protected InventoryUI Inventory;

    [SerializeField]
    private float AnimationSpeed = 0.5f;

    [SerializeField]
    protected LogicTrayUI TrayUI;

    protected Vector3 ShowPosition;
    private Vector3 HidePosition;
    private RectTransform inventoryRect { get { return Inventory.transform as RectTransform; } }

    protected virtual void Awake()
    {
        ShowPosition = new Vector3(0, -inventoryRect.rect.height / 2, 0);
        HidePosition = new Vector3(0, inventoryRect.rect.height/2, 0);
        inventoryRect.anchoredPosition = HidePosition;
    }

    [ContextMenu("Show")]
    public void OnClick_Show()
    {
        StopAllCoroutines();
        Inventory.UpdateUI();
        StartCoroutine( LerpPosition(inventoryRect, ShowPosition, AnimationSpeed));
    }

    [ContextMenu("Hide")]
    public void OnClick_Hide()
    {
        
        StopAllCoroutines();
        StartCoroutine( LerpPosition(inventoryRect, HidePosition, AnimationSpeed));
    }

    public void OnClick_Toggle()
    {
        if (Vector3.Distance(inventoryRect.anchoredPosition,ShowPosition) < Vector3.Distance(inventoryRect.anchoredPosition, HidePosition))
        {
            Debug.Log("Hiding Menu");
            OnClick_Hide();
        }
        else
        {
            Debug.Log("Showing Menu");
            OnClick_Show();
        }
    }

    public abstract void OnClick_Play();

    private IEnumerator LerpPosition(RectTransform rt, Vector3 endPosition, float time)
    {

        float elapsedTime = 0;
        Vector3 startPos = rt.anchoredPosition;

        while (elapsedTime < time)
        {
            rt.anchoredPosition = Vector3.Slerp(startPos, endPosition, (elapsedTime / time));
            elapsedTime += Time.deltaTime;
            yield return new WaitForEndOfFrame();
        }

        rt.anchoredPosition = endPosition;
    }

    
}