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

#include <vcl.h>
#pragma hdrstop
#include <tchar.h>
#include <cxFormats.hpp>
//---------------------------------------------------------------------------
#include "AppGlobalF.h"
#include "ITSDbF.h"
#include "ITSLangTransF.h"

//---------------------------------------------------------------------------
USEFORM("MAIN\FrmVmsLogSttsF.cpp", FrmVmsLogStts);
USEFORM("MAIN\FrmVmsLogSttsSubF.cpp", FrmVmsLogSttsSub);
//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    int nLoginRes;

    DateSeparator = '-';
    TimeSeparator = ':';
    ShortDateFormat ="yyyy-MM-dd";
    ShortTimeFormat = "hh:nn:ss";
    LongTimeFormat  = "hh:nn:ss";

    cxFormatController()->BeginUpdate();
    cxFormatController()->UseDelphiDateTimeFormats = true;
    cxFormatController()->EndUpdate();
    cxFormatController()->GetFormats();
    cxFormatController()->NotifyListeners();

    String sLoginId = "ADMIN";
    bool bLogin = true;
    int nArgs = ParamCount();
    if (nArgs >= 1)
    {
        sLoginId = ParamStr(1);
        bLogin = false;
    }

    int nRes;
    g_nPid     = (int)GetCurrentProcessId();
    g_sAppDir  = ExtractFilePath(Application->ExeName);
    g_sAppName = ChangeFileExt(ExtractFileName(Application->ExeName), "");
    g_sCfgDir  = g_sAppDir + "Cfg\\";
    g_sLogDir  = g_sAppDir + "Log\\";
    g_sTempDir = g_sAppDir + "Temp\\";
    g_sFormsDir = g_sCfgDir + "Forms\\";

    ForceDirectories(g_sCfgDir.c_str());
    ForceDirectories(g_sLogDir.c_str());
    ForceDirectories(g_sTempDir.c_str());
    ForceDirectories(g_sFormsDir.c_str());
    String sTempDir = g_sLogDir + "Db\\";
    ForceDirectories(sTempDir.c_str());

    String sAppDir = ExtractFilePath(Application->ExeName);
    ChDir(sAppDir);

    /*
    *  시스템 운영환경을 ini 파일에서 읽어 온다.
    */
    LoadDefaultConfigInfo("ITS_OP");

    g_sLangDir = g_sAppDir + "Lang\\";
    g_sLangDir = g_sLangDir + g_AppCfg.sLang + "\\";
    ForceDirectories(g_sLangDir.c_str());

    LangTrans = new TLangTrans("VmsLogStts", g_AppCfg.sLang, g_sLangDir, true);

    ITSLog = new TITSLog(g_sLogDir, g_sAppName, g_AppCfg.sLogDay);
    ITSLog->FLogCfg = g_LogCfg;

	if (!ITSDb_Initialize())
	{
        if (g_AppCfg.sLang == "kr")
        {
            Application->MessageBox(L"Database 자원을 시스템으로부터 얻지 못했습니다.\r\n\r\n프로그램을 종료합니다.",
                                    L"프로그램 시작 오류!!!",
                                    MB_OK|MB_ICONERROR);
        }
        else
        {
            Application->MessageBox(L"Database resource could not be obtained from the system.\r\n\r\nExit the program.",
                                    L"Program Start Error!!!",
                                    MB_OK|MB_ICONERROR);
        }
        goto prog_exit;
	}
    ITSDb_SetInfo(g_AppCfg.itsdb.sProvider, g_AppCfg.itsdb.sServerName, g_AppCfg.itsdb.sUserName, g_AppCfg.itsdb.sPassword);

    if (!ITSDb_Open())
    {
        if (g_AppCfg.sLang == "kr")
        {
            Application->MessageBox(L"데이터베이스 접속에 실패하였습니다.\r\n\r\n프로그램을 종료합니다.",
                                    L"프로그램 시작 오류!!!",
                                    MB_OK|MB_ICONERROR);
        }
        else
        {
            Application->MessageBox(L"Database connection failed.\r\n\r\nExit the program.",
                                    L"Program Start Error!!!",
                                    MB_OK|MB_ICONERROR);
        }
        goto prog_exit;
    }
    try
    {
        ReportMemoryLeaksOnShutdown = true;
        Application->Initialize();
        Application->MainFormOnTaskBar = true;
        Application->Title = g_AppCfg.sTitle;
        Application->Title = "VMS 상태이력 조회";
         Application->CreateForm(__classid(TFrmVmsLogStts), &FrmVmsLogStts);
         Application->Run();
    }
    catch (Exception &exception)
    {
         Application->ShowException(&exception);
    }
    catch (...)
    {
         try
         {
             throw Exception("");
         }
         catch (Exception &exception)
         {
             Application->ShowException(&exception);
         }
    }

prog_exit:
    try
    {
        ITSDb_Finalize();
    }
    catch(...)
    {
    }
    
    return 0;
}
//---------------------------------------------------------------------------

