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


#pragma hdrstop

//---------------------------------------------------------------------------
#include <vcl.h>
#include <assert.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <Systobj.h>
#include <winsock.h>
#include <time.h>

#include <Buttons.hpp>
#include <Classes.hpp>
#include <ComCtrls.hpp>
#include <Controls.hpp>
#include <Chart.hpp>
#include <DB.hpp>
#include <DBClient.hpp>
#include <DBGrids.hpp>
#include <DBXpress.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <FMTBcd.hpp>
#include <Forms.hpp>
#include <Graphics.hpp>
#include <Grids.hpp>
#include <jpeg.hpp>
#include <Mask.hpp>
#include <Menus.hpp>
#include <Messages.hpp>
#include <OleCtrls.hpp>
#include <Outline.hpp>
#include <Provider.hpp>
#include <SqlExpr.hpp>
#include <StdCtrls.hpp>
#include <System.hpp>
#include <SysUtils.hpp>
#include <Types.hpp>
#include <Windows.hpp>
#include <inifiles.hpp>
//---------------------------------------------------------------------------
#include "AppGlobalF.h"
//---------------------------------------------------------------------------

#pragma package(smart_init)
//---------------------------------------------------------------------------

/*
******************************************************************************
* Global variables
******************************************************************************
*/
APP_CONFIG       g_AppCfg;
LOG_INFO         g_LogCfg;
TITSLog         *ITSLog = NULL;
TITSLog         *FDbLog = NULL;
bool LOGIN_CHECK = false;
bool LOAD_DB = false;

SYSTEM_INFORMATION *g_SysInfo;

