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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "AppGlobalF.h"
#include "ITSLangTransF.h"
#include "ITS_OPLibF.h"
#pragma hdrstop

#include "CFG0000MF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtonEdit"
#pragma link "cxButtons"
#pragma link "cxCheckBox"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "cxLabel"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TCFG0000M *CFG0000M = NULL;
//---------------------------------------------------------------------------
__fastcall TCFG0000M::TCFG0000M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    CMM_LoadForm(g_sFormsDir, this);

    GrpMap->Visible = false;
    //GrpAlarm->Visible = true;
    GrpLogout->Visible = false;
    Height = 418;

    FTitle = Caption;//"환경설정";
}
//---------------------------------------------------------------------------

void __fastcall TCFG0000M::CommClose()
{
    CMM_SaveForm(g_sFormsDir, this);
}
//---------------------------------------------------------------------------

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

void __fastcall TCFG0000M::TmrShowTimer(TObject *Sender)
{
    TmrShow->Enabled = false;
    RefreshData();
}
//---------------------------------------------------------------------------

void __fastcall TCFG0000M::RefreshData()
{
    // 정보를 읽어와서 표출하자.
#if 0
    ST_GISINFO GisInfo;
    String sLeftTopX     = GetConfigValue("MAP", "LEFTTOPX",     "126.891");
    String sLeftTopY     = GetConfigValue("MAP", "LEFTTOPY",     "037.383");
    String sRightBottomX = GetConfigValue("MAP", "RIGHTBOTTOMX", "126.981");
    String sRightBottomY = GetConfigValue("MAP", "RIGHTBOTTOMY", "037.318");
    String sCenterX      = GetConfigValue("MAP", "CENTERX",      "126.935");
    String sCenterY      = GetConfigValue("MAP", "CENTERY",      "037.360");

    GisInfo.dLeftTopX     = sLeftTopX.ToDouble();
    GisInfo.dLeftTopY     = sLeftTopY.ToDouble();
    GisInfo.dRightBottomX = sRightBottomX.ToDouble();
    GisInfo.dRightBottomY = sRightBottomY.ToDouble();
    GisInfo.dCenterX      = sCenterX.ToDouble();
    GisInfo.dCenterY      = sCenterY.ToDouble();

    GisInfo.sCenterName     = CenterName;
    GisInfo.sMapFileNameL0  = GetConfigValue("MAP", "MAPFILEL0",      "");
    GisInfo.sIdxMapFileName = "";//GetConfigValue("MAP", "INDEXMAPFILE", "");
#endif

    EdCenterId->Text   = CenterId;
    EdCenterName->Text = CenterName;
    //EdGisFile->Text    = g_GisInfo.sMapFileNameL0;
    EdDbProvider->Text = g_AppCfg.itsdb.sProvider;
    EdDbUser->Text     = g_AppCfg.itsdb.sUserName;
    EdDbSource->Text   = g_AppCfg.itsdb.sServerName;
    EdDbPasswd->Text   = g_AppCfg.itsdb.sPassword;
    EdProcessId->Text  = g_AppCfg.sProcessId;
    //EdTaskId->Text     = "";
    //EdNodeId->Text     = "";
    //EdSequence->Text   = "";

    SeTemp->Value = g_AppCfg.Temp.AlarmValue;

    ChkAlarm->Checked       = g_AppCfg.Alarm.Enabled;
    ChkAlarmWindow->Checked = g_AppCfg.Alarm.Window;
    SeAlarmTimeOut->Value   = g_AppCfg.Alarm.TimeOut;

    ChkIncident->Checked     = g_AppCfg.Incident.Enabled;
    SeIncidentTimeOut->Value = g_AppCfg.Incident.TimeOut;

    ChkSaveForm->Checked   = g_AppCfg.bSaveForm;
    ChkAutoLogout->Checked = g_AppCfg.AutoLogout.Enabled;
    SeInterevalMin->Value  = g_AppCfg.AutoLogout.IntervalMin;
}
//---------------------------------------------------------------------------

void __fastcall TCFG0000M::BtnCloseClick(TObject *Sender)
{
    Close();    
}
//---------------------------------------------------------------------------

