Imports System.Net.NetworkInformation
Imports NKC_SDK


Public Class Form1

  ' caricamento del NEXT STACK da redis (come oggetto)
  ' PROD http : http://seriate.steamware.net:8083/NKC/
  ' DEV:  https : http://localhost:44388/
  Dim baseIp As String = "seriate.steamware.net"
  Dim baseUrl As String = "http://seriate.steamware.net:8083/NKC/"
  Dim codPost As String = "WRK001" ' nome macchina
  Dim currNKC As NKC = New NKC(baseIp, baseUrl, codPost)

  Dim currentSheets As SheetWorkList = Nothing
  Dim risultatoPing As PingReply = Nothing
  Dim srvAlive As Boolean = False
  Dim srvClock As String = ""

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnReset.Click

    ' resetto TUTTE le date e salvo il bunk!

    currentSheets.SheetList(cmbSheet.SelectedIndex).Printing.DtStart = Nothing
    currentSheets.SheetList(cmbSheet.SelectedIndex).Printing.DtEnd = Nothing
    currentSheets.SheetList(cmbSheet.SelectedIndex).Machining.DtStart = Nothing
    currentSheets.SheetList(cmbSheet.SelectedIndex).Machining.DtEnd = Nothing
    currentSheets.SheetList(cmbSheet.SelectedIndex).Unloading.DtStart = Nothing
    currentSheets.SheetList(cmbSheet.SelectedIndex).Unloading.DtEnd = Nothing

    ' salvo!
    currNKC.saveSheets(currentSheets)

    updateDataButtons()


    '' invio notifica che c'è una busta da processare
    'Dim redKey = $"PROVA:CURR"
    '' scrivo su REDIS
    'memLayer.ML.setRSV(redKey, "ciao " & DateTime.Now.ToString())
    ''ComLib.sendMaterials()
    'Label1.Text = memLayer.ML.getRSV(redKey)
    ''Dim x As New Takt
    ''x = readTakt("PROD", "20190821.1")
    ''x.StackList(0).PanelsList(0).Printing()
    ''Dim d = memLayer.ML.connRedis.Configure.
    ''Dim h = memLayer.ML.connRedis
  End Sub


  Private Sub cmbSheet_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSheet.SelectedIndexChanged
    ' selezione cambiata su elenco sheet (dal primo all'ultimo a runtime OBBLIGATORIO)
    updateDataButtons()

  End Sub

  Private Sub Button3_Click(sender As Object, e As EventArgs) Handles btnPrintStart.Click
    ' iniziato paint x sheet selezionato
    txtPrintStart.Text = DateTime.Now

  End Sub

  Private Sub Button4_Click(sender As Object, e As EventArgs) Handles btnPrintEnd.Click
    ' completato paint x sheet selezionato
    txtPrintEnd.Text = DateTime.Now

  End Sub


  Private Sub btnLoadBunk_Click(sender As Object, e As EventArgs) Handles btnLoadBunk.Click
    ' recupero BUNK!
    currentSheets = currNKC.getCurrentSheets

    If (currentSheets Is Nothing) Then
      ' non ho un current bunk
      Label1.Text = "non ho trovato nessun bunk corrente"
    Else

      Label1.Text = $"Trovata lista costituita da {currentSheets.SheetList.Count} pannelli"

      ' popolo il selettore dei fogli...
      cmbSheet.DataSource = currentSheets.SheetList

      updateDataButtons()
    End If

  End Sub

  Private Sub updateDataButtons()

    Dim showButtons As Boolean = False

    ' verifico se il ping è ok...
    If IsNothing(risultatoPing) Then
      lblPing.Text = "Effettuare test ping!"

    Else
      ' controllo se sia success..
      If risultatoPing.Status = IPStatus.Success Then
        ' ok show
        showButtons = True
        ' imposto dateora ed in base ai valori dt del currBunk abilito o meno i buttons...
        txtPrintStart.Text = currentSheets.SheetList(cmbSheet.SelectedIndex).Printing.DtStart.ToString()
        txtPrintEnd.Text = currentSheets.SheetList(cmbSheet.SelectedIndex).Printing.DtEnd.ToString()
        txtWorkStart.Text = currentSheets.SheetList(cmbSheet.SelectedIndex).Machining.DtStart.ToString()
        txtWorkEnd.Text = currentSheets.SheetList(cmbSheet.SelectedIndex).Machining.DtEnd.ToString()
        txtUnlStart.Text = currentSheets.SheetList(cmbSheet.SelectedIndex).Unloading.DtStart.ToString()
        txtUnlEnd.Text = currentSheets.SheetList(cmbSheet.SelectedIndex).Unloading.DtEnd.ToString()


        btnPrintStart.Enabled = String.IsNullOrWhiteSpace(txtPrintStart.Text)
        btnPrintEnd.Enabled = String.IsNullOrWhiteSpace(txtPrintEnd.Text)
        btnWorkStart.Enabled = String.IsNullOrWhiteSpace(txtWorkStart.Text)
        btnWorkEnd.Enabled = String.IsNullOrWhiteSpace(txtWorkEnd.Text)
        btnUnlStart.Enabled = String.IsNullOrWhiteSpace(txtUnlStart.Text)
        btnUnlEnd.Enabled = String.IsNullOrWhiteSpace(txtUnlEnd.Text)
      Else
        lblPing.Text = "Ping test fallito! " + risultatoPing.Status.ToString()

      End If


    End If

    ' fix buttons
    btnLoadBunk.Visible = showButtons
    btnPrintStart.Visible = showButtons
    btnPrintEnd.Visible = showButtons
    btnWorkStart.Visible = showButtons
    btnWorkEnd.Visible = showButtons
    btnUnlStart.Visible = showButtons
    btnUnlEnd.Visible = showButtons


  End Sub

  Private Sub btnSaveAll_Click(sender As Object, e As EventArgs) Handles btnSaveAll.Click
    saveSelection()


  End Sub

  Private Sub saveSelection()
    ' salvo SOLO SE è != ""
    If (Not String.IsNullOrEmpty(txtPrintStart.Text)) Then
      currentSheets.SheetList(cmbSheet.SelectedIndex).Printing.DtStart = txtPrintStart.Text
    End If
    If (Not String.IsNullOrEmpty(txtPrintEnd.Text)) Then
      currentSheets.SheetList(cmbSheet.SelectedIndex).Printing.DtEnd = txtPrintEnd.Text
    End If
    If (Not String.IsNullOrEmpty(txtWorkStart.Text)) Then
      currentSheets.SheetList(cmbSheet.SelectedIndex).Machining.DtStart = txtWorkStart.Text
    End If
    If (Not String.IsNullOrEmpty(txtWorkEnd.Text)) Then
      currentSheets.SheetList(cmbSheet.SelectedIndex).Machining.DtEnd = txtWorkEnd.Text
    End If
    If (Not String.IsNullOrEmpty(txtUnlStart.Text)) Then
      currentSheets.SheetList(cmbSheet.SelectedIndex).Unloading.DtStart = txtUnlStart.Text
    End If
    If (Not String.IsNullOrEmpty(txtUnlEnd.Text)) Then
      currentSheets.SheetList(cmbSheet.SelectedIndex).Unloading.DtEnd = txtUnlEnd.Text
    End If

    ' salvo!
    currNKC.saveSheets(currentSheets)

    updateDataButtons()
  End Sub

  Private Sub btnWorkStart_Click(sender As Object, e As EventArgs) Handles btnWorkStart.Click
    txtWorkStart.Text = DateTime.Now

  End Sub

  Private Sub btnWorkEnd_Click(sender As Object, e As EventArgs) Handles btnWorkEnd.Click
    txtWorkEnd.Text = DateTime.Now

  End Sub

  Private Sub btnUnlStart_Click(sender As Object, e As EventArgs) Handles btnUnlStart.Click
    txtUnlStart.Text = DateTime.Now

  End Sub

  Private Sub btnUnlEnd_Click(sender As Object, e As EventArgs) Handles btnUnlEnd.Click
    txtUnlEnd.Text = DateTime.Now

  End Sub

  Private Sub btnPing_Click(sender As Object, e As EventArgs) Handles btnPing.Click
    ' chiamo test ping...
    risultatoPing = currNKC.testPing
    lblPing.Text = risultatoPing.Status.ToString()

  End Sub

  Private Sub btnAlive_Click(sender As Object, e As EventArgs) Handles btnAlive.Click
    Dim answ As String = ""
    If (currNKC.testAlive) Then
      answ = currNKC.testClock
      lblAlive.Text = "Server Alive con clock " + answ

    End If

  End Sub

  Private Sub btnPersist_Click(sender As Object, e As EventArgs) Handles btnPersist.Click
    currNKC.persistedSheetList = currentSheets

  End Sub
End Class
