using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// 
    /// </summary>
    class PgGridOutput
    {
        IPgGridVm viewModel;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="viewModel"></param>
        public void Init(IPgGridVm viewModel)
        {
            this.viewModel = viewModel;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="filepath"></param>
        public async void SaveToXlsx(string filepath)
        {
            if (viewModel.SeriesInfos.All(s => s.ADs.Count() == 0))
            {
                
                FLY.ControlLibrary.Window_WarningTip.Show("输出失败", "没有任何数据", TimeSpan.FromSeconds(2));
                return;

                //await ((MetroWindow)Application.Current.MainWindow).ShowMessageAsync("输出失败",
                //    "没有任何数据");
                //return;
            }

            FLY.ControlLibrary.Window_Tip.Show("请等待...", "开始导出");


            //var controller = await ((MetroWindow)Application.Current.MainWindow).ShowProgressAsync("请等待...", "开始导出",
            //    settings: new MetroDialogSettings()
            //    {
            //        NegativeButtonText = "停止",
            //        AnimateShow = false,
            //        AnimateHide = false,

            //    });
            ////controller.SetCancelable(true);
            //controller.SetIndeterminate();

            await Task.Factory.StartNew(() =>
            {
                using (ExcelPackage p = new ExcelPackage(new FileInfo(filepath)))
                {
                    SaveToXlsxThicks(p);
                    SaveToXlsxAD(p);
                    p.Save();
                }
            });

            //await controller.CloseAsync();

            //await ((MetroWindow)Application.Current.MainWindow).ShowMessageAsync("输出成功",
            //    filepath);
            FLY.ControlLibrary.Window_Tip.ShowShortTime("输出成功", filepath);
        }

        void SaveToXlsxAD(ExcelPackage p)
        {
            var seriesInfos = viewModel.SeriesInfos;
            int posOfGrid = viewModel.PosOfGrid;
            double mmpp = viewModel.Mmpp;
            var ws = p.Workbook.Worksheets.Add("AD数据");
            ws.Cells[1, 1].Value = "脉冲";
            ws.Cells[1, 2].Value = "mm";

            int max_pos_count = 0;
            for (int i = 0; i < seriesInfos.Count(); i++)
            {
                var info = seriesInfos[i];
                var values = info.ADs;
                int col = 3 + i;
                ws.Cells[1, col].Value = $"{info.Header}";
                for (int j = 0; j < values.Count(); j++)
                {
                    if(!double.IsNaN(values[j]))
                        ws.Cells[2 + j, col].Value = (int)values[j];
                }
                if (values.Count() <= max_pos_count)
                    continue;

                // 脉冲 & mm
                for (int j = max_pos_count; j < values.Count(); j++)
                {
                    ws.Cells[2 + j, 1].Value = j * posOfGrid;
                    ws.Cells[2 + j, 2].Value = j * posOfGrid * mmpp;
                }
                max_pos_count = values.Count();
            }

            //设置格式
            //格式
            ws.Column(1).Style.Numberformat.Format = "0";
            ws.Column(2).Style.Numberformat.Format = "0";
            for (int i = 0; i < seriesInfos.Count(); i++)
            {
                int col = 3 + i;
                ws.Column(col).Style.Numberformat.Format = "0";
            }
        }

        void SaveToXlsxThicks(ExcelPackage p)
        {
            var seriesInfos = viewModel.SeriesInfos;
            int posOfGrid = viewModel.PosOfGrid;
            double mmpp = viewModel.Mmpp;
            var ws = p.Workbook.Worksheets.Add("Thick数据");
            ws.Cells[1, 1].Value = "脉冲";
            ws.Cells[1, 2].Value = "mm";

            int max_pos_count = 0;
            for (int i = 0; i < seriesInfos.Count(); i++)
            {
                var info = seriesInfos[i];
                var values = info.Thicks;
                int col = 3 + i;
                ws.Cells[1, col].Value = $"{info.Header}";
                for (int j = 0; j < values.Count(); j++)
                {
                    if (!double.IsNaN(values[j]))
                        ws.Cells[2 + j, col].Value = values[j];
                }
                if (values.Count() <= max_pos_count)
                    continue;

                // 脉冲 & mm
                for (int j = max_pos_count; j < values.Count(); j++)
                {
                    ws.Cells[2 + j, 1].Value = j * posOfGrid;
                    ws.Cells[2 + j, 2].Value = j * posOfGrid * mmpp;
                }
                max_pos_count = values.Count();
            }

            //设置格式
            //格式
            ws.Column(1).Style.Numberformat.Format = "0";
            ws.Column(2).Style.Numberformat.Format = "0";
            for (int i = 0; i < seriesInfos.Count(); i++)
            {
                int col = 3 + i;
                ws.Column(col).Style.Numberformat.Format = "0.00";
            }
        }
    }
}
