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

namespace uBIMEarthTools.Utils
{
    internal class BackgroundTask
    {
        /// <summary>
        /// 后台任务
        /// </summary>
        /// <param name="action"></param>
        public static void BackgroundWork(Action action)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.DoWork += (s, e) =>
                {
                    try
                    {
                        action.Invoke();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerAsync();
            }
        }

        /// <summary>
        /// 后台任务
        /// </summary>
        /// <param name="action">要执行的任务</param>
        /// <param name="completed">完成后执行的任务</param>
        public static void BackgroundWork(Action<object, DoWorkEventArgs> action, Action<object, RunWorkerCompletedEventArgs> completed)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += (s, e) =>
                {
                    try
                    {
                        action.Invoke(s, e);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerCompleted += (s, e) =>
                {
                    try
                    {
                        completed.Invoke(s, e);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerAsync();
            }
        }

        /// <summary>
        /// 后台任务
        /// </summary>
        /// <param name="action">要执行的任务</param>
        /// <param name="completed">完成后执行的任务</param>
        public static void BackgroundWork(Action action, Action completed)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.DoWork += (s, e) =>
                {
                    try
                    {
                        action.Invoke();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerCompleted += (s, e) =>
                {
                    try
                    {
                        completed.Invoke();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerAsync();
            }
        }

        /// <summary>
        /// 后台任务
        /// </summary>
        /// <param name="action">要执行的任务</param>
        /// <param name="progress">任务进度监听</param>
        /// <param name="completed">任务完成监听</param>
        public static void BackgroundWork(Action<object, DoWorkEventArgs> action
            , Action<object, ProgressChangedEventArgs> progress
            , Action<object, RunWorkerCompletedEventArgs> completed)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.WorkerReportsProgress = true;
                bw.DoWork += (s, e) =>
                {
                    try
                    {
                        action.Invoke(s, e);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.ProgressChanged += (s, e) =>
                {
                    try
                    {
                        progress.Invoke(s, e);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerCompleted += (s, e) =>
                {
                    try
                    {
                        completed.Invoke(s, e);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerAsync();
            }
        }

        /// <summary>
        /// 后台执行任务
        /// </summary>
        /// <typeparam name="T">返回值类型</typeparam>
        /// <param name="action">要执行的耗时任务</param>
        /// <param name="completed">完成后执行的任务</param>
        public static void BackgroundWork<T>(System.Func<T> action, Action<T> completed)
        {
            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.DoWork += (s, e) =>
                {
                    try
                    {
                        e.Result = action.Invoke();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerCompleted += (s, e) =>
                {
                    try
                    {
                        completed.Invoke((T)e.Result);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                };
                bw.RunWorkerAsync();
            }
        }
    }
}
