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

namespace DuckHunter
{
    /// <summary>
    /// 游戏屏幕中心的提示
    /// </summary>
    public class TextGameScreenCenter : MonoBehaviour
    {
        private Text _mainText;
        private TextAutoLanguage2 _mainTextAutoLanguage2;
        private Sequence _mainSequence;

        public static TextGameScreenCenter Instance;

        void Awake()
        {
            Instance = this;
            _mainText = GetComponent<Text>();
            _mainTextAutoLanguage2 = GetComponent<TextAutoLanguage2>();
        }

        void OnDestroy()
        {
            if (Instance == this) Instance = null;
        }

        public enum TextName
        {
            GAME_START,
            GAME_START_HIDE,
            GAME_START_FADEOUT,
            ROUND_X,
            SUPER_ARCHER,
            GAME_OVER,
            GAME_COMPLETED,
            FLY_AWAY,
        }


        public void ShowText(TextName textName, object[] args = null, Action onComplete = null)
        {
            if (args == null) args = new object[] { };
            _mainTextAutoLanguage2.textFormatArgs = args;
            if (_mainSequence != null)
            {
                _mainSequence.Kill();
                _mainSequence = null;
            }
            _mainText.alignment = TextAnchor.MiddleCenter;
            if (textName == TextName.GAME_START)
            {
                _mainTextAutoLanguage2.SetTextKey("SC-GAME_START");
                SetAlpha(_mainText, 1);
                onComplete?.Invoke();
            }
            else if (textName == TextName.GAME_START_HIDE)
            {
                _mainTextAutoLanguage2.SetTextKey("SC-GAME_START");
                SetAlpha(_mainText, 0);
                onComplete?.Invoke();
            }
            else if (textName == TextName.GAME_START_FADEOUT)
            {
                _mainTextAutoLanguage2.SetTextKey("SC-GAME_START");
                SetAlpha(_mainText, 1);
                _mainSequence = DOTween.Sequence();
                _mainSequence.Append(_mainText.DOFade(0, 0.5f));
                _mainSequence.AppendCallback(() => onComplete?.Invoke());
            }
            else if (textName == TextName.ROUND_X)
            {
                _mainTextAutoLanguage2.SetTextKey("SC-ROUND_X");
                SetAlpha(_mainText, 0);
                _mainSequence = DOTween.Sequence();
                _mainSequence.Append(_mainText.DOFade(1, 0.3f));
                _mainSequence.AppendInterval(2.4f);
                _mainSequence.Append(_mainText.DOFade(0, 0.3f));
                _mainSequence.AppendCallback(() => onComplete?.Invoke());
            }
            else if (textName == TextName.SUPER_ARCHER)
            {
                _mainText.alignment = TextAnchor.MiddleLeft;
                _mainTextAutoLanguage2.SetTextKey("SC-SUPER_ARCHER");
                SetAlpha(_mainText, 1);
                _mainSequence = DOTween.Sequence();
                _mainSequence.AppendInterval(2.5f);
                _mainSequence.Append(_mainText.DOFade(0, 0.5f));
                _mainSequence.AppendCallback(() => onComplete?.Invoke());
            }
            else if (textName == TextName.GAME_OVER)
            {
                _mainTextAutoLanguage2.SetTextKey("SC-GAME_OVER");
                SetAlpha(_mainText, 1);
                _mainSequence = DOTween.Sequence();
                _mainSequence.AppendInterval(3);
                _mainSequence.AppendCallback(() => onComplete?.Invoke());
            }
            else if (textName == TextName.GAME_COMPLETED)
            {
                // _mainTextAutoLanguage2.SetTextKey("SC-GAME_COMPLETED");
                _mainTextAutoLanguage2.SetTextKey(""); //游戏通关不显示文字
                SetAlpha(_mainText, 1);
                _mainSequence = DOTween.Sequence();
                _mainSequence.AppendInterval(3);
                _mainSequence.AppendCallback(() => onComplete?.Invoke());
            }
            else if (textName == TextName.FLY_AWAY)
            {
                _mainTextAutoLanguage2.SetTextKey("SC-FLY_AWAY");
                SetAlpha(_mainText, 1);
                _mainSequence = DOTween.Sequence();
                _mainSequence.AppendInterval(2);
                _mainSequence.Append(_mainText.DOFade(0, 0.5f));
                _mainSequence.AppendCallback(() => onComplete?.Invoke());
            }
        }

        private void SetAlpha(Text text, float value)
        {
            Color color = text.color;
            color.a = value;
            text.color = color;
        }
    }
}