//---------------------------------------------------------------------------
bool APP_LoadConfigInfo()
{
    String    sCfgFile;
    TIniFile *pIniFile = NULL;

    sCfgFile = g_sCfgDir + g_sAppName + ".ini";
    g_AppCfg.sConfigFile = sCfgFile;

    DATABASE_INFO *pDb;
    try
    {
        String sTmp;

        pIniFile = new TIniFile(sCfgFile);

        g_AppCfg.sTitle     = pIniFile->ReadString("APPLICATION", "TITLE",      "VMS Communication Server");
        g_AppCfg.sSystemId  = pIniFile->ReadString("APPLICATION", "SYSTEMID",   "VMS01");

        g_AppCfg.sProcessId = pIniFile->ReadString("APPLICATION", "PROCESSID",  "30101");
        g_AppCfg.nProcessId = g_AppCfg.sProcessId.ToIntDef(0);
        g_AppCfg.nProcessPort = pIniFile->ReadInteger("APPLICATION", "PROCESSPORT", 5301);;

        g_AppCfg.SaveFormImage  = (bool)pIniFile->ReadInteger("APPLICATION", "SAVEFORMIMAGE", 0);
        g_AppCfg.DownloadBitmap = (bool)pIniFile->ReadInteger("APPLICATION", "DOWNLOADBITMAP", 0);
        LOGIN_CHECK = (bool)pIniFile->ReadInteger("APPLICATION", "LOGINCHECK", 1);
        LOAD_DB = (bool)pIniFile->ReadInteger("APPLICATION", "LOADDB", 0);

        g_sFtpDir = pIniFile->ReadString("APPLICATION", "FTPHOMEDIR", g_sFtpDir);
        if (g_sFtpDir.SubString(g_sFtpDir.Length(), 1) != "\\")
            g_sFtpDir += "\\";

        g_AppCfg.sManagerApp= pIniFile->ReadString("APPLICATION", "MANAGERAPP", "");
        if (g_AppCfg.sManagerApp != "")
        {
            if (!FileExists(g_AppCfg.sManagerApp))
            {
                g_AppCfg.sManagerApp = "";
            }
        }

        g_AppCfg.nTrafficCycleTime = pIniFile->ReadInteger("APPLICATION", "TrafficCycleTime",  300);
        if (g_AppCfg.nTrafficCycleTime < 60) g_AppCfg.nTrafficCycleTime = 60;

        g_AppCfg.nTrafficBasisTime = pIniFile->ReadInteger("APPLICATION", "TrafficBasisTime",  60);
        g_AppCfg.nSaveFormType     = pIniFile->ReadInteger("APPLICATION", "SaveFormType",      0);
        if (g_AppCfg.nSaveFormType < 0 || g_AppCfg.nSaveFormType > 2) g_AppCfg.nSaveFormType = 0;
        g_AppCfg.nDefaultFillColor = pIniFile->ReadInteger("APPLICATION", "DefaultFillColor",  0);

        g_AppCfg.MaxMemory = pIniFile->ReadInteger("RESOURCE", "MAXMEMORY", 512);
        g_AppCfg.MaxHandle = pIniFile->ReadInteger("RESOURCE", "MAXHANDLE", 0);
        g_AppCfg.MaxThread = pIniFile->ReadInteger("RESOURCE", "MAXTHREAD", 4096);
        g_AppCfg.MaxGdi    = pIniFile->ReadInteger("RESOURCE", "MAXGDI",    0);
        g_AppCfg.RcRestart = (bool)pIniFile->ReadInteger("RESOURCE", "RESTART",   0);

        g_AppCfg.comm.nCenterPort  = pIniFile->ReadInteger("COMMUNICATION", "CENTERPORT",  6000);

        g_AppCfg.comm.nListenPort  = pIniFile->ReadInteger("COMMUNICATION", "LISTENPORT",  6020);
        g_AppCfg.comm.sLocalDomain = pIniFile->ReadString ("COMMUNICATION", "LOCALDOMAIN", "VMS_SERVER");

        g_AppCfg.comm.nClientPort = pIniFile->ReadInteger("COMMUNICATION", "CLIENTPORT", 355);
        g_AppCfg.comm.sLoginUser  = pIniFile->ReadString ("COMMUNICATION", "LOGINUSER",  "ysvms");
        g_AppCfg.comm.sLoginPswd  = pIniFile->ReadString ("COMMUNICATION", "LOGINPSWD",  "ysvms");

        g_AppCfg.comm.nConnectWait  = pIniFile->ReadInteger("COMMUNICATION", "CONNECTWAIT",  60);
        g_AppCfg.comm.nCmdTimeOut   = pIniFile->ReadInteger("COMMUNICATION", "CMDTIMEOUT",   5);
        g_AppCfg.comm.nWatchDogTime = pIniFile->ReadInteger("COMMUNICATION", "WATCHDOGTIME", 30);
        g_AppCfg.comm.nPollingTime  = pIniFile->ReadInteger("COMMUNICATION", "POLLINGTIME",  20);

        if (g_AppCfg.comm.nPollingTime < 10) g_AppCfg.comm.nPollingTime = 10;
        if (g_AppCfg.comm.nPollingTime > 90) g_AppCfg.comm.nPollingTime = 90;

	    g_AppCfg.nCenterCommLogLevel = pIniFile->ReadInteger("COMMUNICATION", "CenterCommLogLevel", 0);

        if (g_AppCfg.comm.nConnectWait  < 10) g_AppCfg.comm.nConnectWait  = 10;
        if (g_AppCfg.comm.nConnectWait  > 60) g_AppCfg.comm.nConnectWait  = 60;
        if (g_AppCfg.comm.nCmdTimeOut   <  5) g_AppCfg.comm.nCmdTimeOut   =  5;
        if (g_AppCfg.comm.nWatchDogTime < 30) g_AppCfg.comm.nWatchDogTime = 30;
        g_AppCfg.comm.nWatchDogTime += 5;   // 5초정도를 더 추가해 준다(딜레이발생할 경우)

        pDb = &g_AppCfg.db;
        pDb->sProvider   = pIniFile->ReadString("DATABASE", "PROVIDER",   "");
        pDb->sServerName = pIniFile->ReadString("DATABASE", "SERVERNAME", "");
        pDb->sCatalog    = pIniFile->ReadString("DATABASE", "CATALOG",    "");
        pDb->sUserName   = pIniFile->ReadString("DATABASE", "USERNAME",   "");
        pDb->sPassword   = pIniFile->ReadString("DATABASE", "PASSWORD",   "");
        pDb->MakeConnString();

        try
        {
            g_AppCfg.nMaxLogLines = pIniFile->ReadInteger("LOG", "LOGLINE", 200);
            if (g_AppCfg.nMaxLogLines > 1000) g_AppCfg.nMaxLogLines = 1000;
            if (g_AppCfg.nMaxLogLines < 0)    g_AppCfg.nMaxLogLines = 100;

            g_AppCfg.sLogDay      = pIniFile->ReadString ("LOG", "LOGDAY",  "");

            g_LogCfg.Info    = (bool)pIniFile->ReadInteger("LOG", "INFO",    1);
            g_LogCfg.Data    = (bool)pIniFile->ReadInteger("LOG", "DATA",    0);
            g_LogCfg.Error   = (bool)pIniFile->ReadInteger("LOG", "ERROR",   1);
            g_LogCfg.Warning = (bool)pIniFile->ReadInteger("LOG", "WARNING", 1);
            g_LogCfg.Debug   = (bool)pIniFile->ReadInteger("LOG", "DEBUG",   0);
            g_LogCfg.Detail  = (bool)pIniFile->ReadInteger("LOG", "DETAIL",  0);
            g_LogCfg.Detail  = 0;
        } catch(Exception &e) {}

        g_AppCfg.bThrExit = false;
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }

    return true;
}
//---------------------------------------------------------------------------

