using AppData;
using NKC_SDK;
using System;
using System.Collections.Generic;
using System.Web.UI;

namespace NKC_WF
{
    public partial class Utility : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtCodTakt.Text = $"{DateTime.Now:yyyyMMdd}.1";
                txtNumStaks.Text = "3";
                txtNumPanels.Text = "5";
            }
        }

        protected int numStackReq
        {
            get
            {
                int answ = 0;
                Int32.TryParse(txtNumStaks.Text, out answ);
                return answ;
            }
        }
        protected int numPanels
        {
            get
            {
                int answ = 0;
                Int32.TryParse(txtNumPanels.Text, out answ);
                return answ;
            }
        }


        protected void lbtMakeSim_Click(object sender, EventArgs e)
        {
            // creazione degli stack e dei pannelli...
            List<ProdBunk> stackList = new List<ProdBunk>();
            List<ProdSheet> panelList;
            ProdBunk currStack;
            ProdSheet currPanel;
            MaterialData currMaterial;
            WorkData currPrintData;
            WorkData currMachiningData;
            WorkData currUnloadData;
            for (int i = 1; i <= numStackReq; i++)
            {
                // devo creare fino a maxNumPanels
                panelList = new List<ProdSheet>();
                for (int j = 1; j <= numPanels; j++)
                {
                    currMaterial = new MaterialData()
                    {
                        MaterialId = 1,
                        MaterialPN = "6120",
                        MaterialDescription = "BOARD, WAFER, 1/4 X 48 X 96"
                    };
                    currPrintData = new WorkData()
                    {
                        ProgramPath = @"c:\temp\PrintProgram.cnc"
                    };
                    currMachiningData = new WorkData()
                    {
                        ProgramPath = @"c:\temp\MachiningProgram.cnc"
                    };
                    currUnloadData = new WorkData()
                    {
                        ProgramPath = @""
                    };
                    currPanel = new ProdSheet()
                    {
                        SheetId = j,
                        Status = PStatus.Programmed,
                        Material = currMaterial,
                        Printing = currPrintData,
                        Machining = currMachiningData,
                        Unloading = currUnloadData
                    };
                    panelList.Add(currPanel);
                }

                currStack = new ProdBunk()
                {
                    DataMatrix = $"DM{txtCodTakt.Text}.{i}",
                    BunkId = i,
                    Status = CStatus.Programmed,
                    SheetList = panelList
                };
                stackList.Add(currStack);
            }
            // creazione oggetto takt
            ComLib.Takt currTakt = new ComLib.Takt()
            {
                TaktId = txtCodTakt.Text,
                Status = CStatus.Programmed,
                StackList = stackList
            };
            // serializzo 
            //string jsonData = JsonConvert.SerializeObject(currTakt);
            string jsonData = ComLib.serializeTakt(currTakt);

            // scrivo su label
            lblOut.Text = jsonData;
            // salvo in redis / DB
            ComLib.saveTakt("SERV", currTakt);
        }
    }
}