void __fastcall TCFG0000M::BtnSaveClick(TObject *Sender)
{
    //bool   bSaved = false;
    String sMsgString;
    String sID, sIPAddress;
    int sMsgType = MB_OK|MB_ICONWARNING|MB_APPLMODAL;

    sMsgString = lblQry->Caption;//"시스템 환경설정 정보를 저장하시겠습니까?";

    sMsgType = MB_YESNO|MB_ICONINFORMATION|MB_APPLMODAL;
    if (Application->MessageBox(sMsgString.c_str(), FTitle.c_str(), sMsgType)==IDNO)
    {
        return;
    }
    
    String sCenterId   = EdCenterId->Text.Trim();
    String sCenterName = EdCenterName->Text.Trim();
    String sGisFile    = EdGisFile->Text.Trim();
    String sDbProvider = EdDbProvider->Text.Trim();
    String sDbUser     = EdDbUser->Text.Trim();
    String sDbSource   = EdDbSource->Text.Trim();
    String sDbPasswd   = EdDbPasswd->Text.Trim();
    String sProcessId  = EdProcessId->Text.Trim();
    g_AppCfg.bSaveForm = ChkSaveForm->Checked;
    String sSaveForm   = g_AppCfg.bSaveForm ? "1" : "0";
    //String sTaskId     = EdTaskId->Text.Trim();
    //String sNodeId     = EdNodeId->Text.Trim();
    //String sSequence   = EdSequence->Text.Trim();

    g_AppCfg.Temp.AlarmValue = SeTemp->Value;

    g_AppCfg.Alarm.Enabled = ChkAlarm->Checked;
    g_AppCfg.Alarm.Window  = ChkAlarmWindow->Checked;
    g_AppCfg.Alarm.TimeOut = SeAlarmTimeOut->Value;
    String sAlarm        = g_AppCfg.Alarm.Enabled ? "1" : "0";
    String sAlarmWindow  = g_AppCfg.Alarm.Window  ? "1" : "0";
    String sAlarmTimeOut = String(g_AppCfg.Alarm.TimeOut);

    g_AppCfg.Incident.Enabled = ChkIncident->Checked;
    g_AppCfg.Incident.TimeOut = SeIncidentTimeOut->Value;
    String sIncident        = g_AppCfg.Incident.Enabled ? "1" : "0";
    String sIncidentTimeOut = String(g_AppCfg.Incident.TimeOut);

    g_AppCfg.AutoLogout.Enabled = ChkAutoLogout->Checked;
    g_AppCfg.AutoLogout.IntervalMin = SeInterevalMin->Value;
    String sAutoLogout = g_AppCfg.AutoLogout.Enabled ? "1" : "0";
    String sInterMin   = String(g_AppCfg.AutoLogout.IntervalMin);
    try {
        SetConfigValue("CENTER", "CENTERID",   sCenterId);
        SetConfigValue("CENTER", "CENTERNAME", sCenterName);

        //SetConfigValue("MAP",   "MAPFILEL0",  sGisFile);
        
        SetConfigValue("ITSDB", "PROVIDER",   sDbProvider);
        SetConfigValue("ITSDB", "SERVERNAME", sDbSource);
        SetConfigValue("ITSDB", "USERNAME",   sDbUser);
        SetConfigValue("ITSDB", "PASSWORD",   sDbPasswd);

        SetConfigValue("TEMPERATURE", "ALARMVALUE", String(g_AppCfg.Temp.AlarmValue));

        SetConfigValue("ALARM", "ENABLED", sAlarm);
        SetConfigValue("ALARM", "WINDOW",  sAlarmWindow);
        SetConfigValue("ALARM", "TIMEOUT", sAlarmTimeOut);

        SetConfigValue("INCIDENT", "ENABLED", sIncident);
        SetConfigValue("INCIDENT", "TIMEOUT", sIncidentTimeOut);

        SetConfigValue("AUTOLOGOUT", "ENABLED",     sAutoLogout);
        SetConfigValue("AUTOLOGOUT", "INTERVALMIN", sInterMin);

        SetConfigValue("APPLICATION", "PROCESSID", sProcessId);
        SetConfigValue("APPLICATION", "SAVEFORM",  sSaveForm);

        CenterId   = sCenterId;
        CenterName = sCenterName;
        CenterCd   = String(StrToInt(CenterId.SubString(2, 2)));

        //g_GisInfo.sMapFileNameL0   = sGisFile;

        g_AppCfg.itsdb.sProvider   = sDbProvider;
        g_AppCfg.itsdb.sUserName   = sDbUser;
        g_AppCfg.itsdb.sServerName = sDbSource;
        g_AppCfg.itsdb.sPassword   = sDbPasswd;
        g_AppCfg.sProcessId        = sProcessId;

        Application->MessageBox(lblQryRes->Caption.c_str(),//L"환경설정 정보를 정상적으로 저장하였습니다.\r\일부 환경설정은 다음 프로그램 실행 부터 반영됩니다.",
                                FTitle.c_str(), MB_OK|MB_ICONINFORMATION|MB_APPLMODAL);

        CMM_SetFormPosition(g_AppCfg.bSaveForm);
    } catch(...) {
    }
    
    //Close();
}
//---------------------------------------------------------------------------

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

String __fastcall TCFG0000M::GetConfigValue(String sSection, String sIdent, String sDefVal)
{
    String    sResult = sDefVal;
    TIniFile *pIniFile  = NULL;

    String sCfgFile = ExtractFilePath(Application->ExeName) + "\\cfg\\" + FIniFile;

    try
    {
        pIniFile = new TIniFile(sCfgFile);
        sResult = pIniFile->ReadString(sSection, sIdent, sDefVal);
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }
    return sResult;
}
//---------------------------------------------------------------------------

void __fastcall TCFG0000M::SetConfigValue(String sSection, String sIdent, String sSetVal)
{
    TIniFile *pIniFile  = NULL;

    String sCfgFile = ExtractFilePath(Application->ExeName) + "\\cfg\\" + FIniFile;

    try
    {
        pIniFile = new TIniFile(sCfgFile);
        pIniFile->WriteString(sSection, sIdent, sSetVal);
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }
}
//---------------------------------------------------------------------------

void __fastcall TCFG0000M::EdGisFilePropertiesButtonClick(TObject *Sender, int AButtonIndex)
{
    try {
        OpenDialog1->FileName = EdGisFile->Text.Trim();
        OpenDialog1->InitialDir = ExtractFilePath(Application->ExeName) + "\\cfg\\";
        OpenDialog1->HistoryList->Clear();
        OpenDialog1->Filter = "지도설정파일 (*.DVA)|*.DVA";
        //OpenDialog1->Options = dlgOpen->Options << ofFileMustExist;
        //OpenDialog1->Options = dlgOpen->Options >> ofAllowMultiSelect;

        if (!OpenDialog1->Execute()) return;

        EdGisFile->Text = OpenDialog1->FileName;
    } catch(...) {}
}
//---------------------------------------------------------------------------

