using FLY.Thick.FilmCasting.UI.DbViewer.Core;
using MahApps.Metro.Controls.Dialogs;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Unity;

namespace FLY.Thick.FilmCasting.UI.DbViewer
{
    /// <summary>
    /// PageMain.xaml 的交互逻辑
    /// </summary>
    public partial class PgMain : Page
    {
        IUnityContainer container;
        DbViewerModel dbViewerModel;

        public PgMain()
        {
            InitializeComponent();
        }
        [Unity.InjectionMethod]
        public void Init(IUnityContainer container, DbViewerModel dbViewerModel)
        {
            this.container = container;
            this.dbViewerModel = dbViewerModel;
            this.DataContext = dbViewerModel;
        }
        private void ButtonSelect_Click(object sender, RoutedEventArgs e)
        {
            PgSelect p = new PgSelect();
            container.BuildUp(p);
            this.NavigationService.Navigate(p);
        }

        private void ButtonChart_Click(object sender, RoutedEventArgs e)
        {

        }

        private async void ButtonExcel_Click(object sender, RoutedEventArgs e)
        {
            string strDesktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

            //导出一个文件夹

            System.Windows.Forms.FolderBrowserDialog open = new System.Windows.Forms.FolderBrowserDialog();

            open.SelectedPath = strDesktopPath;

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ExcelOuput ouput = new ExcelOuput();

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

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

                string dirPath = System.IO.Path.Combine(open.SelectedPath, $"流延记录_{DateTime.Now:yyyyMMddHHmmss}");
                //创建文件夹
                System.IO.Directory.CreateDirectory(dirPath);

                ouput.PropertyChanged += Ouput_PropertyChanged;
                for (int i = 0; i < dbViewerModel.ProfilePacks.Count(); i++) {

                    var pack = dbViewerModel.ProfilePacks[i];
                    string fileName = $"{pack.Profile.PName}-{pack.Profile.Batch}-{pack.Profile.Number}-{pack.Profile.ID}.xlsx";
                    fileName = MakeFileNameValid(fileName);
                    
                    string filePath = System.IO.Path.Combine(dirPath, fileName);
                    await ouput.SaveToXlsx(filePath, pack);
                }
                ouput.PropertyChanged -= Ouput_PropertyChanged;

                await controller.CloseAsync();
                controller = null;

                await App.MetroWindow.ShowMessageAsync("输出成功",
                    open.SelectedPath);
            }
        }
        string MakeFileNameValid(string fileName) 
        {

            StringBuilder sb = new StringBuilder(fileName);
            //不能让它有特殊符合
            var invalidChars = System.IO.Path.GetInvalidFileNameChars();
            for (int j = 0; j < sb.Length; j++)
            {
                if (invalidChars.Contains(sb[j]))
                    sb[j] = 'x';
            }
            fileName = sb.ToString();
            return fileName;
        }
        ProgressDialogController controller;
        private void Ouput_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var output = sender as ExcelOuput;
            if ((e.PropertyName == nameof(ExcelOuput.Progress)) ||
                (e.PropertyName == nameof(ExcelOuput.Msg)))
            {
                controller.SetProgress(output.Progress);
                controller.SetMessage(output.Msg);
            }
        }


        private void ButtonSetup_Click(object sender, RoutedEventArgs e)
        {
            PgSetup p = new PgSetup();
            container.BuildUp(p);
            this.NavigationService.Navigate(p);
        }
    }
}
