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

namespace Project.Device
{
	public class SATOPrinter : arDev.RS232
	{
		public string LastPrintZPL = string.Empty;
		public string qrData = string.Empty;
        public string ZPLFileName { get; set; } =  UTIL.MakePath("data","zpl.txt");
        public string baseZPL
        {
            get
            {
                var fi = new System.IO.FileInfo(ZPLFileName);
                if (fi.Exists == false || fi.Length == 0)
                {
                    PUB.log.AddE($"{ZPLFileName} does not exist or has no data. Changed to default zpl.txt");
                    fi = new System.IO.FileInfo( UTIL.MakePath("data", "zpl.txt"));
                    if (fi.Exists == false) PUB.log.AddE("Print template file (zpl.txt) does not exist");
                }
                if (fi.Exists && fi.Length > 1)
                    return System.IO.File.ReadAllText(fi.FullName, System.Text.Encoding.Default);
                else
                {
                    PUB.log.AddAT("No ZPL file found, using ZPL code from settings");
                    return string.Empty;
                }

            }
        }

        public SATOPrinter()
		{
			//this.baseZPL = Properties.Settings.Default.ZPL7;
			this.Terminal = eTerminal.CR;
			this.ReceiveData += SATOPrinter_ReceiveData;
			this.ReceiveData_Raw += SATOPrinter_ReceiveData_Raw;
		}

		private void SATOPrinter_ReceiveData_Raw(object sender, ReceiveDataEventArgs e)
		{
			PUB.log.Add($"SATO Recv(RAW) : " + e.StrValue);
		}

		private void SATOPrinter_ReceiveData(object sender, ReceiveDataEventArgs e)
		{
			PUB.log.Add($"SATO Recv : " + e.StrValue);
		}

		public Boolean TestPrint(Boolean drawbox, string manu = "", string mfgdate = "")
		{
			var dtstr = DateTime.Now.ToShortDateString();
			var printcode = "103077807;Z577603504;105-35282-1105;15000;RC00004A219001W;20210612";
			var reel = new Class.Reel(printcode);
			//reel.id = "RLID" + DateTime.Now.ToString("MMHHmmssfff");
			reel.SID = "103000000";
			reel.PartNo = "PARTNO".PadRight(20, '0'); //20자리

			if (mfgdate == "") reel.mfg = dtstr;
			else reel.mfg = mfgdate;

			reel.venderLot = "LOT000000000";
			if (manu == "") reel.venderName = "ATK4EET1";
			else reel.venderName = manu;

			reel.qty = 15000;
			var rlt = Print(reel, true, drawbox);
			var fi = new System.IO.FileInfo(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "temp_zpl.txt"));
			System.IO.File.WriteAllText(fi.FullName, LastPrintZPL, System.Text.Encoding.Default);
			return rlt;
		}


		public Boolean Print(Class.Reel reel, Boolean display1drid, Boolean drawOUtBox)
		{
			string prtData;
			prtData = makeZPL_210908(reel, drawOUtBox, out qrData);

			return Print(prtData);
		}

		public bool Print(string _zpl)
		{
			this.LastPrintZPL = _zpl;
			if (this.IsOpen() == false) return false;

			try
			{
				this.Write(_zpl);
				return true;
			}
			catch (Exception ex)
			{
				return false;
			}
		}

		public string makeZPL_210908(Class.Reel reel, Boolean drawbox, out string qrData)
		{
			string m_strSend = string.Empty;
			qrData = string.Format("{0};{1};{2};{3};{4};{5};{6}", reel.SID, reel.venderLot, reel.venderName, reel.qty, reel.id, reel.mfg, reel.PartNo);

			var reeid = reel.id;
			if (reeid.Length > 20)
				reeid = "..." + reeid.Substring(reeid.Length - 20);

			m_strSend = this.baseZPL;
			m_strSend = m_strSend.Replace("{qrData}", qrData);
			m_strSend = m_strSend.Replace("{sid}", reel.SID);
			m_strSend = m_strSend.Replace("{lot}", reel.venderLot);
			m_strSend = m_strSend.Replace("{partnum}", reel.PartNo);
			m_strSend = m_strSend.Replace("{rid}", reeid);
			m_strSend = m_strSend.Replace("{qty}", reel.qty.ToString());
			m_strSend = m_strSend.Replace("{mfg}", reel.mfg);
			m_strSend = m_strSend.Replace("{supply}", reel.venderName);

			//줄바꿈제거
			m_strSend = m_strSend.Replace("\r", "").Replace("\n", "");
			return m_strSend;
		}
	}
}