using AppData;
using Newtonsoft.Json;
using NKC_SDK;
using SteamWare;
using System;
using System.Drawing.Imaging;

namespace NKC_WF.WebUserControls
{
    public partial class cmp_unloadSmart : BaseUserControl
    {
        #region Protected Fields

        protected string redMachUnloadTable = "NKC:SERV:MACH_UNLOAD:TABLES";

        #endregion Protected Fields

        #region Protected Properties

        protected DS_App.BinsDataTable currBinTab
        {
            get
            {
                DS_App.BinsDataTable answ = null;
                string redKey = $"{redMachUnloadTable}:currBinTab:{itemIdSelected}";
                string rawVal = memLayer.ML.getRSV(redKey);
                // cerco su redis...
                if (string.IsNullOrEmpty(rawVal))
                {
                    // se non trovo --> DB
                    answ = DLMan.taBN.getByItemID(itemIdSelected);
                    string rawData = JsonConvert.SerializeObject(answ);
                    // salvo in redis... TTL 2 sec
                    memLayer.ML.setRSV(redKey, rawData, 2);
                }
                else
                {
                    answ = JsonConvert.DeserializeObject<DS_App.BinsDataTable>(rawVal);
                }
                return answ;
            }
        }

        protected DS_App.CartsDataTable currCartTab
        {
            get
            {
                DS_App.CartsDataTable answ = null;
                string redKey = $"{redMachUnloadTable}:currCartTab:{itemIdSelected}";
                string rawVal = memLayer.ML.getRSV(redKey);
                // cerco su redis...
                if (string.IsNullOrEmpty(rawVal))
                {
                    // se non trovo --> DB
                    answ = DLMan.taCR.getByItemID(itemIdSelected);
                    string rawData = JsonConvert.SerializeObject(answ);
                    //salvo in redis... TTL 2 sec
                    memLayer.ML.setRSV(redKey, rawData, 2);
                }
                else
                {
                    answ = JsonConvert.DeserializeObject<DS_App.CartsDataTable>(rawVal);
                }
                return answ;
            }
        }

        /// <summary>
        /// ID univoco da IP
        /// </summary>
        protected string DeviceId
        {
            set
            {
                hfDeviceId.Value = value;
            }
            get
            {
                return hfDeviceId.Value;
            }
        }

        /// <summary>
        /// Comando barcode letto
        /// </summary>
        protected string lastCmd
        {
            get
            {
                return hfLastBCode.Value;
            }
            set
            {
                hfLastBCode.Value = value;
                lastValidCmd = value;
            }
        }

        /// <summary>
        /// ULTIMO Comando barcode VALIDO (!="") letto
        /// </summary>
        protected string lastValidCmd
        {
            get
            {
                return hfLastValidBCode.Value;
            }
            set
            {
                // solo se è !=""
                if (!string.IsNullOrEmpty(value))
                {
                    hfLastValidBCode.Value = value;
                }
            }
        }

        protected string secOp
        {
            get
            {
                return hfSecOp.Value;
            }
            set
            {
                hfSecOp.Value = value;
            }
        }

        /// <summary>
        /// Sheet selezionato...
        /// </summary>
        protected int SheetID
        {
            set
            {
                hfSheetID.Value = value.ToString();
            }
            get
            {
                int answ = 0;
                int.TryParse(hfSheetID.Value, out answ);
                return answ;
            }
        }

        protected bool showBin
        {
            set
            {
                hfShowBin.Value = value.ToString();
            }
            get
            {
                bool answ = false;
                bool.TryParse(hfShowBin.Value, out answ);
                return answ;
            }
        }

        protected bool showCart
        {
            set
            {
                hfShowCart.Value = value.ToString();
            }
            get
            {
                bool answ = false;
                bool.TryParse(hfShowCart.Value, out answ);
                return answ;
            }
        }

        protected bool showSecOp
        {
            set
            {
                hfShowSecOp.Value = value.ToString();
            }
            get
            {
                bool answ = false;
                bool.TryParse(hfShowSecOp.Value, out answ);
                return answ;
            }
        }

        #endregion Protected Properties

        #region Public Properties

        /// <summary>
        /// Batch corrente...
        /// </summary>
        public int BatchId
        {
            set
            {
                hfBatchID.Value = value.ToString();
            }
            get
            {
                int answ = 0;
                int.TryParse(hfBatchID.Value, out answ);
                return answ;
            }
        }

