//---------------------------------------------------------------------------

#include <vcl.h>
#include "AppGlobalF.h"
#include "CDSMonitoringObjF.h"
#include "DMDbF.h"
#pragma hdrstop

#include "FrmInitializeF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxProgressBar"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TFrmInitialize *FrmInitialize = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmInitialize::TFrmInitialize(TComponent* Owner)
    : TForm(Owner)
{
    if (g_AppCfg.sLang != "kr")
    {
        Caption = "Program initialize";
        LblStatus->Caption = "Initialize...";
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmInitialize::FormShow(TObject *Sender)
{
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmInitialize::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;

    Refresh();

    Application->ProcessMessages();
    cxProgressBar1->Properties->Max = 100;

    try
    {
        if (g_AppCfg.sLang != "kr") LblStatus->Caption = "Connect database...";
        else                        LblStatus->Caption = "데이터베이스 연결 중...";
        Application->ProcessMessages();
        cxProgressBar1->Position = 10;

        DMDb->SetConnectString(g_AppCfg.itsdb.sProvider, g_AppCfg.itsdb.sServerName, g_AppCfg.itsdb.sUserName, g_AppCfg.itsdb.sPassword);
        if (!DMDb->Connect())
        {
            if (g_AppCfg.sLang != "kr")
            {
                Application->MessageBox(L"Database connection failed.\r\n\r\nPlease restart the program.",
                                        L"Database connection error !!!",
                                        MB_OK|MB_ICONERROR);
            }
            else
            {
                Application->MessageBox(L"데이터베이스 연결에 실패하였습니다.\r\n\r\n프로그램을 다시 시작하십시요.",
                                        L"데이터베이스 연결 오류!!!",
                                        MB_OK|MB_ICONERROR);
            }
            return;
        }

        if (g_AppCfg.sLang != "kr") LblStatus->Caption = "Loading basic informations...";
        else                        LblStatus->Caption = "기본 정보 로딩 중...";
        Application->ProcessMessages();
        cxProgressBar1->Position = 20;

        ObjCtlrManager->LoadCtlrFromDb(DMDb->GetConnection());

        if (g_AppCfg.sLang != "kr") LblStatus->Caption = "Initialize default monitoring screen...";
        else                        LblStatus->Caption = "기본 모니터링 화면 초기화...";
        Application->ProcessMessages();
        cxProgressBar1->Position = 40;

        ObjCtlrManager->InitMonitoringFormFromDb(DMDb->GetConnection());

        if (g_AppCfg.sLang != "kr") LblStatus->Caption = "Displaying monitoring screen...";
        else                        LblStatus->Caption = "모니터링 화면 조회중...";
        Application->ProcessMessages();
        Application->ProcessMessages();
        cxProgressBar1->Position = 60;

        ObjCtlrManager->LoadMonitoringFormFromDb(DMDb->GetConnection());

        if (g_AppCfg.sLang != "kr")  LblStatus->Caption = "Configuring monitoring screen...";
        else                         LblStatus->Caption = "모니터링 화면 구성중...";
        Application->ProcessMessages();
        cxProgressBar1->Position = 100;
        Application->ProcessMessages();

        DMDb->Close();;
    }
    catch(...)
    {
    }

    Close();
}
//---------------------------------------------------------------------------

void __fastcall TFrmInitialize::FormClose(TObject *Sender, TCloseAction &Action)
{
    Action = caFree;    
}
//---------------------------------------------------------------------------

