using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

[RequireComponent(typeof(CanvasGroup))]
public class UIMenu : MonoBehaviour
{
    [SerializeField] private UIMenu _nextMenu;
    [SerializeField] private UIMenu _previousMenu;

    private UIMenu _firstNextMenu;
    private UIMenu _firstPreviousMenu;

    private RectTransform _rectTransfrom;
    public CanvasGroup canvasGroup { get; private set; }

    private void OnEnabled()
    {
        canvasGroup.alpha = 1;
    }

    private void Awake()
    {
        _rectTransfrom = GetComponent<RectTransform>();
        canvasGroup = GetComponent<CanvasGroup>();

        _firstNextMenu = _nextMenu;
        _firstPreviousMenu = _previousMenu;
    }

    public void Reset()
    {
        _nextMenu = _firstNextMenu;
        _previousMenu = _firstPreviousMenu;
    }

    public void SetNextMenu(UIMenu menu) => _nextMenu = menu;
    public void SetPreviousMenu(UIMenu menu) => _previousMenu = menu;

    public void ShowNextMenu()
    {
        _nextMenu?.gameObject.SetActive(true);

        if(_nextMenu?.canvasGroup.alpha == 0)
            _nextMenu.canvasGroup.alpha = 1;

        canvasGroup.DOFade(0, .15f)
            .OnComplete(() => gameObject.SetActive(false));
    }

    public void ShowPreviousMenu()
    {
        _previousMenu?.gameObject.SetActive(true);

        if(_previousMenu?.canvasGroup.alpha == 0)
            _previousMenu.canvasGroup.alpha = 1;

        canvasGroup.DOFade(0, .15f)
            .OnComplete(() => gameObject.SetActive(false));
    }
}