        public int itemIdSelected
        {
            get
            {
                int answ = 0;
                int.TryParse(hfItemID.Value, out answ);
                return answ;
            }
            set
            {
                hfItemID.Value = value.ToString();
                bool showBtn = value != 0;
                // fix visibilità
                lbtCancel.Visible = showBtn;
                lbtScrapped.Visible = showBtn;
                lbtParkArea.Visible = showBtn;
                lbtResetSel.Visible = showBtn;
            }
        }

        /// <summary>
        /// Macchina letta
        /// </summary>
        public string MachineSel
        {
            get
            {
                return cmp_MachSelSmart.MachineSel;
            }
            set
            {
                PlaceCod = value;
                cmp_MachSelSmart.MachineSel = value;
            }
        }

        #endregion Public Properties

        #region Private Methods

        private void Cmp_barcode_eh_doRefresh(object sender, EventArgs e)
        {
            bool doRaiseEv = false;
            // processo evento..
            lastCmd = cmp_barcode.inputAcquired.ToUpper();
            doRaiseEv = processLastCmd(doRaiseEv);
            // reset comando
            cmp_barcode.inputAcquired = "";
            ComLib.man.resetSheetUnload(SheetID);
            // aggiorno...
            doUpdate();
            if (doRaiseEv)
            {
                resetMessageDivs();
            }
        }

        private void Cmp_barcode_eh_doReset(object sender, EventArgs e)
        {
            resetMessages();
            resetButtons();
        }

        private void doUpdate()
        {
            // reset grafico
            icnCart.Attributes.Remove("class");
            icnCart.Attributes.Add("class", "btn btn-sm btn-block btn-outline-secondary");
            icnBin.Attributes.Remove("class");
            icnBin.Attributes.Add("class", "btn btn-sm btn-block btn-outline-secondary");
            icnSecOp.Attributes.Remove("class");
            icnSecOp.Attributes.Add("class", "btn btn-sm btn-block btn-outline-secondary");

            // in base al secondo mostro uno o altro...
            if (showCart)
            {
                icnCart.Attributes.Remove("class");
                icnCart.Attributes.Add("class", "btn btn-sm btn-block btn-success");
                // fix visualizzazione di inviato al cart
                // controllo SE HO cart
                if (currCartTab.Count >= 1)
                {
                    var cartRow = currCartTab[0];
                    // imposto OUT
                    lblDestination.Text = $"SEND TO CART <b>C{cartRow.CartIndex}</b> ({cartRow.CartDtmx})";
                }
                else
                {
                    lblDestination.Text = $"ERROR SEARCHING CART: {currCartTab.Count} found!";
                }
                lblDestination.CssClass = "text-success";
            }
            else if (showBin)
            {
                icnBin.Attributes.Remove("class");
                icnBin.Attributes.Add("class", "btn btn-sm btn-block btn-primary");
                if (currBinTab.Count == 1)
                {
                    var binRow = currBinTab[0];
                    lblDestination.Text = $"SEND TO BIN <b>B{binRow.BinIndex}</b> ({binRow.BinDtmx})";
                }
                else
                {
                    lblDestination.Text = $"ERROR SEARCHING BIN: {currBinTab.Count} found!";
                }
                lblDestination.CssClass = "text-primary";
            }
            if (showSecOp)
            {
                icnSecOp.Attributes.Remove("class");
                icnSecOp.Attributes.Add("class", "btn btn-sm btn-block btn-info");
                lblLastBCode.Text = $"DO SEC OP: {secOp}";
                lblLastBCode.CssClass = "text-info";
            }
        }

