using BBWY.Client.Models;
using BBWY.Client.Models.APIModel;
using BBWY.Client.Views.FallWare;
using BBWY.Client.Views.PackTask;
using BBWY.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Drawing.Printing;
using System.IO;
using System.Printing;
using System.Reflection;
using System.Windows;
using WebSocketSharp;

namespace BBWY.Client.Views.SplitTask
{
    /// <summary>
    /// PrintExceptionTaskWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PrintExceptionTaskWindow : BWindow
    {
        public PrintExceptionTaskWindow(PackTaskModel model)
        {
            InitializeComponent();
            InitPrintList();
            this.DataContext = this;
            this.model = model;
        }

       private PackTaskModel model;



        /// <summary>
        /// 打印机列表
        /// </summary>
        public ObservableCollection<string> PrintList { get; set; }
        public string PrintName { get; set; }
        public int PrintCount { get=> printCount; set {Set(ref printCount,value); } }
        private int printCount = 1;


        public void InitPrintList()
        {

            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 void PrintBox()
        {

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

               
            });
        }

        private void BButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (PrintName.IsNullOrEmpty())
            {
                MessageBox.Show("请选择打印设备");
                return;
            }

            if (string.IsNullOrEmpty(PrintName))
            {
                return;
            }
            var localPrintServer = new LocalPrintServer();
            var printQueue = localPrintServer.GetPrintQueue(PrintName);
            if (printQueue.IsInError)
            {
                System.Windows.MessageBox.Show("打印机处于错误状态");
                return;
            }

            ExceptionTaskWindow window = new ExceptionTaskWindow(model);
            for (int i = 0; i < PrintCount; i++)
            {
                window.Print(PrintName);
            }

           

            this.Close();


        }
    }
}