using BBWY.Client.Extensions;
using BBWY.Client.Helpers;
using BBWY.Client.Models.PackTask;
using BBWY.Controls;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Spire.Xls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Printing;
using System.IO;
using System.Printing;
using System.Reflection;
using System.Text;
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;

namespace BBWY.Client.Views.SealBox
{
    /// <summary>
    /// SetSealBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class SetSealBoxWindow : BWindow
    {
        public SetSealBoxWindow(SealBoxModel sealBoxModel)
        {
            SealBoxModel = sealBoxModel;
            InitializeComponent();
            LoadPrints();
        }


        SealBoxModel SealBoxModel { get; set; }
        /// <summary>
        /// 获取打印机名称
        /// </summary>
        private void LoadPrints()
        {
            var printingNames = PrinterSettings.InstalledPrinters;//获取本机的打印机数据
            int index = -1;
            int selectIndex = 0;
            foreach (string name in printingNames)
            {
                if (name == "Microsoft XPS Document Writer" || name == "Microsoft Print to PDF" || name == "Fax")
                {
                    continue;
                }
                //index++;
                //if (name.Contains("Deli"))
                //{
                //    selectIndex = index;
                //}
                cbPrint.Items.Add(name);
            }
            //if (cbPrint.Items.Count > selectIndex)
            //{
            //    cbPrint.SelectedIndex = selectIndex;
            //}
            var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string printNamePath = $"{applicationPath}/printSealName.init";
            if (File.Exists(printNamePath))
            {
                cbPrint.Text = File.ReadAllText(printNamePath);
            }
        }


        public Action<int> SendBoxCount { get; set; }
        private void BButton_Click(object sender, RoutedEventArgs e)
        {





            int boxCount = 0;
            try
            {
                boxCount = Convert.ToInt32(tbSealBoxCount.Text);
            }
            catch (Exception)
            {
                MessageBox.Show("请输入数字!");
                return;
            }
            string printName = cbPrint.Text;
            var localPrintServer = new LocalPrintServer();
            var printQueue = localPrintServer.GetPrintQueue(printName);
            if (printQueue.IsInError)
            {
                MessageBox.Show("打印机处于错误状态");
                return;
            }

            //PrintSealboxModel(printName, boxCount);

            MyPrintHelper.PrintSealBoxData(SealBoxModel, printName, boxCount, 1);
            //todo: 打印单子
            if (SendBoxCount != null) SendBoxCount(boxCount);

            this.Close();
        }

        private void PrintSealboxModel(string printName, int boxCount, int printCount = 1)
        {


            //

        }
        private void PrintSealboxModel1(string printName, int boxCount)
        {
            var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string printNamePath = $"{applicationPath}/printSealName.init";
            if (File.Exists(printNamePath))
            {
                File.Delete(printNamePath);
            }
            File.WriteAllText(printNamePath, printName);

            var excel = $"{applicationPath}/Resources/ExccelModel/sealbox.xlsx";

            var newExccel = $"{applicationPath}/Resources/ExccelModel/newsealbox.xlsx";
            if (File.Exists(newExccel))
            {
                File.Delete(newExccel);
            }
            FileStream fs = new FileStream(excel, FileMode.Open, FileAccess.Read);
            XSSFWorkbook wb = new XSSFWorkbook(fs);
            var sheet = wb.GetSheetAt(0);  // 得到第一个sheet

            var nameCell = sheet.GetRow(0).GetCell(0);  // name列,第2行
            var ageCell = sheet.GetRow(3).GetCell(0);   // age列,第2行


            XSSFWorkbook wb2 = new XSSFWorkbook();
            //wb2.CreateSheet("Sheet1");
            sheet.CopyTo(wb2, "Sheet1", true, false);
            var sheet2 = wb2.GetSheet("Sheet1");


            var nameCell2 = sheet2.GetRow(0).GetCell(1);
            nameCell2.SetCellValue(SealBoxModel.ShopName);

            sheet2.GetRow(3).GetCell(1).SetCellValue(SealBoxModel.WareName);

            StringBuilder sb = new StringBuilder();
            int totalCount = 0;
            for (int i = 0; i < SealBoxModel.SealBoxSkus.Count; i++)
            {
                var title = SealBoxModel.SealBoxSkus[i].SkuTitle;
                //if (title.Length>5)
                //{
                //    title = title.Substring(0,5);
                //}
                totalCount += SealBoxModel.SealBoxSkus[i].SkuCount;
                sheet2.GetRow(i + 6).GetCell(1).SetCellValue($"名称:{title}");
                sheet2.GetRow(i + 6).GetCell(2).SetCellValue($"SKU:{SealBoxModel.SealBoxSkus[i].SkuId}");
                sheet2.GetRow(i + 6).GetCell(3).SetCellValue($"数量:{SealBoxModel.SealBoxSkus[i].SkuCount}");

            }
            sheet2.GetRow(25).GetCell(1).SetCellValue(totalCount);
            sheet2.GetRow(28).GetCell(1).SetCellValue(boxCount);
            FileStream fs1 = new FileStream(newExccel, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            wb2.Write(fs1);
            fs1.Close();
            fs1.Dispose();
            //Workbook workbook = new Workbook();
            //workbook.loa
            //workbook.LoadFromFile(newExccel);
            //var print = workbook.PrintDocument;
            //print.PrinterSettings.PrinterName = printName;
            //print.Print();

            //

        }

        void SpirePrint()
        {

        }
    }


}