Imports EgtUILib
Imports EgtWPFLib5
Imports EgtBEAMWALL.Core
Imports EgtBEAMWALL.ViewerOptimizer

Public Module DbControllers

   Friend m_SupervisorId As String = "1"
   Private m_bNetwork As Boolean = False
   Friend ReadOnly Property bNetwork As Boolean
      Get
         Return m_bNetwork
      End Get
   End Property

   Public m_ProjController As DataLayer.Controllers.ProjController
   Public m_ProdController As DataLayer.Controllers.ProdController
   Public m_MachGroupController As DataLayer.Controllers.MachGroupController
   Public m_StatusMapController As DataLayer.Controllers.StatusMapController
   Public m_PartController As DataLayer.Controllers.PartController

   Sub Init()
   End Sub

   Sub New()

      ' se demo--> server = db.steamware.net, nKey e sKey come le hai...

      Dim sKey = "", sUser = "", sPwd = ""

      EgtGetKeyInfo(sKey)
      sKey = sKey.Replace(" ", "")
      If Not IsNothing(sKey) AndAlso sKey.Length > 11 Then
         sUser = sKey.Substring(3, 6)
         sPwd = sKey.Substring(10, sKey.Length - 10).Replace("(", "").Replace(")", "")
      End If
      Try
         Dim DbAddress As String = "127.0.0.1"
         Dim DbMasterKey As Integer = 0
         Dim sDbMasterKey As String = ""
         Dim sSharedFolder As String = ""
         If Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.NETWORK) Then
            GetMainPrivateProfileString(S_GENERAL, K_DBADDRESS, "", DbAddress)
            DbMasterKey = GetMainPrivateProfileInt(S_GENERAL, K_DBMASTERKEY, 0)
            If DbMasterKey > 0 Then
               sDbMasterKey = DbMasterKey.ToString("000000")
            End If
            If String.IsNullOrWhiteSpace(DbAddress) OrElse DbMasterKey <= 0 Then
               DbAddress = "127.0.0.1"
               DbMasterKey = 0
            Else
               m_bNetwork = True
            End If
         End If
         If Not m_bNetwork AndAlso Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.ANY_DB) Then
            DbMasterKey = GetMainPrivateProfileInt(S_GENERAL, K_DBMASTERKEY, 0)
            If DbMasterKey > 0 Then
               sDbMasterKey = DbMasterKey.ToString("000000")
            End If
         ElseIf GetMainPrivateProfileInt(S_GENERAL, K_DBMASTERKEY, 0) = 0 Then
            Dim nKey As Integer = 0
            If Integer.TryParse(sKey.Substring(3, 6), nKey) AndAlso nKey > 0 Then
               WriteMainPrivateProfileString(S_GENERAL, K_DBMASTERKEY, nKey)
            End If
         End If
         DataLayer.DbConfig.InitDb(DbAddress, sUser, sPwd, sDbMasterKey)
         DataLayer.DbConfig.SetupLogDir(Map.refMainWindowVM.MainWindowM.sTempDir)
         DataLayer.DbConfig.CheckMigrateDb()
         DataLayer.DbConfig.CheckUser(sUser, sPwd, m_bNetwork AndAlso Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.NETWORK))
         DataLayer.DbConfig.CheckViews(sUser, sPwd)
         ' riattivare il dump per il backup
         'DataLayer.DbConfig.DumpDB("C:\Program Files\MariaDB 10.5\bin\mysqldump", "c:\Temp\Pippo.sql")
         If m_bNetwork AndAlso Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.NETWORK) Then
            ' imposto cartella condivisa
            GetMainPrivateProfileString(S_GENERAL, K_SHAREDFOLDER, Map.refMainWindowVM.MainWindowM.sDataDir, sSharedFolder)
            Map.refMainWindowVM.MainWindowM.UpdatePathForNetwork(sSharedFolder)
            Dim sSupervisorMachName As String = ""
            GetMainPrivateProfileString(S_MACH, K_SUPERVISORMACH, "", sSupervisorMachName)
            If Not String.IsNullOrWhiteSpace(sSupervisorMachName) Then m_SupervisorId = sSupervisorMachName
         ElseIf Map.refMainWindowVM.MainWindowM.GetKeyOption(KEY_OPT.ANY_DB) Then
            ' imposto cartella chiave master
            Map.refMainWindowVM.MainWindowM.UpdatePathForKey(sDbMasterKey)
         Else
            ' imposto cartella chiave locale
            Map.refMainWindowVM.MainWindowM.UpdatePathForKey(sKey.Substring(3, 6))
         End If
      Catch ex As Exception
         EgtOutLog("InitDb error: " & ex.ToString())
         MessageBox.Show(Application.Current.MainWindow, "Db connection error!!", "Error")
         End
      End Try

      m_ProjController = New DataLayer.Controllers.ProjController
      m_ProdController = New DataLayer.Controllers.ProdController
      m_MachGroupController = New DataLayer.Controllers.MachGroupController
      m_StatusMapController = New DataLayer.Controllers.StatusMapController
      m_PartController = New DataLayer.Controllers.PartController
   End Sub

End Module
