using System.Diagnostics;
using System.Threading.Tasks;

namespace WS
{
    ///<summary>任务组件</summary>
    public class WSTaskComponent : WSComponent
    {
        private ProcessManager process;
        ///<summary>流程管理期</summary>
        private ProcessManager Process
        {
            get
            {
                if (process == null)
                    process = Parent.Parent as ProcessManager;
                return process;
            }
        }

        ///<summary>Id</summary>
        public int Id { get { return Data.Id; } }
        ///<summary>名称</summary>
        public string Name { get { return Data.Name; } }
        ///<summary>分数</summary>
        public float Score { get { return Data.Score; } }
        ///<summary>是否完成</summary>
        private bool isComplete;
        ///<summary>是否完成</summary>
        public bool IsComplete
        {
            get { return isComplete; }
            set
            {
                if (value && !isComplete)
                {
                    Process.OnCompleteVTaskCallback?.Invoke(Id);
                    if (Data.Perform)
                        Process.GetVTask(Data.PerformId).StartVTask();
                }
                isComplete = value;
            }
        }
        ///<summary>数据</summary>
        public WSTask Data;
        ///<summary>任务错误</summary>
        public bool IsError;
        ///<summary>当前操作</summary>
        private BaseOperate CurrentOperate;

        //帧循环
        public void Update()
        {
            if (CurrentOperate != null)
            {
                if (!CurrentOperate.IsComplete)
                    CurrentOperate.Update();
                else
                    SwitchOperate();
            }
        }

        ///<summary>开始任务</summary>
        public void StartVTask()
        {
#if UNITY_EDITOR || DEBUG_MODEL
            Log.LogDebug($"进入任务:{Name} ID:{Id}", WSLogType.橙);
#endif
            SwitchOperate();
        }

        /// <summary>跳任务</summary>
        /// <param name="invertedorder">倒序</param>
        public async Task JumpVTask(bool invertedorder)
        {
            try
            {
                int count = Data.Operates.Count;
                if (!invertedorder)
                {
                    int startid = CurrentOperate != null ? CurrentOperate.Id : 0;
                    if (CurrentOperate != null)
                    {
                        await CurrentOperate.EndState();
                        CurrentOperate.Dispose();
                    }
                    for (int i = startid; i < count; i++)
                    {
                        await GetJumpOperate(i, invertedorder);
                    }
                    IsComplete = true;
                }
                else
                {
                    IsComplete = false;
                    int startid = CurrentOperate != null ? CurrentOperate.Id - 2 : count - 1;
                    if (CurrentOperate != null)
                    {
                        await CurrentOperate?.StartState();
                        CurrentOperate?.Dispose();
                    }
                    for (int i = startid; i >= 0; i--)
                    {
                        await GetJumpOperate(i, invertedorder);
                    }
                }
                CurrentOperate = null;
            }
            catch (System.Exception ex)
            {
                Log.LogError(ex);
            }
        }

        ///<summary>获取跳转操作</summary>
        /// <param name="invertedorder">倒序</param>
        private async Task GetJumpOperate(int i, bool invertedorder)
        {
            BaseOperate operate = Process.GetOperate(Data.Operates[i], this);
            if (!invertedorder)
                await operate.EndState();
            else
                await operate.StartState();
            operate.Dispose();
        }

        ///<summary>设置任务错误</summary>
        public void SetVTaskError()
        {
            IsError = true;
        }

        ///<summary>获取关联任务</summary>
        public WSTaskComponent GetContactVTask()
        {
            if (Data.Get)
            {
                return Process.GetVTask(Data.GetId);
            }
            return null;
        }

        ///<summary>提示</summary>
        public void Prompt()
        {
            CurrentOperate.Prompt();
        }

        ///<summary>切换操作</summary>
        private void SwitchOperate()
        {
            if (CurrentOperate != null)
            {
                int index = CurrentOperate.Id;
                CurrentOperate.Dispose();
                if (Data.Operates.Count > index)
                    CurrentOperate = Process.GetOperate(Data.Operates[index], this);
                else
                    IsComplete = true;
            }
            else
                CurrentOperate = Process.GetOperate(Data.Operates[0], this);
            if (!IsComplete)
                CurrentOperate.StartOperate();
        }

        public override void Dispose()
        {
            base.Dispose();
            isComplete = false;
            Data = null;
            IsError = false;
            if (CurrentOperate != null)
            {
                if (!CurrentOperate.IsDisposed)
                    CurrentOperate.Dispose();
                CurrentOperate = null;
            }
        }
    }
}