using BBWY.Client.APIServices;
using BBWY.Client.Models;
using BBWY.Client.Models.FallWare;
using BBWY.Controls;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Printing;
using System.IO;
using System.Reflection;
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.Shapes;

namespace BBWY.Client.Views.FallWare
{
    /// <summary>
    /// PrintBoxWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PrintBoxWindow : BWindow
    {
        public PrintBoxWindow(JDWareBoxModel model, WareType WareType, SealBoxService sealBoxService)
        {
            JDWareBoxModel = model; this.WareType = WareType; this.sealBoxService = sealBoxService;
            InitializeComponent();
            this.DataContext = this;
            InitPrintList();

        }
        public void InitPrintList()
        {
            //TaskImage = MyPrintHelper.GetBarcodeImage(TaskId.ToString(), 300, 60);

            //BarcodeImage = MyPrintHelper.GetBarcodeImage(SkuId, 300, 60);


            PrintList = new ObservableCollection<string>();
            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;
                }
                PrintList.Add(name);
            }
            try
            {
                var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                string printNames = System.IO.Path.Combine(applicationPath, "printName.init");
                if (File.Exists(printNames))
                {
                    PrintName = File.ReadAllText(printNames);
                }
                else
                {
                    if (PrintList.Count > 0)
                    {
                        PrintName = PrintList[0].ToString();
                    }
                }
            }
            catch (Exception)
            {


            }

        }

        public string PrintName { get; set; }
        public int PrintCount { get; set; } = 1;
        public SealBoxService sealBoxService { get; set; }


        public JDWareBoxModel JDWareBoxModel { get; set; }

        /// <summary>
        /// 箱唛类型
        /// </summary>
        public WareType WareType { get; set; }
        /// <summary>
        /// 打印机列表
        /// </summary>
        public ObservableCollection<string> PrintList { get; set; }

        private void BButton_Click(object sender, RoutedEventArgs e)
        {
            bool isNeedPrintAllBox = false;
            if (cbx_allsomebox.IsChecked != null && cbx_allsomebox.IsChecked.Value)//选择打印预约单的所有箱唛
                isNeedPrintAllBox = true;
            Task.Factory.StartNew(() =>
            {
                if (WareType == WareType.京仓)
                {

                    if (isNeedPrintAllBox)//选择打印预约单的所有箱唛
                    {
                        var datas = sealBoxService.GetWareLSOrderPurchaseList(JDWareBoxModel.PrewOrder);

                        foreach (var item in datas.Data.GetWareLSOrderPurchases)
                        {

                            App.Current.Dispatcher.Invoke(new Action(() =>
                            {

                                SetJDWareBoxWindow jdWindow = new SetJDWareBoxWindow(new JDWareBoxModel
                                {
                                    BoxCount = item.SealBoxCount.Value,
                                    PrewOrder = item.PrewOrder,
                                    ProductCount = item.ProductCount,
                                    ProductTitle = item.ProductTitle,
                                    ProviderName = item.ProviderName,
                                    PurchaseOrder = item.PurchaseOrder,
                                    WareName = item.WareName,
                                });
                                jdWindow.PrintBox(PrintName);
                            }));


                        }
                    }
                    else
                    {
                        App.Current.Dispatcher.Invoke(new Action(() =>
                        {

                            SetJDWareBoxWindow jdWindow = new SetJDWareBoxWindow(JDWareBoxModel);
                            jdWindow.PrintBox(PrintName);
                        }));
                    }




                }

                if (WareType == WareType.云仓 || WareType == WareType.聚水潭)
                {
                    App.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        SetCloudWareBoxWindow cloudWareWindow = new SetCloudWareBoxWindow(JDWareBoxModel, WareType);
                        cloudWareWindow.PrintBox(PrintName);
                    }));
                }

                App.Current.Dispatcher.Invoke(new Action(() =>
                {

                    this.Close();
                }));

            });

            var applicationPath = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            string printNames = System.IO.Path.Combine(applicationPath, "printName.init");
            try
            {
                if (File.Exists(printNames))
                {
                    File.Delete(printNames);
                }
                File.WriteAllText(printNames, PrintName);
            }
            catch
            {


            }
        }
    }
}