Imports Newtonsoft.Json
Imports SteamWare
Imports System
Imports System.Collections.Generic

Namespace AppData
    Public Class ComLib
        Public Shared Function traduci(ByVal lemma As String) As String
            Return user_std.UtSn.Traduci(lemma)
        End Function

        Protected Shared redOutPath As String = "NKC:SERV:BREQ"
        Protected Shared redMsgCount As String = "NKC:SERV:BREQ:MCount"
        Protected Shared redMsgList As String = "NKC:SERV:BREQ:MList"
        Protected Shared redMLCurrStack As String = "NKC:SERV:TAKT:CurrStack"

        Public Enum CStatus
            Programmed = 0
            Running
            Done
        End Enum

        Public Enum BatchStatus
            Imported = 0
            EstimationRequested
            EstimationDone
            NestRequested
            NestDone
            Approved
            Discarded
        End Enum

        Public Enum BatchPosition
            NotStarted = 0
            StackStarted
            StackDone
            Current
            Completed
        End Enum

        Public Shared Function PositionStatusDescr(ByVal value As Object) As String
            Dim answ As String = ""

            Try
                Dim pStatus As ComLib.BatchPosition = CType([Enum].Parse(GetType(ComLib.BatchPosition), value.ToString()), ComLib.BatchPosition)

                Select Case pStatus
                    Case BatchPosition.NotStarted
                        answ = traduci("NotStarted")
                    Case BatchPosition.StackStarted
                        answ = traduci("Stacking")
                    Case BatchPosition.StackDone
                        answ = traduci("StackDone")
                    Case Else
                End Select

            Catch
            End Try

            Return answ
        End Function

        Public Shared Function BatchStatusDescr(ByVal value As Object) As String
            Dim answ As String = ""

            Try
                Dim bStatus As ComLib.BatchStatus = CType([Enum].Parse(GetType(ComLib.BatchStatus), value.ToString()), ComLib.BatchStatus)

                Select Case bStatus
                    Case BatchStatus.Imported
                        answ = "Imported"
                    Case BatchStatus.EstimationRequested
                        answ = "Estimation Requested"
                    Case BatchStatus.EstimationDone
                        answ = "Estimation Completed"
                    Case BatchStatus.NestRequested
                        answ = "Nesting Requested"
                    Case BatchStatus.NestDone
                        answ = "Nesting Completed"
                    Case BatchStatus.Approved
                        answ = "Nesting Approved"
                    Case BatchStatus.Discarded
                        answ = "Nesting Discarded"
                    Case Else
                End Select

            Catch
            End Try

            Return answ
        End Function

        Public Enum PStatus
            Programmed = 0
            Present
            Printing
            Printed
            Machining
            Machined
            Out
        End Enum

        Public Class MaterialData
            Public Property MaterialId As Integer
            Public Property MaterialPN As String
            Public Property MaterialDescription As String
        End Class

        Public Class WorkData
            Public Property ProgramPath As String
            Public Property DtStart As DateTime
            Public Property DtEnd As DateTime

            Public ReadOnly Property WorkTimeMin As Double
                Get
                    Dim answ As Double = 0

                    If DtStart IsNot Nothing AndAlso DtEnd IsNot Nothing Then

                        Try
                            answ = DtEnd.Subtract(DtStart).TotalMinutes
                        Catch
                        End Try
                    End If

                    Return answ
                End Get
            End Property
        End Class

        Public Class Panel
            Public Property PanelId As Integer
            Public Property Material As MaterialData
            Public Property Status As PStatus
            Public Property Printing As WorkData
            Public Property Machining As WorkData
            Public Property Unloading As WorkData
        End Class

        Public Class WStack
            Public Property StackId As Integer
            Public Property Status As CStatus
            Public Property DataMatrix As String
            Public Property DtStart As DateTime
            Public Property DtEnd As DateTime
            Public Property PanelsList As List(Of Panel)

            Public ReadOnly Property NumPanels As Integer
                Get
                    Dim answ As Integer = 0

                    Try
                        answ = PanelsList.Count
                    Catch
                    End Try

                    Return answ
                End Get
            End Property
        End Class

        Public Class Takt
            Public Property TaktId As String
            Public Property Status As CStatus
            Public Property StackList As List(Of WStack)

            Public ReadOnly Property NumStack As Integer
                Get
                    Dim answ As Integer = 0

                    Try
                        answ = StackList.Count
                    Catch
                    End Try

                    Return answ
                End Get
            End Property
        End Class

        Public Class Parte
            Public Property PartId As Integer
            Public Property DataMatrix As String
            Public Property ExtCode As String
            Public Property Description As String
            Public Property MatId As Integer
            Public Property PostProc As String
            Public Property ProcessReq As String
            Public Property CadFilePath As String
            Public Property Qty As Integer
        End Class

        Public Class Ordine
            Public Property OrdCod As String
            Public Property DestPlant As String
            Public Property DataMatrix As String
            Public Property ExtCode As String
            Public Property Model As String
            Public Property Qty As Integer
            Public Property Items As List(Of Parte)
        End Class

        Public Class Batch
            Public Property BatchId As Integer
            Public Property maxTime As Integer
            Public Property procType As Integer
            Public Property Orders As List(Of Ordine)
        End Class

        Public Shared Function sendMaterials() As Boolean
            Dim answ As Boolean = False
            Dim table = DataLayer.man.taMat.GetData()
            Dim redVal As String = JsonConvert.SerializeObject(table)
            Dim redKey As String = $"NKC:SERV:CONF:MATERIALS"
            memLayer.ML.setRSV(redKey, redVal)
            Return answ
        End Function

        Public Shared Function getNextEnv(ByVal BatchID As Integer, ByVal note As String) As String
            Dim nextIndex As Long = memLayer.ML.setRCntI(redMsgCount)
            Dim answ As String = $"Z{nextIndex}"
            Dim lista As Dictionary(Of String, String) = New Dictionary(Of String, String)()
            lista.Add(answ, $"{BatchID}|{note}")
            memLayer.ML.redSaveHashDict(redMsgList, lista)
            Return answ
        End Function

        Public Shared Function sendBatchReq(ByVal BatchID As Integer, ByVal note As String) As Boolean
            Dim answ As Boolean = False
            Dim nextEnv As String = getNextEnv(BatchID, note)

            Try
                Dim batch = DataLayer.man.taBL.getByKey(BatchID)
                Dim mTime As Integer = 1
                Dim pType As Integer = 1

                If batch(0).STATUS < CInt(BatchStatus.EstimationDone) Then
                    mTime = memLayer.ML.cdvi("estimMaxTime")
                Else
                    mTime = memLayer.ML.cdvi("nestMaxTime")
                    pType = 2
                End If

                Dim tblOrd = DataLayer.man.taOL.getByBatch(BatchID)
                Dim redVal As String = JsonConvert.SerializeObject(tblOrd)
                Dim redKey As String = $"{redOutPath}:{nextEnv}:ORDERS"
                memLayer.ML.setRSV(redKey, redVal)
                Dim tblItm = DataLayer.man.taIL.getByBatch(BatchID)
                redVal = JsonConvert.SerializeObject(tblItm)
                redKey = $"{redOutPath}:{nextEnv}:ITEMS"
                memLayer.ML.setRSV(redKey, redVal)
                Dim currBatch = New Batch() With {
                    .BatchId = BatchID,
                    .maxTime = mTime,
                    .procType = pType
                }
                redVal = JsonConvert.SerializeObject(currBatch)
                redKey = $"{redOutPath}:{nextEnv}:DATA"
                memLayer.ML.setRSV(redKey, redVal)
                redKey = $"{redOutPath}:CURR"
                memLayer.ML.setRSV(redKey, nextEnv)
                answ = True
            Catch
            End Try

            Return answ
        End Function

        Public Shared Function serializeTakt(ByVal currData As Takt) As String
            Dim answ As String = JsonConvert.SerializeObject(currData)
            Return answ
        End Function

        Public Shared Function deserializeTakt(ByVal rawData As String) As Takt
            Dim answ As Takt = JsonConvert.DeserializeObject(Of Takt)(rawData)
            Return answ
        End Function

        Public Shared Function saveTakt(ByVal origin As String, ByVal currData As Takt) As Boolean
            Dim answ As Boolean = False

            Try
                Dim redKey As String = $"NKC:{origin.ToUpper()}:TAKT:{currData.TaktId}"
                Dim redVal As String = serializeTakt(currData)
                memLayer.ML.setRSV(redKey, redVal)
                answ = True
            Catch
            End Try

            Return answ
        End Function

        Public Shared Function readTakt(ByVal origin As String, ByVal TaktId As String) As Takt
            Dim answ As Takt = Nothing

            Try
                Dim redKey As String = $"NKC:{origin.ToUpper()}:TAKT:{TaktId}"
                Dim redVal As String = memLayer.ML.getRSV(redKey)
                answ = deserializeTakt(redVal)
            Catch
            End Try

            Return answ
        End Function

        Public Function prodGetNextTakt() As Takt
            Return Nothing
        End Function

        Public Shared Property taktMLCurrStack As String
            Get
                Return memLayer.ML.getRSV(redMLCurrStack)
            End Get
            Set(ByVal value As String)
                memLayer.ML.setRSV(redMLCurrStack, value)
            End Set
        End Property
    End Class
End Namespace
