using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Project.Class
{
    public class CAmkorSTDBarcode
    {
        public string Message { get; private set; }
        public Boolean isValid = false;
        public string SID { get; private set; }
        public string VLOT { get; private set; }
        public string RID { get; private set; }
        public string MFGDate { get; set; }
        public string SUPPLY { get; set; }
        public int QTY { get; set; }
        public string RAW { get; set; }
        public Boolean DateError { get; set; }
        public CAmkorSTDBarcode()
        {
            isValid = false;
        }
        public CAmkorSTDBarcode(string raw)
        {
            SetBarcode(raw);
        }
        public void SetBarcodeDemo()
        {
            SetBarcode("101410653;AG64B3W;SAMSUNG;20000;AG64B3W0031;19000101;");
        }
        /// 
        /// Returns barcode value with current property values.
        /// To check the original barcode value, check the RAW property
        /// 
        /// 
        public string GetBarcode()
        {
            return string.Format("{0};{1};{2};{3};{4};{5};",
                SID, VLOT, SUPPLY, QTY, RID, MFGDate);
        }
        public void SetBarcode(string raw)
        {
            isValid = false;
            this.Message = string.Empty;
            this.RAW = raw;
            var buf = raw.Split(';');
            if (buf.Length < 5)
            {
                isValid = false;
                Message = "buffer len error : " + raw;
                return;
            }
            decimal vSID = 0;
            double vQty = 0;
            if (decimal.TryParse(buf[0], out vSID) == false)
            {
                Message = "SID value is not a number. Amkor STD barcode consists of numbers";
                return;
            }
            this.SID = vSID.ToString();
            this.VLOT = buf[1];
            this.SUPPLY = buf[2];
            if (double.TryParse(buf[3], out vQty) == false) return;
            this.QTY = (int)vQty;
            this.RID = buf[4];
            //DateTime vMFGDate;
            MFGDate = buf[5].Trim();
            DateError = false;
            //buf[5] = buf[5].Replace("-", ""); //날짜표식제거
            //if (this.SUPPLY.ToUpper() == "SAMSUNG" && buf[5].Length == 4)   //삼성은 년도와 주차로 입력한다   210202
            //{
            //    var y = "20" + buf[5].Substring(0, 2);
            //    MFGDate = new DateTime(int.Parse(y), 1, 1).ToString("yyyyMMdd");   //주차계산무시한다
            //    DateError = true;
            //}
            //else if (this.SUPPLY.ToUpper() == "WT" && buf[5].Length == 4)   //삼성은 년도와 주차로 입력한다   210202
            //{
            //    var y = "20" + buf[5].Substring(0, 2);
            //    MFGDate = new DateTime(int.Parse(y), 1, 1).ToString("yyyyMMdd");   //주차계산무시한다
            //    DateError = true;
            //}
            //else if (buf[5].Length == 8)    //일반적인날짜데이터이며 YYYYMMDD 형태이다
            //{
            //    buf[5] = buf[5].Substring(0, 4) + "-" + buf[5].Substring(4, 2) + "-" + buf[5].Substring(6, 2);
            //    if (DateTime.TryParse(buf[5], out vMFGDate) == false) return;
            //    MFGDate = vMFGDate.ToString("yyyyMMdd");
            //    DateError = false;
            //}
            ////else if (buf[5].Equals("N/A"))    //날짜값에 NA
            ////{
            ////    MFGDate = "N/A";
            ////    DateError = false;
            ////}
            ////else if (buf[5].Equals("NA"))    //날짜값에 NA
            ////{
            ////    MFGDate = "NA";
            ////    DateError = false;
            ////}
            //else
            //{
            //    MFGDate = buf[5].Trim();
            //    Message = "Date value Length Error : " + buf[5];
            //    DateError = false;
            //    //return;
            //}
            isValid = true;
        }
    }
}