        /// <summary>
        /// Processo il DataMatrix letto
        /// </summary>
        /// <param name="tipoCod"></param>
        /// <param name="rawData"></param>
        /// <param name="codeInt"></param>
        private void processItemSuggestion(codeType tipoCod, string rawData, int codeInt)
        {
            displMessage("", false);
            DS_App.ItemListDataTable tabItem = null;
            DataLayer DLMan = new DataLayer();
            string message = "";
            // processo suggerimenti x ITEM / cart / bin dato suo RawData (Dtmx) e Cod univoco (intero)
            switch (tipoCod)
            {
                case codeType.Item:
                    tabItem = DLMan.taIL.getBySearch(codeInt, rawData, 0, 999);
                    if (tabItem.Count == 1)
                    {
                        showItemDetail(true, tabItem[0], false);
                    }
                    else if (tabItem.Count == 0)
                    {
                        showItemDetail(false, null, true);
                    }
                    break;

                case codeType.ItemGeneric:
                    // PRIMA CERCA SE ne ho in stato 1..3
                    tabItem = DLMan.taIL.getBySearch(0, rawData, 1, 3);
                    if (tabItem.Count == 0)
                    {
                        // se NON ne trovo cerci FINO a stato 5...
                        tabItem = DLMan.taIL.getBySearch(0, rawData, 1, 5);
                    }
                    if (tabItem.Count == 1)
                    {
                        showItemDetail(true, tabItem[0], false);
                    }
                    else if (tabItem.Count == 0)
                    {
                        showItemDetail(false, null, true);
                    }
                    break;

                case codeType.Cart:
                    // verifico NON SIA richiesto PAINT
                    if (showBin)
                    {
                        displError(traduci("WrongActionBinReq"), true);
                    }
                    else
                    {
                        if (currCartTab.Count >= 1)
                        {
                            // controllo se sia quello corretto
                            if (rawData != currCartTab[0].CartDtmx)
                            {
                                displError(traduci("WrongActionCart"), true);
                            }
                            else
                            {
                                // dichiaro che è depositato
                                DLMan.taIL.updateStatus(itemIdSelected, 3, PlaceCod);
                                int cartIndex = 0;
                                try
                                {
                                    cartIndex = currCartTab[0].CartIndex;
                                }
                                catch
                                { }
                                message = $"{traduci("Part")} {itemIdSelected} {traduci("PutInCart")} C{cartIndex} {rawData}";
                                lblDestination.Text = message;
                                lgInfo($"cmp_unloadSmart | Status --> 3 | {message} | PlaceCod: {PlaceCod}");
                                resetSelection(false);
                                resetButtons();
                            }
                        }
                    }
                    break;

                case codeType.Bin:
                    // verifico NON SIA richiesto PAINT
                    if (showCart)
                    {
                        displError(traduci("WrongActionCartReq"), true);
                    }
                    else
                    {
                        if (currBinTab.Count == 1)
                        {
                            // controllo se sia quello corretto
                            if (rawData != currBinTab[0].BinDtmx)
                            {
                                displError(traduci("WrongActionBin"), true);
                            }
                            else
                            {
                                // dichiaro che è depositato
                                DLMan.taIL.updateStatus(itemIdSelected, 4, PlaceCod);
                                message = $"{traduci("Part")} {itemIdSelected} {traduci("PutInBin")} {rawData}";
                                lblDestination.Text = message;
                                lgInfo($"cmp_unloadSmart | Status --> 4 | {message} | PlaceCod: {PlaceCod}");
                                resetSelection(false);
                                resetButtons();
                            }
                        }
                    }
                    break;

                case codeType.SecScreen:
                    // se item già letto
                    if (divItemDet.Visible)
                    {
                        int defaultSecScreenShowTimeout = memLayer.ML.CRI("defaultSecScreenShowTimeout");
                        ComLib.setSecScreenRequest(rawData, lblItemDtmx.Text, 60 * defaultSecScreenShowTimeout);
                        message = $"{traduci("PartToSecScreen")} | {lblItemDtmx.Text} ! {defaultSecScreenShowTimeout} min";
                        displMessage(message, false);
                        lgDebug($"cmp_unloadSmart | SecScreen | {message} | PlaceCod: {PlaceCod}");
                    }
                    else
                    {
                        // chiedo di leggere un ITEM prima...
                        displError("Please read ITEM before Screen and Retry", true);
                    }
                    break;

                default:
                    break;
            }
        }

