using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace 智慧物流软件系统.Tool
{
    public static class PrintTender
    {
        public static BarTender.Application Inbtapp;
        static BarTender.Format InbtFormat;

        //TO DO 打印机型号需要可配置
        static string PKNmae = "TSC TTP-244 Pro";

        static object lockll = new object();
        static object lockPrint = new object();

        //记录日志
        public static void WriteOneLine(string msg)//写到本地记录
        {
            lock (lockPrint)
            {
                try
                {
                    Logs.Write($"{msg}");
                }
                catch (Exception ex)
                {
                    ;
                }
            }
        }

        /// <summary>
        /// 打印外箱条码
        /// PKGip 打印机地址
        /// PKNmae 打印机名字
        /// </summary>
        /// <param name="PKGip"></param>
        /// <param name="test"></param>
        public static bool PrintTag(PrintClass printtext)
        {
            lock (lockll)
            {
                try
                {
                    if (Inbtapp == null)
                    {
                        Inbtapp = new BarTender.Application();
                    }
                    WriteOneLine($"标签开始打印");
                    InbtFormat = Inbtapp.Formats.Open(System.Environment.CurrentDirectory + "\\Resources\\物料条码.btw", false, "");
                    InbtFormat.PrintSetup.Printer = PKNmae;
                    InbtFormat.PrintSetup.IdenticalCopiesOfLabel = 1;  //设置同序列打印的份数
                    InbtFormat.SetNamedSubStringValue("MatCode", printtext.MatCode);
                    InbtFormat.SetNamedSubStringValue("MatName", printtext.MatName);
                    InbtFormat.SetNamedSubStringValue("MatBatch", "批次:" + printtext.MatBatch);
                    InbtFormat.SetNamedSubStringValue("MatQty", "数量:" + printtext.MatQty);
                    InbtFormat.SetNamedSubStringValue("MatSn", printtext.MatSn);
                    InbtFormat.SetNamedSubStringValue("MatSpec", "规格:" + printtext.MatSpec);
                    InbtFormat.SetNamedSubStringValue("DateTime", DateTime.Now.ToString());

                    Thread.Sleep(20);
                    int result = InbtFormat.PrintOut(true, false); //第二个false设置打印时是否跳出打印属性
                    InbtFormat.Close(BarTender.BtSaveOptions.btSaveChanges); //退出时是否保存标签
                    DateTime datenow = DateTime.Now;
                    Thread.Sleep(200);
                    return true;
                }
                catch (Exception ex)
                {
                    WriteOneLine("打标签失败" + ex.ToString());
                    return false;
                }
            }
        }

    }

    public class PrintClass
    {
        public string MatQty { get; set; }
        public string MatBatch { get; set; }
        public string MatCode { get; set; }
        public string MatName { get; set; }
        public string MatSn { get; set; }
        public string MatSpec { get; set; }
    }

}
