using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace X2Engine
{
    public class DayOfTime : MonoBehaviour
    {
        public EnvironmentProfile m_curProfile = null;

        public float m_hour = 0.0f;  // hour
        public float m_timeScale =  24.0f * 60.0f * 60.0f / (5.0f * 60.0f);
        public EnvironmentProfile[] m_environmentProfile = new EnvironmentProfile[24];

        class SubTime
        {
            public float curTime;
            public float speed;
            public float totalTime;
            public EnvironmentProfile[] environmentProfile;
        };

        Queue<SubTime> m_subTimeList = new Queue<SubTime>();

        // Start is called before the first frame update
        void Start()
        {
            if (EnvironmentSystem.instance != null)
            {
                var len = m_environmentProfile.Length > 0 ? m_environmentProfile.Length : 1;

                float timeInterval = 24.0f / (float)len;
                int curIndex = (int)(m_hour / timeInterval) % len;

                EnvironmentProfile profile = ScriptableObject.CreateInstance<EnvironmentProfile>();
                profile.name = "Init_Profile";
                if(curIndex<m_environmentProfile.Length)
                {
                    profile.CopyFrom(m_environmentProfile[curIndex]);
                    EnvironmentSystem.instance.LoadProfile(profile);
                }
            }
        }

        private float _realtimeHour;
        private float _realtimeScale;
        public bool pause =false;
        public void ChangeToNoon()
        {
            _realtimeHour = m_hour;
            _realtimeScale = m_timeScale;
            m_hour = 0;
            m_timeScale = 0;
             pause = true;
        }
        public void ChangeToTime(float time)
        {
            _realtimeHour = m_hour;
            _realtimeScale = m_timeScale;
            m_hour = time;
            m_timeScale = 0;
            pause = true;
        }
        public void BackToCurrent()
        {
            m_hour = _realtimeHour;
            m_timeScale = _realtimeScale;
            pause =false;
        }

        public void InsertSubTime(EnvironmentProfile profile, float totalTime)
        {
            SubTime sub = new SubTime();
            sub.totalTime = totalTime;
            sub.curTime = 0;
            sub.speed = 1;
            sub.environmentProfile = new EnvironmentProfile[2];
            sub.environmentProfile[0] = profile;
            sub.environmentProfile[1] = ScriptableObject.CreateInstance<EnvironmentProfile>(); 
            sub.environmentProfile[1].CopyFrom(EnvironmentSystem.instance.profile);
            m_subTimeList.Enqueue(sub);
        }

        private void Update()
        {
            if(pause ==false)
                DoUpdate();
          
        }

        // Update is called once per frame
        public void DoUpdate()
        {
#if UNITY_EDITOR
            for(int i = 0; i < m_environmentProfile.Length; i++)
            {
                if(m_environmentProfile[i] == null)
                {
                    return;
                    //Debug.LogError("m_environmentProfile[" + i.ToString() + "] == null");
                }
            }
#endif
            if(m_subTimeList.Count > 0)
            {
                SubTime sub = m_subTimeList.Peek();
                sub.curTime += (sub.speed * Time.deltaTime);
                float s = sub.totalTime / sub.environmentProfile.Length;
                int index = (int)(sub.curTime / s);
                if (index >= sub.environmentProfile.Length)
                    index = sub.environmentProfile.Length - 1;
               
                float t = sub.curTime - index * s;
                t /= s;
                if (t > 1.0f)
                    t = 1.0f;
                if (EnvironmentSystem.instance != null)
                {
                    
                    EnvironmentSystem.instance.profile.Lerp(sub.environmentProfile[index],  t);

                }
                if (sub.curTime >= sub.totalTime)
                {
                    m_subTimeList.Dequeue();
                }
                return;
            }

            m_hour += (Time.deltaTime * m_timeScale * 1.0f / (60.0f * 60.0f));
            m_hour = ((int)m_hour) % 24 + (m_hour - (int)m_hour);   // m_hour %= 24;

            var len = m_environmentProfile.Length > 0 ? m_environmentProfile.Length : 1;

            float timeInterval = 24.0f / (float)len;
            int curIndex = (int)(m_hour / timeInterval) % len;
            int nextIndex = (curIndex + 1) % len;
            float blendWeight = (m_hour - curIndex * timeInterval) / timeInterval;

            if(curIndex<m_environmentProfile.Length)
            {
                m_curProfile = m_environmentProfile[curIndex];

                if (EnvironmentSystem.instance != null)
                {
                    EnvironmentSystem.instance.profile.Lerp(m_environmentProfile[curIndex], m_environmentProfile[nextIndex], blendWeight);
                }
            }
        }
    }

}   // namespace X2Engine
