using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Txgy.EWS.Client.Common
{
    public class TaskQueue
    {
        /// <summary>
        /// 任务队列
        /// </summary>
        private Queue<TaskData> QueuesTask = new Queue<TaskData>();
        public List<Task> tasks { get; set; } = new List<Task>();
        /// <summary>
        /// 任务队列是否在执行中
        /// </summary>
        private bool isExecuteing = false;

        private static TaskQueue _instance = null;
        public static TaskQueue Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new TaskQueue();
                return _instance;
            }
        }

        /// <summary>
        /// 任务是否进行中
        /// </summary>
        /// <returns></returns>
        public bool IsTasking()
        {
            return isExecuteing;
        }
        public int TaskCancel()
        {
            isExecuteing = false;
            //Console.WriteLine($"------------------未完成任务：{tasks.Count}");
            Task.WaitAll(tasks.ToArray());
            //Console.WriteLine($"------------------未完成任务：{tasks.Count}");
            return tasks.Count(ta => ta.Status == TaskStatus.Running);
        }
        /// <summary>
        /// 添加任务，任务会按照队列自动执行
        /// </summary>
        /// <param name="task"></param>
        public void AddTaskAndRuning(TaskData taskData)
        {
            if (taskData == null) return;

            QueuesTask.Enqueue(taskData);

            StartPerformTask();
        }

        /// <summary>
        /// 执行任务
        /// </summary>
        private async void StartPerformTask()
        {
            if (isExecuteing) { return; }

            while (QueuesTask.Count > 0)
            {
                isExecuteing = true;
                await Task.Run(() =>
                {
                    TaskData taskDatas = QueuesTask.Dequeue();
                    Task task = taskDatas.Tasks;
                    tasks.Add(task);
                    taskDatas.BeginTime = DateTime.Now;
                    task.Start();
                    task.Wait();
                    if (taskDatas.CallBack != null) taskDatas.CallBack((DateTime.Now - taskDatas.BeginTime).TotalMilliseconds.ToString());

                });
            }

            isExecuteing = false;
        }

        private TaskQueue()
        {
        }
        ~TaskQueue()
        {
            try
            {
                isExecuteing = false;
                //Console.WriteLine($"------------------未完成任务：{tasks.Count(ta=>ta.Status==TaskStatus.Running)}");
                Task.WaitAll(tasks.ToArray());
                //Console.WriteLine($"------------------未完成任务：{tasks.Count(ta => ta.Status == TaskStatus.Running)}");
            }
            catch { }
        }
    }

    public class TaskData
    {
        /// <summary>
        /// 任务名
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 任务
        /// </summary>
        public Task Tasks { get; set; }
        /// <summary>
        /// 任务完成后的回调
        /// </summary>
        public Action<string> CallBack { get; set; }
        public string FilePath { get; set; }
        public bool IsComplete { get; set; } = false;
        public string EventTimeStr { get; set; }
        /// <summary>
        /// 任务开始时间
        /// </summary>
        public DateTime BeginTime { get; set; }
        /// <summary>
        /// 任务结束时间
        /// </summary>
        public DateTime EndTime { get; set; }
    }
}
