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

#include <vcl.h>
#pragma hdrstop

#include "AppGlobalF.h"
#include "ITSDbF.h"
#include <inifiles.hpp>

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
String           CenterCd = "";         // 지역센터 코드
String           CenterId = "";         // 지역센터 아이디
String           CenterName = "";       // 지역센터 명칭

int              g_nPid    = -1;        // Application Process ID
String           g_sAppDir = "";        // Application Directory
String           g_sAppName= "";        // Program name
String           g_sCfgDir = "";        // Config File Directory
String           g_sLogDir = "";        // Program Log Directory
String           g_sTempDir  = "";      // Program Temp Directory
String           g_sFormsDir = "";      // Program Forms Directory
String           g_sLangDir = "";

APP_CONFIG       g_AppCfg;
LOG_INFO         g_LogCfg;
TITSLog         *ITSLog = NULL;

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

void LoadIcon(TIniFile* AIniFile, String ASection, String AItem, String& AValue)
{
    try
    {
        AValue = AIniFile->ReadString(ASection, AItem, "");
        if (AValue != "")
        {
            AValue = g_sAppDir + "Image\\" + AValue;
            if (!FileExists(AValue)) AValue = "";
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------
bool LoadDefaultConfigInfo(String ACfgFile/*=""*/)
{
    String   sCfgFile;
    TIniFile *pIniFile = NULL;

    String sIniFile = ChangeFileExt(ExtractFileName(Application->ExeName), ".ini");
    if (!ACfgFile.IsEmpty())
        sIniFile = ACfgFile + ".ini";

    sCfgFile = g_sCfgDir + sIniFile;
    g_AppCfg.sConfigFile = sCfgFile;

    g_AppCfg.AutoLogout.Enabled = false;
    g_AppCfg.AutoLogout.IntervalMin = 0;
    g_AppCfg.AutoLogout.LogoutExit = false;

    g_AppCfg.thr.pThread    = NULL;
    g_AppCfg.thr.dwThreadId = 0;
    g_AppCfg.thr.nHandle    = 0;
    g_AppCfg.thr.bRunning   = false;

    g_AppCfg.bAppClose    = false;

    try
    {
        String sTmp;

        pIniFile = new TIniFile(sCfgFile);

        g_AppCfg.sTitle     = pIniFile->ReadString("APPLICATION", "TITLE",     "광역교통정보 교통관리시스템");
        g_AppCfg.sProcessId = pIniFile->ReadString("APPLICATION", "PROCESSID", "999999");
        g_AppCfg.sLogDay    = pIniFile->ReadString("APPLICATION", "LOGDAY",    "");
        g_AppCfg.sAppPath   = pIniFile->ReadString("APPLICATION", "APP_PATH",  "");

        g_AppCfg.sSkinName  = pIniFile->ReadString("APPLICATION", "SKINNAME", "Black");
        if (g_AppCfg.sSkinName.IsEmpty()) g_AppCfg.sSkinName = "Blue";

        sTmp = pIniFile->ReadString("APPLICATION", "SAVEFORM", "1");
        g_AppCfg.bSaveForm = (sTmp == "1") ? true : false;

        sTmp = "1";
        sTmp = pIniFile->ReadString("APPLICATION", "LOGINPROMPT", "1");
        g_AppCfg.bLoginPrompt = (sTmp == "1") ? true : false;
        g_AppCfg.sDefUseId = pIniFile->ReadString("APPLICATION", "LASTUSER", "");

        g_AppCfg.sLang = pIniFile->ReadString("APPLICATION", "LANGUAGE", "kr");
        g_AppCfg.sLang = g_AppCfg.sLang.LowerCase();
        if (g_AppCfg.sLang != "en" && g_AppCfg.sLang != "kr")
            g_AppCfg.sLang = "kr";

        g_AppCfg.bDebug = false;
        sTmp = pIniFile->ReadString("APPLICATION", "DEBUG", "0");
        if (sTmp == "1") g_AppCfg.bDebug = true;

        String sSqlLog;
        g_AppCfg.itsdb.bSqlLog     = false;
        g_AppCfg.itsdb.sProvider   = pIniFile->ReadString("ITSDB", "PROVIDER",   "OraOLEDB.Oracle.1");
        g_AppCfg.itsdb.sServerName = pIniFile->ReadString("ITSDB", "SERVERNAME", "HANTE");
        g_AppCfg.itsdb.sUserName   = pIniFile->ReadString("ITSDB", "USERNAME",   "hnits");
        g_AppCfg.itsdb.sPassword   = pIniFile->ReadString("ITSDB", "PASSWORD",   "hnits");
        //sSqlLog                    = pIniFile->ReadString("ITSDB", "SQLLOG",     "0");
        //if (sSqlLog == "1") g_AppCfg.itsdb.bSqlLog = true;

        LoadIcon(pIniFile, "CCTV", "CAMERA_EMPTY", g_AppCfg.sEmptyImg);

        CenterId   = pIniFile->ReadString("CENTER", "CENTERID",   "L01");
        CenterName = pIniFile->ReadString("CENTER", "CENTERNAME", "중앙센터");
        CenterCd   = String(StrToInt(CenterId.SubString(2, 2)));
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }

    return true;
}
//---------------------------------------------------------------------------
/*
 * 환경설정 정보를 저장하는 함수.
 *  arguments
 *      String : RegisterKey 또는 파일이름
 *  return
 *      bool : 실패하면 false
 */
bool WriteConfigInfo(String sCfgFile, String sTitle, String sItem, String sValue)
{
	String	 ConfigFile;
	TIniFile *pIniFile = NULL;

    ConfigFile = sCfgFile;

    try
    {
        pIniFile = new TIniFile(ConfigFile);
        if (pIniFile == NULL)
        {
            return false;
        }

        pIniFile->WriteString(sTitle, sItem, sValue);
    }
    catch(...)
    {
    }

    if (pIniFile)
    {
        pIniFile->Free();
        pIniFile = NULL;
    }

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

/*
 * 환경설정 정보를 읽어오는 함수.
 *  arguments
 *      String : RegisterKey 또는 파일이름
 *  return
 *      bool : 실패하면 false
 */
bool ReadConfigInfo(String sCfgFile, String sTitle, String sItem, String &sValue)
{
    bool     bRes;
	String   ConfigFile;
	TIniFile *pIniFile = NULL;

    bRes = false;

    ConfigFile = sCfgFile;

    try
    {
        pIniFile = new TIniFile(ConfigFile);
        if (pIniFile == NULL)
        {
            return bRes;
        }

        sValue = pIniFile->ReadString(sTitle, sItem, "");
        if (sValue != "")
        {
            bRes = true;
        }
    }
    catch(...)
    {
    }

    if (pIniFile)
    {
        pIniFile->Free();
        pIniFile = NULL;
    }

	return bRes;
}
//---------------------------------------------------------------------------

void ShowErrorMsg(String ATitle, String AErrMsg)
{
    Application->NormalizeTopMosts();
    Application->MessageBox(AErrMsg.c_str(), ATitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
    Application->RestoreTopMosts();
}
//----------------------------------------------------------------------------

ULONG ProcIDFromWnd(HWND hwnd) // 윈도우 핸들로 프로세스 아이디 얻기
{
  ULONG idProc;
  GetWindowThreadProcessId( hwnd, &idProc );
  return idProc;
}
//----------------------------------------------------------------------------

HWND GetWinHandle(ULONG pid) // 프로세스 아이디로 윈도우 핸들 얻기
{
  HWND tempHwnd = FindWindow(NULL,NULL); // 최상위 윈도우 핸들 찾기

  while( tempHwnd != NULL )
  {
    if( GetParent(tempHwnd) == NULL ) // 최상위 핸들인지 체크, 버튼 등도 핸들을 가질 수 있으므로 무시하기 위해
      if( pid == ProcIDFromWnd(tempHwnd) )
        return tempHwnd;
    tempHwnd = GetWindow(tempHwnd, GW_HWNDNEXT); // 다음 윈도우 핸들 찾기
  }
  return NULL;
}
//----------------------------------------------------------------------------

TColor  g_DispColor[MAX_DISPCOLOR];
void APP_InitDisplayColor()
{
    g_DispColor[ 0] = Graphics::clBlue;
    g_DispColor[ 1] = Graphics::clRed;
    g_DispColor[ 2] = Graphics::clLime;
    g_DispColor[ 3] = Graphics::clMaroon;
    g_DispColor[ 4] = Graphics::clGreen;
    g_DispColor[ 5] = Graphics::clMenuHighlight;
    g_DispColor[ 6] = Graphics::clBackground;
    g_DispColor[ 7] = Graphics::clPurple;
    g_DispColor[ 8] = Graphics::clTeal;
    g_DispColor[ 9] = Graphics::clYellow;
    g_DispColor[10] = Graphics::clFuchsia;
    g_DispColor[11] = Graphics::clAqua;
    g_DispColor[12] = Graphics::clMoneyGreen;
    g_DispColor[13] = Graphics::clSkyBlue;
    g_DispColor[14] = Graphics::clRed;
    g_DispColor[15] = Graphics::clLime;
    g_DispColor[16] = Graphics::clYellow;
    g_DispColor[17] = Graphics::clBlue;
    g_DispColor[18] = Graphics::clFuchsia;
    g_DispColor[19] = Graphics::clAqua;
    g_DispColor[20] = Graphics::clMoneyGreen;
    g_DispColor[21] = Graphics::clActiveCaption;
}
//---------------------------------------------------------------------------

TColor APP_GetDisplayColor(int ASeq)
{
    return g_DispColor[ASeq % MAX_DISPCOLOR];
}
//---------------------------------------------------------------------------

