using GCGame.Table;
using Module.Log;
using UnityEngine;
using UnityEngine.UI;

public class SkillProgressLogic : MonoBehaviour
{
    public enum ProgressModel
    {
        INVALD_TYPE = -1,
        ORDERMODEL,
        REVERSEDMODE
    }

    private static SkillProgressLogic _instance;
    private static ProgressBarLoadingState state = ProgressBarLoadingState.NotLoad;
    private static int _handleId;
    private static float _duration;
    private static float _startTime;
    private static ProgressModel _playModel;
    private static string _skillName;
    private static bool _needDotAnim; // 是否需要显示 "......." 的动画
    public GameObject _SkillNameGO;
    public Text dotAnim;

    public float dotAnimInternal;
    public int dotCount = 6;
    public int dotIndex;
    private float passedTime;

    public Slider progressSlider;
    public Text skillNameText;

    public static void PlayProcess(ProgressModel playModel, float fContinTime, string skillName = default(string),
        int handleId = 0)
    {
        _startTime = Time.time;
        _duration = fContinTime;
        _handleId = handleId;
        _playModel = playModel;
        _skillName = skillName;
        var str = StrDictionary.GetClientDictionaryString("#{4753}");
        if (_skillName.IndexOf(str) != -1)
            _needDotAnim = true;
        else
            _needDotAnim = false;

        if (state == ProgressBarLoadingState.NotLoad)
        {
            state = ProgressBarLoadingState.Loading;
            UIManager.ShowUI(UIInfo.SkillProgress, ShowUiOver);
        }
        else if (state == ProgressBarLoadingState.Loaded || _instance != null)
        {
            PlayProgress();
        }
    }

    private static void ShowUiOver(bool bSuccess, object param)
    {
        if (bSuccess)
        {
            state = ProgressBarLoadingState.Loaded;
            PlayProgress();
        }
        else
        {
            state = ProgressBarLoadingState.NotLoad;
            LogModule.ErrorLog(string.Format("{0}加载结束,但预制物不存在!", UIInfo.SkillProgress));
        }
    }

    private static void PlayProgress()
    {
        if (_instance == null)
            LogModule.ErrorLog(string.Format("{0}已经加载,但是_instance不存在!", UIInfo.SkillProgress));
        else
            _instance.PlaySkillProgress();
    }

    public static void StopProcess(int handleId)
    {
        //LogModule.ErrorLog(Time.frameCount + "   StopProcess : " + handleId);
        if (_handleId == handleId)
            if (_instance != null)
            {
                _instance.progressSlider.value = 0f;
                _instance.CloseWindow();
            }
    }

    private void Awake()
    {
        _instance = this;
        //gameObject.SetActive(false);
    }

    private void OnDestroy()
    {
        state = ProgressBarLoadingState.NotLoad;
        _instance = null;
    }

    public void PlaySkillProgress()
    {
        if (_needDotAnim)
        {
            dotAnim.gameObject.SetActive(true);
            dotAnim.text = "";
        }
        else
        {
            dotAnim.gameObject.SetActive(false);
        }

        gameObject.SetActive(true);
        if (string.IsNullOrEmpty(_skillName))
        {
            _SkillNameGO.SetActive(false);
        }
        else
        {
            skillNameText.text = _skillName;
            _SkillNameGO.SetActive(true);
        }
    }

    // Update is called once per frame
    private void Update()
    {
        if (_startTime + _duration > Time.time)
        {
            var ratio = (Time.time - _startTime) / _duration;
            if (_playModel == ProgressModel.REVERSEDMODE)
                ratio = 1 - ratio;

            progressSlider.value = ratio;

            if (_needDotAnim)
            {
                passedTime += Time.deltaTime;
                if (passedTime > dotAnimInternal)
                {
                    if (dotIndex < 6)
                    {
                        if (dotIndex == 3)
                            dotAnim.text += " .";
                        else
                            dotAnim.text += ".";

                        ++dotIndex;
                    }
                    else
                    {
                        dotIndex = 0;
                        dotAnim.text = "";
                    }

                    passedTime -= dotAnimInternal;
                }
            }
        }
        else
        {
            CloseWindow();
        }
    }

    public void CloseWindow()
    {
        _handleId = 0;
        gameObject.SetActive(false);
        state = ProgressBarLoadingState.Loaded;
    }

    private enum ProgressBarLoadingState
    {
        NotLoad,
        Loading,
        Loaded
    }
}