Imports System.Windows.Threading
Imports System.Collections.ObjectModel
Imports System.IO
Imports EgtUILib
Imports EgtWPFLib

Public Class StatisticsPageUC

   ' Riferimento alla MainWindow
   Private m_MainWindow As MainWindow = DirectCast(Application.Current.MainWindow, MainWindow)

   Private Sub StatisticsPage_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized
      ' Imposto i messaggi letti dal file dei messaggi
      DayProductionGpBx.Header = EgtMsg(91071)
      DayPartsTxBl.Text = EgtMsg(91074)
      DayCutsTxBl.Text = EgtMsg(91075)
      DayProductionResetBtn.Content = EgtMsg(91078)
      WeekProductionGpBx.Header = EgtMsg(91072)
      WeekPartsTxBl.Text = EgtMsg(91074)
      WeekCutsTxBl.Text = EgtMsg(91075)
      WeekProductionResetBtn.Content = EgtMsg(91078)
      ProjectProductionGpBx.Header = EgtMsg(91073)
      ProjectPartsTxBl.Text = EgtMsg(91074)
      ProjectCutsTxBl.Text = EgtMsg(91079)
   End Sub

   Private Sub StatisticsPage_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
      
      ' Unità di misura
      Dim dCoeff As Double = GetCoeff()
      Dim sUnit As String = GetUnits()

      ' Leggo il giorno
      Dim sDay As String = String.Empty
      GetPrivateProfileString(S_STATDATA, K_SD_DAY, "---", sDay, m_MainWindow.GetIniFile())
      DayTxBl.Text = sDay
      ' Leggo il numero della settimana
      Dim nWeek As Integer = GetPrivateProfileInt(S_STATDATA, K_SD_WEEK, -1, m_MainWindow.GetIniFile())
      WeekTxBl.Text = ( nWeek + 1).ToString()

      ' Leggo area pezzi del giorno
      Dim dDayParts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_DAYPARTS, 0, m_MainWindow.GetIniFile())
      Dim dDayResetParts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_DAYRESETPARTS, 0, m_MainWindow.GetIniFile())
      DayPartsTxBx.Text = DoubleToString( ( dDayParts - dDayResetParts) * dCoeff, 3) & sUnit
      ' Leggo area tagli del giorno
      Dim dDayCuts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_DAYCUTS, 0, m_MainWindow.GetIniFile())
      Dim dDayResetCuts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_DAYRESETCUTS, 0, m_MainWindow.GetIniFile())
      DayCutsTxBx.Text = DoubleToString( ( dDayCuts - dDayResetCuts) * dCoeff, 3) & sUnit
      ' Leggo area pezzi della settimana
      Dim dWeekParts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_WEEKPARTS, 0, m_MainWindow.GetIniFile())
      Dim dWeekResetParts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_WEEKRESETPARTS, 0, m_MainWindow.GetIniFile())
      WeekPartsTxBx.Text = DoubleToString( ( dWeekParts - dWeekResetParts) * dCoeff, 3) & sUnit
      ' Leggo area tagli della settimana
      Dim dWeekCuts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_WEEKCUTS, 0, m_MainWindow.GetIniFile())
      Dim dWeekResetCuts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_WEEKRESETCUTS, 0, m_MainWindow.GetIniFile())
      WeekCutsTxBx.Text = DoubleToString( ( dWeekCuts - dWeekResetCuts) * dCoeff, 3) & sUnit

      ' Recupero il nome del progetto
      Dim sName As String = m_MainWindow.m_CurrentProjectPageUC.GetCurrentProjectName()
      ProjectTxBl.Text = sName
      ' Recupero area pezzi del progetto
      Dim dTotArea As Double = m_MainWindow.m_CurrentProjectPageUC.GetTotalArea()
      ProjectPartsTxBx.Text = DoubleToString(dTotArea * dCoeff, 3) & sUnit
      ' Recupero area tagli del progetto
      Dim dToProdArea As Double = m_MainWindow.m_CurrentProjectPageUC.GetToProduceArea()
      ProjectCutsTxBx.Text = DoubleToString(dToProdArea * dCoeff, 3) & sUnit

   End Sub

   Private Sub DayProductionResetBtn_Click(sender As Object, e As RoutedEventArgs) Handles DayProductionResetBtn.Click

      ' Unità di misura
      Dim dCoeff As Double = GetCoeff()
      Dim sUnit As String = GetUnits()

      ' Leggo l'attuale area pezzi del giorno e la scrivo nel corrispondente valore di reset
      Dim dDayResetParts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_DAYPARTS, 0, m_MainWindow.GetIniFile())
      WritePrivateProfileString(S_STATDATA, K_SD_DAYRESETPARTS, dDayResetParts, m_MainWindow.GetIniFile())
      ' Aggiorno area pezzi del giorno
      DayPartsTxBx.Text = DoubleToString( 0 * dCoeff, 3) & sUnit

      ' Leggo l'attuale area tagli del giorno e la scrivo nel corrispondente valore di reset
      Dim dDayResetCuts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_DAYCUTS, 0, m_MainWindow.GetIniFile())
      WritePrivateProfileString(S_STATDATA, K_SD_DAYRESETCUTS, dDayResetCuts, m_MainWindow.GetIniFile())
      ' Aggiorno area tagli del giorno
      DayCutsTxBx.Text = DoubleToString( 0 * dCoeff, 3) & sUnit
   End Sub

   Private Sub WeekProductionResetBtn_Click(sender As Object, e As RoutedEventArgs) Handles WeekProductionResetBtn.Click

      ' Unità di misura
      Dim dCoeff As Double = GetCoeff()
      Dim sUnit As String = GetUnits()

      ' Leggo l'attuale area pezzi della settimana e la scrivo nel corrispondente valore di reset
      Dim dWeekResetParts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_WEEKPARTS, 0, m_MainWindow.GetIniFile())
      WritePrivateProfileString(S_STATDATA, K_SD_WEEKRESETPARTS, dWeekResetParts, m_MainWindow.GetIniFile())
      ' Aggiorno area pezzi della settimana
      WeekPartsTxBx.Text = DoubleToString( 0 * dCoeff, 3) & sUnit

      ' Leggo l'attuale area tagli della settimana e la scrivo nel corrispondente valore di reset
      Dim dWeekResetCuts As Double = GetPrivateProfileDouble(S_STATDATA, K_SD_WEEKCUTS, 0, m_MainWindow.GetIniFile())
      WritePrivateProfileString(S_STATDATA, K_SD_WEEKRESETCUTS, dWeekResetCuts, m_MainWindow.GetIniFile())
      ' Aggiorno area tagli della settimana
      WeekCutsTxBx.Text = DoubleToString( 0 * dCoeff, 3) & sUnit
   End Sub

   Private Function GetCoeff() As Double
      Return If( EgtUiUnitsAreMM(), 1.0 / 1000000.0, 1.0 / (12 * ONEINCH * 12 * ONEINCH))
   End Function

   Private Function GetUnits() As String
      Return If( EgtUiUnitsAreMM(), " m²", " ft²")
   End Function

End Class

