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

#include <vcl.h>
#include "WindowMsgF.h"

#include "AppGlobalF.h"
#include "CDSIncidentF.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 "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "IdBaseComponent"
#pragma link "IdComponent"
#pragma link "IdExplicitTLSClientServerBase"
#pragma link "IdFTP"
#pragma link "IdTCPClient"
#pragma link "IdTCPConnection"
#pragma resource "*.dfm"

//TFrmInitialize *FrmInitialize = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmInitialize::TFrmInitialize(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

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

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

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

    try
    {
        cxProgressBar1->Position = 10;
        LblStatus->Caption = "VMS 정보 로딩...";
        Application->ProcessMessages();
        if (!VmsCtlrManager)
        {
            VmsCtlrManager = new TVmsCtlrManager();
            VmsCtlrManager->Load();
            LblStatus->Caption = "VMS 상태정보 로딩...";
            Application->ProcessMessages();
            VmsCtlrManager->LoadStts();
        }
#if 0
        cxProgressBar1->Position = 10;
        LblStatus->Caption = cxLabel1->Caption;;//"코드정보 로딩...";
        Application->ProcessMessages();
        if (!ItsCodeManager)
        {
            ItsCodeManager = new TItsCodeManager();
#if 0
            ItsCodeManager->LoadFromDb();
#endif
        }
        if (!CDSFontManager)
        {
            CDSFontManager = new TCDSFontManager();
            CDSFontManager->LoadFontInfo();
        }
        if (!CDSAtmpManager)
        {
            CDSAtmpManager = new TCDSAtmpManager();
#ifdef USE_ATMP
            CDSAtmpManager->LoadFromDb();
#endif
        }
        if (!CDSParkManager)
        {
            CDSParkManager = new TCDSParkManager();
#ifdef USE_PARK
            CDSParkManager->LoadFromDb();
#endif
        }

        if (!CDSVmsFormManager)
        {
            CDSVmsFormManager = new TCDSVmsFormManager();
            CDSVmsFormManager->LoadFromDb();
        }
        if (!VmsTypeManager)
        {
            VmsTypeManager = new TVmsTypeManager();
            VmsTypeManager->LoadFromDb();
        }
        cxProgressBar1->Position = 20;
        LblStatus->Caption = cxLabel2->Caption;//"프로세스정보 로딩...";
        Application->ProcessMessages();
        if (!ItsProcessManager)
        {
            ItsProcessManager = new TItsProcessManager();
            ItsProcessManager->LoadFromDb();
            //ItsProcessManager->LoadProcessStatusFromDb();
            FrmVmsOprMain->PostJobMessage(WM_THREAD, WP_PARAM_TRAFFIC, LP_MSG_PROCESS_STTS);
            FrmVmsOprMain->FPollPrcsState.Polling = GetTickCount();
        }

        cxProgressBar1->Position = 30;
        LblStatus->Caption = cxLabel3->Caption;//"VMS정보 로딩...";
        Application->ProcessMessages();
        if (!VmsCtlrManager)
        {
            VmsCtlrManager = new TVmsCtlrManager();
            VmsCtlrManager->LoadVmsSize();
            VmsCtlrManager->LoadFromDb();
            VmsCtlrManager->LoadVmsFormUsage();

            //VmsCtlrManager->LoadStatusFromDb();
            FrmVmsOprMain->PostJobMessage(WM_THREAD, WP_PARAM_TRAFFIC, LP_MSG_FACILITY_STTS);
            FrmVmsOprMain->FPollVmsState.Polling = GetTickCount();

            VmsCtlrManager->LoadIfscRltn();

            //VmsCtlrManager->LoadVmsMsg();
            FrmVmsOprMain->PostJobMessage(WM_THREAD, WP_PARAM_TRAFFIC, LP_MSG_VMS_DSPLMSG);
            FrmVmsOprMain->FPollVmsMsg.Polling = GetTickCount();
        }

        cxProgressBar1->Position = 40;
        LblStatus->Caption = cxLabel7->Caption;//"링크구간정보 로딩...";
        Application->ProcessMessages();
        if (!LinkManager)
        {
            LinkManager = new TLinkManager();
            //LinkManager->LoadFromDb();
            //LinkManager->LoadVertexFromDb();
        }

        //정보제공구간-정보제공구간정보만 사용(소통정보사용안함)
        cxProgressBar1->Position = 50;
        LblStatus->Caption = cxLabel4->Caption;//"정보제공구간정보 로딩...";
        Application->ProcessMessages();
        if (!IfscManager)
        {
            IfscManager = new TIfscManager();
            IfscManager->LoadFromDb();
            IfscManager->LoadVertexFromDb();
        }

        //VMS정보제공구간-소통정보도 사용함
        cxProgressBar1->Position = 60;
        LblStatus->Caption = cxLabel5->Caption;//"VMS정보제공구간정보 로딩...";
        Application->ProcessMessages();
        if (!VmsIfscManager)
        {
            VmsIfscManager = new TVmsIfscManager();
            VmsIfscManager->LoadFromDb();
            //VmsIfscManager->LoadTraffic();
        }
        FrmVmsOprMain->PostJobMessage(WM_THREAD, WP_PARAM_TRAFFIC, LP_MSG_TRAFFIC);
        FrmVmsOprMain->FPollTraffic.Polling = GetTickCount();

        cxProgressBar1->Position = 130;
        LblStatus->Caption = cxLabel6->Caption;//"돌발발생정보 로딩...";
        Application->ProcessMessages();
        if (!ItsIncidentManager)
        {
            ItsIncidentManager = new TItsIncidentManager();
            //ItsIncidentManager->LoadFromDb();
            FrmVmsOprMain->PostJobMessage(WM_THREAD, WP_PARAM_TRAFFIC, LP_MSG_INCIDENT);
            FrmVmsOprMain->FPollIncident.Polling = GetTickCount();
        }
#endif
    }
    catch(...)
    {
    }

    UploadVmsVideoFiles();

    Close();
}
//---------------------------------------------------------------------------
void __fastcall TFrmInitialize::FormClose(TObject *Sender, TCloseAction &Action)
{
    Action = caFree;
}
//---------------------------------------------------------------------------
bool __fastcall TFrmInitialize::VmsFtpConnect()
{
    if (g_AppCfg.vmsFtp.SERVER == "") return false;

    IdFtp->Host     = g_AppCfg.vmsFtp.SERVER;
    IdFtp->Port     = g_AppCfg.vmsFtp.PORT;
    IdFtp->Username = g_AppCfg.vmsFtp.USER;
    IdFtp->Password = g_AppCfg.vmsFtp.PSWD;
    IdFtp->Passive  = g_AppCfg.vmsFtp.PASSIVE;

    String sErrMsg;
    bool   bConnect = false;

    try
    {
        IdFtp->Connect();
        IdFtp->ChangeDir("VIDEO");
        IdFtp->IOHandler->DefStringEncoding = TIdTextEncoding::UTF8;//IndyTextEncoding(949);
        bConnect = true;

        IdFtp->TransferType = ftBinary;
    }
    catch (EIdSocketError &exSock)
    {
        sErrMsg = exSock.Message;
        sErrMsg = StringReplace(sErrMsg, "\r\n", " ", TReplaceFlags() << rfReplaceAll);
        //Application->MessageBox(sErrMsg.c_str(), FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return false;
    }
    catch (Exception &e)
    {
        sErrMsg = e.Message;
        sErrMsg = StringReplace(sErrMsg, "\r\n", " ", TReplaceFlags() << rfReplaceAll);
        //Application->MessageBox(sErrMsg.c_str(), FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return false;
    }
    return bConnect;
}
//---------------------------------------------------------------------------
bool __fastcall TFrmInitialize::VmsFtpFileDownload(String AOrgFile, String ADstFile)
{
    String sErrMsg;
    try
    {
        IdFtp->TransferType = ftBinary;
        IdFtp->Get(AOrgFile, ADstFile, false);
    }
    catch (EIdSocketError &exSock)
    {
        sErrMsg = exSock.Message;
        sErrMsg = StringReplace(sErrMsg, "\r\n", " ", TReplaceFlags() << rfReplaceAll);
        //Application->MessageBox(sErrMsg.c_str(), FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return false;
    }
    catch (Exception &e)
    {
        sErrMsg = e.Message;
        sErrMsg = StringReplace(sErrMsg, "\r\n", " ", TReplaceFlags() << rfReplaceAll);
        //Application->MessageBox(sErrMsg.c_str(), FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
        return false;
    }
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TFrmInitialize::VmsFtpDisconnect()
{
    if (IdFtp->Connected())
    {
        IdFtp->Abort();
        IdFtp->Quit();
        IdFtp->Disconnect();
    }
}
//---------------------------------------------------------------------------
bool __fastcall TFrmInitialize::UploadVmsVideoFiles()
{
#if 0
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT SYMB_LIB_NMBR, SYMB_FILE_NM          \r\n"
           "  FROM TB_VMS_SYMB_LIB                      \r\n"
           " WHERE SYMB_LIB_NMBR BETWEEN 5000 AND 5999  \r\n"
           "   AND SYMB_TYPE = 'SBT3'                   \r\n"
           "   AND DEL_YN = 'N'                         \r\n";

    ForceDirectories(g_sVideoDir.c_str());
#endif

return true;
#if 0
    if (!VmsFtpConnect()) return false;

    bool bRes = false;
    try
    {
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLOpen(pADO);

            for( ; !pADO->Eof; pADO->Next())
            {
                String SYMB_FILE_NM = pADO->FieldByName("SYMB_FILE_NM")->AsString;
                String strmFileName = g_sVideoDir + SYMB_FILE_NM;
                if (!FileExists(strmFileName))
                {
                    VmsFtpFileDownload(SYMB_FILE_NM, strmFileName);
                }
            }
            bRes = true;
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("TFrmInitialize::UploadVmsVideoFiles", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("TFrmInitialize::UploadVmsVideoFiles", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        VmsFtpDisconnect();
    }
    return bRes;
#endif
}
//---------------------------------------------------------------------------

