using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
using System;

public class ScreenFader : MonoBehaviour
{
    public Image _image = null;
    private Color _inColor;
    private Color _outColor;

    public float fadeInDuration = 2;
    public float intervalDuration = 0.5f;
    public float fadeOutDuration = 2;

    public void FadeIn(Action action)
    {
        _image
            .DOFade(1, fadeInDuration)
            .OnComplete(() =>
            {
                action();
            });
    }

    public void FadeOut() { }

    private void OnEnable()
    {
        _inColor = _image.color;
        _inColor.a = 1;

        _outColor = _image.color;
        _outColor.a = 0;

        _image.color = _outColor;
    }

    public void FadeInAndOut(Action intervalCallback = null, Action completeCallback = null)
    {
        Sequence sequence = DOTween.Sequence();

        sequence
            .Append(_image.DOFade(1, fadeInDuration).SetEase(Ease.InCirc))
            .AppendInterval(intervalDuration)
            .AppendCallback(() =>
            {
                intervalCallback();
            })
            .Append(_image.DOFade(0, fadeOutDuration).SetEase(Ease.OutCirc))
            .AppendCallback(() =>
            {
                completeCallback();
            });
    }

    [ContextMenu("Test")]
    public void Test()
    {
        FadeInAndOut(
            () =>
            {
                Debug.Log("Interval");
            },
            () =>
            {
                Debug.Log("Complete");
            }
        );
    }
}
