using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Module.Log;

// 经验副本场景动画
public class DailyCopySceneAnimCtr : MonoBehaviour {
    private float deltaTime = 1.0f / 60;
    //private static DailyCopySceneAnimCtr instance;
    //public static DailyCopySceneAnimCtr Instance
    //{
    //    get {
    //        if (instance == null)
    //        {
    //            GameObject go = GameObject.Find("Sence");
    //            if (go == null)
    //            {
    //                LogModule.ErrorLog("Can't find gameObjet(Sence) to add DailyCopySceneAnimCtr component !");
    //            }
    //            else
    //            {
    //                var c = go.GetComponent<DailyCopySceneAnimCtr>();
    //                if (c != null)
    //                {
    //                    Destroy(c);
    //                }

    //                go.AddComponent<DailyCopySceneAnimCtr>();
    //            }
    //        }

    //        return instance;
    //    }
    //}

    private void Awake()
    {
        //if (instance == null)
        //{
        //    instance = this;
        //}

        var wallGO = GameObject.Find("FubenAnim");
        if (wallGO == null)
        {
            LogModule.ErrorLog("Can't find wall");
        }
        wall = wallGO.transform;

        var floorComponent = wallGO.transform.Find("floor");
        if (floorComponent == null)
        {
            LogModule.ErrorLog("Can't find floor");
        }
        floor = floorComponent.gameObject;

        initHeight = wall.position.y;
        minHeight = initHeight - delHeight;
    }

    private void OnDestroy()
    {
        //if (instance != null)
        //{
        //    instance = null;
        //}
    }

    private Transform wall;
    private GameObject floor;
    private float delHeight = 10.0f;
    private float changeHeight = 0.5f;
    private float speed = 0.01f;

    private float initHeight;
    private float minHeight;
    private bool isFristTime = false;           // 记录是否第一次,仅在第一次显示 floor

    private Coroutine c;

    public void MoveNext()
    {
        if (c == null)
        {
            c = StartCoroutine(MoveDown());
        }
    }

    private IEnumerator MoveDown()
    {
        Vector3 tempTarget = wall.position;
        tempTarget.y = minHeight;
        //bool hasChange = false;
        float deltaTime_Temp = 0.0f;
        Vector3 tempPos = wall.position;
        while (true)
        {
            // 手动控制位移执行间隔,防止低帧率游戏动画变慢问题。
            deltaTime_Temp += Time.deltaTime;
            if (deltaTime_Temp > deltaTime)
            {
                while (deltaTime_Temp > deltaTime)
                {
                    tempPos += Vector3.down * speed;
                    deltaTime_Temp -= deltaTime;
                }

                wall.position = tempPos;
                if (wall.position.y < (minHeight + changeHeight))
                {
                    //hasChange = true;
                    float delta_1 = minHeight - wall.position.y;
                    float delta_2 = minHeight - wall.position.y;
                    wall.position = new Vector3(wall.position.x, initHeight - delta_1, wall.position.z);
                    //tempTarget.y = initHeight;

                    if (!isFristTime)
                    {
                        isFristTime = true;
                        floor.SetActive(false);
                    }
                }

                tempPos = wall.position;
            }

            yield return null;
        }
    }

    public void StopMoveNext()
    {
        if (c != null)
        {
            StopCoroutine(c);
        }
    }
}