using BBWY.Client.Models.APIModel;
using BBWY.Client.Models.PackTask;
using BBWY.Client.Views.SealBox;
using BBWY.Controls;
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>
    /// PrintLSOrderWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PrintLSOrderWindow : BWindow
    {
        public PrintLSOrderWindow(List<GetWareLSOrderPurchase> getWareLSOrderPurchases, string prewOrder, GetWareLSAcceptOrderResponse getWareLSAcceptOrderResponse)
        {
            InitializeComponent();
            InitPrintList();

            this.DataContext = this;
            GetWareLSOrderPurchases = getWareLSOrderPurchases;
            PrewOrder = prewOrder;
            GetWareLSAcceptOrderResponse = getWareLSAcceptOrderResponse;
        }
        /// <summary>
        /// 打印机列表
        /// </summary>
        public ObservableCollection<string> PrintList { get; set; }
        public string PrintName { get; set; }
        public int PrintCount { get; set; } = 1;
        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)
            {


            }

        }

        /// <summary>
        /// 采购单列表
        /// </summary>
        public List<GetWareLSOrderPurchase> GetWareLSOrderPurchases { get; set; }
        public string PrewOrder { get; set; }
        public GetWareLSAcceptOrderResponse GetWareLSAcceptOrderResponse { get; set; }



        private void BButton_Click(object sender, RoutedEventArgs e)
        {
            int limitIndex = 10;//单页限制数量

            int totalPageIndex = GetWareLSOrderPurchases.Count % 10 == 0 ? GetWareLSOrderPurchases.Count / 10 : GetWareLSOrderPurchases.Count / 10 + 1;


            Task.Factory.StartNew(() =>
            {

                for (int i = 0; i < PrintCount; i++)//打印份数
                {
                    int nowPageIndex = 0;
                again:
                    nowPageIndex++;
                    if (GetWareLSOrderPurchases.Count > limitIndex)
                    {
                        PrintBox(GetWareLSOrderPurchases.GetRange(0, limitIndex), nowPageIndex, totalPageIndex);

                        GetWareLSOrderPurchases.RemoveRange(0, limitIndex);
                        goto again;
                    }
                    else
                    {
                        PrintBox(GetWareLSOrderPurchases, nowPageIndex, totalPageIndex);
                    }
                }
                App.Current.Dispatcher.Invoke(() => {
                    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
            {


            }
        }

        public void PrintBox(List<GetWareLSOrderPurchase> GetWareLSOrderPurchases, int nowPageIndex, int totalPageIndex)
        {

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

                WareLSAcceptOrderWindow lSAcceptOrderWindow = new WareLSAcceptOrderWindow(GetWareLSAcceptOrderResponse, GetWareLSOrderPurchases, nowPageIndex, totalPageIndex, PrewOrder);
                //lSAcceptOrderWindow.Show();
                lSAcceptOrderWindow.PrintLSOrder(PrintName);
            });
        }
    }
}