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

#pragma hdrstop

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

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

#pragma package(smart_init)

//---------------------------------------------------------------------------
TMonitoringScreenManager *ObjScreenManager = NULL;
//---------------------------------------------------------------------------

TMonitoringScreenManager::TMonitoringScreenManager()
{
}
//---------------------------------------------------------------------------
TMonitoringScreenManager::~TMonitoringScreenManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------
void TMonitoringScreenManager::Init()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TMonitoringCtlrManager *ObjCtlrManager = NULL;
//---------------------------------------------------------------------------

TMonitoringCtlrManager::TMonitoringCtlrManager(int AMonitoringType)
{
    String sAppName = ChangeFileExt(ExtractFileName(Application->ExeName), ".ini");
    String sCfgDir  = ExtractFilePath(Application->ExeName) + "Cfg\\";
    FCfgFile = sCfgDir + sAppName;
    FMonitoringType = AMonitoringType;
}
//---------------------------------------------------------------------------
TMonitoringCtlrManager::~TMonitoringCtlrManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

bool TMonitoringCtlrManager::LoadCtlrFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    try
    {
        FLists.Lock();
        FLists.RemoveAll();
    }
    __finally
    {
        FLists.UnLock();
    }

    TIniFile *pIniFile = NULL;
    pIniFile = new TIniFile(FCfgFile);
    try
    {
        FLists.Lock();
        try
        {
            String item;
            for(int ii = 1; ii <= 200; ii++)
            {
                item.printf(L"%03d", ii);
                String info = pIniFile->ReadString("CAMERA", item, "").Trim();
                if (info == "") {
                    continue;
                }
                TStringList *pStringList = new TStringList;
                try {
                    try {
                        pStringList->CommaText = info;
                        if (pStringList->Count < 5) {
                            continue;
                        }

                        TMonitoringCtlr *pObj = new TMonitoringCtlr();
                        pObj->CTLR_NMBR = pStringList->Strings[0];
                        pObj->ID        = pStringList->Strings[1];
                        pObj->CTLR_ID   = pStringList->Strings[1];
                        pObj->NAME      = pStringList->Strings[2];
                        pObj->CTLR_IP   = pStringList->Strings[3];
                        pObj->STRM_ADDR = pStringList->Strings[4];
                        pObj->FULL_STRM_ADDR = pObj->STRM_ADDR;
                        if (pStringList->Count > 5) {
                            pObj->FULL_STRM_ADDR = pStringList->Strings[5];
                        }

                        FLists.Push(pObj->CTLR_NMBR, pObj);
                    } catch(Exception &e) {
                    }
                } __finally {
                    if (pStringList) delete pStringList;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        FLists.UnLock();
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }

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

bool TMonitoringCtlrManager::InitMonitoringFormFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    TIniFile *pIniFile = NULL;
    pIniFile = new TIniFile(FCfgFile);

    try {
        String item;
        int COUNT = 0;
        for (int ii = 1; ii <= MAX_CAMERA_SCREEM; ii++) {
            item.printf(L"%03d", ii);
            String MONITORING_NM = pIniFile->ReadString("MONITORING", item, "").Trim();
            if (MONITORING_NM != "") {
                COUNT++;
                break;
            }
        }

        if (COUNT <= 0) {
            // 기본 모니터링 화면 생성
            String section = "기본";
            pIniFile->WriteString("MONITORING", "COUNT", "1");
            pIniFile->WriteString("MONITORING", "001", section);

            int nFormCnt = 0;
            FOR_STL(TMonitoringCtlr*, pObj, FLists)
            {
                nFormCnt++;
                item.printf(L"%03d", nFormCnt);
                pIniFile->WriteString(section, item, pObj->CTLR_NMBR);
                if (nFormCnt == 16) break;
            }
        }
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }

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

bool TMonitoringCtlrManager::LoadMonitoringFormFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    if (ObjScreenManager == NULL)
    {
        ObjScreenManager = new TMonitoringScreenManager();
    }
    ObjScreenManager->Init();

    TIniFile *pIniFile = NULL;
    pIniFile = new TIniFile(FCfgFile);

    try {
        String item;
        for (int nForms = 1; nForms <= MAX_CAMERA_SCREEM; nForms++) {
            item.printf(L"%03d", nForms);
            String MONITORING_NM = pIniFile->ReadString("MONITORING", item, "").Trim();
            if (MONITORING_NM == "") {
                continue;
            }

            String seq;
            for(int ii = 1; ii <= MAX_CAMERA_PER_SCREEM; ii++)
            {
                seq.printf(L"%03d", ii);
                String cameraId = pIniFile->ReadString(MONITORING_NM, seq, "").Trim();
                if (cameraId == "") {
                    continue;
                }

                TMonitoringScreen *pObj = ObjScreenManager->FLists.Find(MONITORING_NM);
                if (!pObj)
                {
                    pObj = new TMonitoringScreen();
                    pObj->SEQ  = ii;
                    pObj->NAME = MONITORING_NM;
                    ObjScreenManager->FLists.Push(pObj->NAME, pObj);
                }
                TMonitoringCtlr *pCamera = FLists.Find(cameraId);
                if (pCamera) {
                    TScreenCtlr *pScr = new TScreenCtlr();
                    pScr->SEQ       = pObj->FLists.Size()+1;
                    pScr->CTLR_NMBR = cameraId;
                    pScr->VIEW_MODE = 1;
                    pObj->FLists.Push(pScr->SEQ, pScr);
                }
            }
        }
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;

        FLists.UnLock();

        FOR_STL(TMonitoringScreen*, pObj, ObjScreenManager->FLists)
        {
            int nCtlrCount = pObj->FLists.Size();
            switch(nCtlrCount)
            {
            case  0: case  1:                   pObj->LAYOUT = 0; break;  //1*1
            case  2: case  3: case  4:          pObj->LAYOUT = 1; break;  //2*2
            case  5: case  6:                   pObj->LAYOUT = 3; break;  //3*2
            case  7: case  8: case  9:          pObj->LAYOUT = 2; break;  //3*3
            case 10: case 11: case 12:          pObj->LAYOUT = 4; break;  //4*3
            case 13: case 14: case 15: case 16: pObj->LAYOUT = 5; break;  //4*4
            case 17: case 18: case 19: case 20: pObj->LAYOUT = 7; break;  //5*4
            case 21: case 22: case 23: case 24: pObj->LAYOUT = 8; break;  //6*4
            case 25: case 26: case 27: case 28:
            case 29: case 30:                   pObj->LAYOUT = 9; break;  //6*5
            default:
                if      (nCtlrCount <= 42)      pObj->LAYOUT = 10;        //7*6
                else if (nCtlrCount <= 56)      pObj->LAYOUT = 11;        //8*7
                else                            pObj->LAYOUT = 12;        //8*8
                break;
            }
        }
    }

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

bool TMonitoringCtlrManager::DelMonitoringFormFromDb(String AMonitoringName, String ANewFormName, TADOConnection *ADbConn/*=NULL*/)
{
    String oldFormName = AMonitoringName.Trim();
    String newFormName = ANewFormName.Trim();

    TIniFile *pIniFile = NULL;
    pIniFile = new TIniFile(FCfgFile);

    String item;
    try {
        if (oldFormName != "") {
            // 기존 데이터 변경 또는 삭제
            // 화면에 속한 데이터 삭제
            pIniFile->EraseSection(oldFormName);

            if (newFormName != "") {
                // 기존 데이터 변경
                if (oldFormName == newFormName) {
                    // 이름이 변경되지 않아 처리할 게 없음.
                }
                else {
                    // 화면명칭 변경됨
                    TMonitoringScreen *pObj = ObjScreenManager->FLists.Find(oldFormName);
                    if (pObj) {
                        item.printf(L"%03d", pObj->SEQ);
                        pIniFile->WriteString("MONITORING", item, newFormName);
                    }
                }
            }
            else {
                // 기존 데이터 삭제
                //pIniFile->EraseSection("MONITORING");
                for (int ii = 1; ii <= MAX_CAMERA_SCREEM; ii++) {
                    item.printf(L"%03d", ii);
                    pIniFile->DeleteKey("MONITORING", item);
                }

                int nForms = 1;
                FOR_STL(TMonitoringScreen*, pScreen, ObjScreenManager->FLists) {
                    if (oldFormName == pScreen->NAME) {
                        continue;
                    }
                    item.printf(L"%03d", nForms++);
                    pIniFile->WriteString("MONITORING", item, pScreen->NAME);
                }
            }
        }
        else {
            if (newFormName != "") {
                // 신규화면 추가
                item.printf(L"%03d", ObjScreenManager->FLists.Size() + 1);
                pIniFile->WriteString("MONITORING", item, newFormName);
            }
        }
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }
    return true;
}
//---------------------------------------------------------------------------

bool TMonitoringCtlrManager::InsMonitoringFormFromDb(String AMonitoringName, int ASeq, String ACtlrNmbr, int AViewMode, TADOConnection *ADbConn/*=NULL*/)
{
    TIniFile *pIniFile = NULL;
    pIniFile = new TIniFile(FCfgFile);

    try {
        String item;
        item.printf(L"%03d", ASeq);
        pIniFile->WriteString(AMonitoringName, item, ACtlrNmbr);
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }
    return true;
}
//---------------------------------------------------------------------------

bool TMonitoringCtlrManager::SaveCameraInfo(int ASeq, String ANmbr, String AId, String ANm, String AIp, String AStream, String AFullStream/*=""*/)
{
    TIniFile *pIniFile = NULL;
    pIniFile = new TIniFile(FCfgFile);

    String item;
    String data;
    try {
        if (ASeq == 1) {
            pIniFile->EraseSection("CAMERA_INFO");
        }

        item.printf(L"%03d", ASeq);

        if (AFullStream == "")
            data.printf(L"%s,%s,%s,%s,%s", ANmbr, AId, ANm, AIp, AStream);
        else
            data.printf(L"%s,%s,%s,%s,%s,%s", ANmbr, AId, ANm, AIp, AStream, AFullStream);

        pIniFile->WriteString("CAMERA_INFO", item, data);
    }
    __finally
    {
        if (pIniFile) delete pIniFile;
        pIniFile = NULL;
    }
    return true;
}
//---------------------------------------------------------------------------