        private bool processLastCmd(bool doRaiseEv)
        {
            if (string.IsNullOrEmpty(lastCmd)) doRaiseEv = true;
            // processiamo barcode letto
            decodedData decoData = DLMan.decodeBcode(lastCmd);
            switch (decoData.codeType)
            {
                case codeType.UNK:
                    cmp_barcode.showOutput(cssClass.danger, $"Unknown Data: {decoData.rawData} --> no action");
                    resetSelection(false);
                    doRaiseEv = true;
                    break;

                case codeType.Item:
                    if (string.IsNullOrEmpty(MachineSel))
                    {
                        cmp_barcode.showOutput(cssClass.danger, $"{traduci("MissingMachineSel")}: {decoData.rawData} --> {traduci("NoValiAction")}");
                    }
                    else
                    {
                        tryPickup(decoData.rawData);
                        cmp_barcode.showOutput(cssClass.success, $"Valid IT Code: {decoData.rawData}");
                        processItemSuggestion(decoData.codeType, decoData.rawData, decoData.codeInt);
                    }
                    break;

                case codeType.ItemGeneric:
                    if (string.IsNullOrEmpty(MachineSel))
                    {
                        cmp_barcode.showOutput(cssClass.danger, $"{traduci("MissingMachineSel")}: {decoData.rawData} --> {traduci("NoValiAction")}");
                    }
                    else
                    {
                        tryPickup(decoData.rawData);
                        cmp_barcode.showOutput(cssClass.success, $"Valid IG Code: {decoData.rawData}");
                        processItemSuggestion(decoData.codeType, decoData.rawData, decoData.codeInt);
                    }
                    break;

                case codeType.Material:
                    cmp_barcode.showOutput(cssClass.warning, $"Material - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;

                case codeType.Sheet:
                    cmp_barcode.showOutput(cssClass.warning, $"Sheet - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;

                case codeType.Stack:
                    cmp_barcode.showOutput(cssClass.warning, $"BUNK - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;

                case codeType.OtherItem:
                    cmp_barcode.showOutput(cssClass.warning, $"Generic PART - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;

                case codeType.Batch:
                    cmp_barcode.showOutput(cssClass.warning, $"Batch - ignored: {decoData.description}");
                    doRaiseEv = true;
                    break;

                case codeType.Cart:
                    if (string.IsNullOrEmpty(MachineSel))
                    {
                        cmp_barcode.showOutput(cssClass.danger, $"{traduci("MissingMachineSel")}: {decoData.rawData} --> {traduci("NoValiAction")}");
                    }
                    else
                    {
                        cmp_barcode.showOutput(cssClass.success, $"Valid CR Code: {decoData.description}");
                        processItemSuggestion(decoData.codeType, decoData.rawData, decoData.codeInt);
                    }
                    break;

                case codeType.Bin:
                    if (string.IsNullOrEmpty(MachineSel))
                    {
                        cmp_barcode.showOutput(cssClass.danger, $"{traduci("MissingMachineSel")}: {decoData.rawData} --> {traduci("NoValiAction")}");
                    }
                    else
                    {
                        cmp_barcode.showOutput(cssClass.success, $"Valid BN Code: {decoData.description}");
                        processItemSuggestion(decoData.codeType, decoData.rawData, decoData.codeInt);
                    }
                    break;

                case codeType.SecScreen:
                    if (string.IsNullOrEmpty(MachineSel))
                    {
                        cmp_barcode.showOutput(cssClass.danger, $"{traduci("MissingMachineSel")}: {decoData.rawData} --> {traduci("NoValiAction")}");
                    }
                    else
                    {
                        cmp_barcode.showOutput(cssClass.success, $"Valid Screen Code: {decoData.description}");
                        processItemSuggestion(decoData.codeType, decoData.rawData, decoData.codeInt);
                    }
                    break;

                case codeType.MachSelection:
                    MachineSel = decoData.code;
                    cmp_barcode.showOutput(cssClass.success, $"{ traduci("MachineSel")}: {decoData.code}");
                    break;

                default:
                    cmp_barcode.showOutput(cssClass.danger, $"Unknown Data: {decoData.rawData} --> no action");
                    resetSelection(false);
                    break;
            }

            return doRaiseEv;
        }

        /// <summary>
        /// resetto buttons azioni
        /// </summary>
        private void resetButtons()
        {
            lbtCancel.Visible = false;
            lbtScrapped.Visible = false;
            lbtParkArea.Visible = false;
            lbtResetSel.Visible = false;
        }

        /// <summary>
        /// Resetto testi
        /// </summary>
        private void resetMessages()
        {
            lblDestination.CssClass = "text-secondary";
            lblDestination.Text = "";
            lblLastBCode.Text = "";
        }

        /// <summary>
        /// Reset booleani visualizzazione
        /// </summary>
        private void resetShowData()
        {
            showCart = false;
            showBin = false;
            showSecOp = false;
        }

        /// <summary>
        /// Mostra o nasconde dettaglio su ITEM letto
        /// </summary>
        /// <param name="v"></param>
        /// <param name="itemRow"></param>
        private void showItemDetail(bool showItem, DS_App.ItemListRow itemRow, bool showError)
        {
            resetShowData();
            divItemDet.Visible = showItem;
            divItemError.Visible = showError;
            if (showError)
            {
                displError("Item not found", true);
            }
            if (showItem)
            {
                lblItemCode.Text = itemRow.ItemExtCode;
                lblItemDesc.Text = itemRow.ItemDesc;
                lblItemDtmx.Text = itemRow.ItemDtmx;
                itemIdSelected = itemRow.ItemID;
                // CONTROLLO SIA in stato 1 --> worked, 2--> selected, 100--> parked...
                switch (itemRow.StatusID)
                {
                    case 0:
                        displError("ERROR: Item not ready", false);
                        break;

                    case 1:
                    case 2:
                    case 100:
                        // salvo che è in STATO 2 (picked up)
                        DataLayer currDataLayer = new DataLayer();
                        currDataLayer.taIL.updateStatus(itemRow.ItemID, 2, PlaceCod);
                        // verifico cosa devo mostrare come PROX passaggio...
                        showCart = string.IsNullOrEmpty(itemRow.ProcessesReq);
                        showBin = (itemRow.ProcessesReq.Contains("PaintFlag"));
                        secOp = itemRow.PostProcList;
                        showSecOp = (!string.IsNullOrEmpty(secOp));
                        doUpdate();
                        break;

                    case 3:
                        displError("Item already on CART!", false);
                        break;

                    case 4:
                        // fino a concorrenza qta richiesta sposto, POI do errori
                        displError("ALL Item already scanned on BIN!", false);
                        break;

                    case 5:
                        displError("Item already at KIT STATION!", false);
                        break;

                    case 990:
                        displError("Item declared SCRAP!", false);
                        break;

                    case 991:
                        displError("Item declared SCRAP with CNC program created!", false);
                        break;

                    default:
                        displError("ERROR: Item status UNKNOWN!", false);
                        break;
                }
            }
        }

        #endregion Private Methods

        #region Protected Methods

        /// <summary>
        /// Mostra errore ed effettua reset vari...
        /// </summary>
        /// <param name="errorMessage"></param>
        /// <param name="resetStatus"></param>
        protected void displError(string errorMessage, bool resetStatus)
        {
            lblErrorMsg.Text = errorMessage;
            divItemError.Visible = true;
            resetSelection(resetStatus);
        }

        /// <summary>
        /// Mostra INFO ed effettua reset vari...
        /// </summary>
        /// <param name="currMessage"></param>
        /// <param name="resetStatus"></param>
        protected void displMessage(string currMessage, bool resetStatus)
        {
            lblInfoMessage.Text = currMessage;
            divInfoMessage.Visible = true;
            resetSelection(resetStatus);
        }

        protected void lbtCancel_Click(object sender, EventArgs e)
        {
            // resetto item selezionato...
            try
            {
                DLMan.taIL.updateStatus(itemIdSelected, 1, PlaceCod);
                lgInfo($"cmp_unloadSmart | cancelSelect | Status --> 1 | itemIdSelected: {itemIdSelected} | PlaceCod: {PlaceCod}");
            }
            catch
            { }
            resetSelection(true);
            showItemDetail(false, null, false);
        }

        /// <summary>
        /// imposto a parcheggiato
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtParkArea_Click(object sender, EventArgs e)
        {
            // resetto item selezionato...
            DLMan.taIL.updateStatus(itemIdSelected, 100, PlaceCod);
            lgInfo($"cmp_unloadSmart | parkArea | Status --> 100 | itemIdSelected: {itemIdSelected} | PlaceCod: {PlaceCod}");
            resetSelection(true);
            showItemDetail(false, null, false);
            lblLastBCode.Text = traduci("PartParked");
            lblLastBCode.CssClass = "text-secondary";
        }

        protected void lbtResetSel_Click(object sender, EventArgs e)
        {
            var tabSheet = DLMan.taSHL.getByItemID(itemIdSelected);
            if (tabSheet.Count == 1)
            {
                resetShowData();
                // resetto sheet selezionato...
                DLMan.taIL.updateSheetStatusPU(tabSheet[0].SheetID, PlaceCod);
                lgInfo($"cmp_unloadSmart | updateSheetStatusPU | SheetID: {tabSheet[0].SheetID} | PlaceCod: {PlaceCod}");
                resetSelection(true);
                showItemDetail(false, null, false);
            }
        }

        /// <summary>
        /// Imposto come danneggiato/scrapped da rilavorare offline
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbtScrapped_Click(object sender, EventArgs e)
        {
            // resetto item selezionato...
            DLMan.taIL.updateStatus(itemIdSelected, 990, PlaceCod);
            lgInfo($"cmp_unloadSmart | SCRAP | Status --> 990 | itemIdSelected: {itemIdSelected} | PlaceCod: {PlaceCod}");
            resetSelection(false);
            showItemDetail(false, null, false);
            lblLastBCode.Text = traduci("PartScrapped");
            lblLastBCode.CssClass = "text-danger";
            // riprocesso barcode...
            lastCmd = lastValidCmd;
            processLastCmd(false);
            resetButtons();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                resetShowData();
                resetMessages();
                resetButtons();
                updateCurrData();
            }
            cmp_barcode.eh_doRefresh += Cmp_barcode_eh_doRefresh;
            cmp_barcode.eh_doReset += Cmp_barcode_eh_doReset;
            // resetto
            lblLastBCode.Text = "------";
            lblDestination.Text = "--";
        }

        /// <summary>
        /// Resetta mesaggi ed errori
        /// </summary>
        protected void resetMessageDivs()
        {
            divInfoMessage.Visible = false;
            lblInfoMessage.Text = "";
            divItemError.Visible = false;
            lblErrorMsg.Text = "";
        }

        /// <summary>
        /// Reset selezione item + blocchi suggerimento + sel REDIS x pagina unload
        /// </summary>
        /// <param name="resetStatus"></param>
        protected void resetSelection(bool resetStatus)
        {
            if (resetStatus)
            {
                try
                {
                    // SE item esiste...
                    var tabItem = DLMan.taIL.getBySearch(itemIdSelected, itemIdSelected.ToString(), 0, 999);
                    if (tabItem.Count == 1)
                    {
                        // riposto item a status 1...
                        DLMan.taIL.updateStatus(itemIdSelected, 1, PlaceCod);
                        lgDebug($"cmp_unloadSmart | resetSelection | Status --> 1 | itemIdSelected: {itemIdSelected} | PlaceCod: {PlaceCod}");
                    }
                }
                catch
                { }
                // elimino item sel...
                itemIdSelected = 0;
            }
            resetShowData();
            ComLib.resetItemPickup(SheetID, DeviceId);
            lgDebug($"ComLib.resetItemPickup | SheetID {SheetID} | DeviceId: {DeviceId}");
        }

        /// <summary>
        /// Prova a fare pickup (SOLO SE il mio item è nel foglio corrente...)
        /// </summary>
        /// <param name="itemDtmx"></param>
        protected void tryPickup(string itemDtmx)
        {
            // salvo in item sel...
            updateCurrData();
            bool fatto = ComLib.saveItemPickup(SheetID, DeviceId, itemDtmx);
            lgInfo($"cmp_unloadSmart | tryPickup | item: {itemDtmx} | SheetID: {SheetID} | DeviceId: {DeviceId} | done: {fatto}");
        }

        /// <summary>
        /// Aggiorna dati correnti (IP, batch, sheet...)
        /// </summary>
        protected void updateCurrData()
        {
            // FORSE 5/5?!?
            DataLayer DLMan = new DataLayer();
            var sheetList = DLMan.taSHL.getByMLStatus(BatchId, 3, 5);
            if (sheetList.Count > 0)
            {
                SheetID = sheetList[0].SheetID;
            }
            DeviceId = ComLib.GetIPAddress().Replace(".", "0").Replace(":", "0");
        }

        #endregion Protected Methods
    }
}