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

#pragma hdrstop

#include "AppGlobalF.h"
#include "CDSMonitoringObjF.h"

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

#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)
{
    FMonitoringType = AMonitoringType;
}
//---------------------------------------------------------------------------
TMonitoringCtlrManager::~TMonitoringCtlrManager()
{
    FLists.RemoveAll();
}
//---------------------------------------------------------------------------

bool TMonitoringCtlrManager::LoadCtlrFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String apiUri = g_AppCfg.sRestApiUri + "/api/vms/manager/info/list";
    TMcJsonItem *ObjList = NULL;

    try {
        FLists.Lock();
        FLists.RemoveAll();
    }
    __finally {
        FLists.UnLock();
    }

    try {
        FLists.Lock();

        ObjList = TRestObjectManager::RequestGet(apiUri);
        if (ObjList == NULL) {
            return false;
        }

        for (int ii = 0; ii < ObjList->Count; ii++) {
            String sDelYn = TRestObjectManager::GetValue(ObjList->Items[ii], "del_yn");
            if (sDelYn != "N") {
                continue;
            }

            TMonitoringCtlr *pObj = new TMonitoringCtlr();

            pObj->CTLR_NMBR = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_nmbr").ToIntDef(0);
            pObj->ID        = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_id");
            pObj->CTLR_ID   = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_id");
            pObj->NAME      = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_nm");
            pObj->CTLR_IP   = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_ip");
            pObj->INFO1     = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_type_desc");

            pObj->STRM_IP   = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_ip");
            pObj->STRM_ID   = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_id");
            pObj->STRM_PSWD = TRestObjectManager::GetValue(ObjList->Items[ii], "web_cmra_pwd");
            pObj->STRM_ADDR = TRestObjectManager::GetValue(ObjList->Items[ii], "strm_sesn_nm");
            pObj->FULL_STRM_ADDR = TRestObjectManager::GetValue(ObjList->Items[ii], "strm_sesn_nm");
            if (pObj->FULL_STRM_ADDR == "")
            {
                pObj->FULL_STRM_ADDR = pObj->STRM_ADDR;
            }

            FLists.Push(pObj->CTLR_NMBR, pObj);
        }
    } __finally {
        FLists.UnLock();
        if (ObjList) delete ObjList;
    }
    return true;
}
//---------------------------------------------------------------------------
bool TMonitoringCtlrManager::LoadMonitoringFormFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

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

    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/common/monitoring/list/" + String(FMonitoringType);
    TMcJsonItem *ObjList = NULL;

    try {
        FLists.Lock();

        ObjList = TRestObjectManager::RequestGet(apiUri);
        if (ObjList == NULL) {
            return false;
        }

        for (int ii = 0; ii < ObjList->Count; ii++) {
            String MONITORING_NM  = TRestObjectManager::GetValue(ObjList->Items[ii], "monitoring_nm");
            int    MONITORING_SEQ = TRestObjectManager::GetValue(ObjList->Items[ii], "monitoring_seq").ToIntDef(0);
            int    VMS_CTLR_NMBR  = TRestObjectManager::GetValue(ObjList->Items[ii], "vms_ctlr_nmbr").ToIntDef(0);
            int    VIEW_MODE      = 0;

            TMonitoringScreen *pObj = ObjScreenManager->FLists.Find(MONITORING_NM);
            if (!pObj)
            {
                pObj = new TMonitoringScreen();
                pObj->NAME = MONITORING_NM;
                ObjScreenManager->FLists.Push(pObj->NAME, pObj);
            }
            TScreenCtlr *pScr = new TScreenCtlr();
            pScr->SEQ       = pObj->FLists.Size()+1;
            pScr->CTLR_NMBR = VMS_CTLR_NMBR;
            pScr->VIEW_MODE = VIEW_MODE;

            pObj->FLists.Push(pScr->SEQ, pScr);
        }
    } __finally {
        FLists.UnLock();
        if (ObjList) delete ObjList;

        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, TADOConnection *ADbConn/*=NULL*/)
{
    bool result = false;
    String resultVal;
    String key = "abcdefg1234567890x";
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/common/monitoring/delete/" + key + "/" + String(FMonitoringType);
    TMcJsonItem *ObjList = new TMcJsonItem();

    try {
        ObjList->Add("monitoring_type")->AsInteger = FMonitoringType;
        ObjList->Add("monitoring_nm")->AsString    = AMonitoringName;
        ObjList->Add("monitoringSeq")->AsInteger   = 0;
        ObjList->Add("vms_ctlr_nmbr")->AsInteger   = 0;

        result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }

    } __finally {
    }

    return result;
}
//---------------------------------------------------------------------------

bool TMonitoringCtlrManager::InsMonitoringFormFromDb(String AMonitoringName, int ASeq, String ACtlrNmbr, int AViewMode, TADOConnection *ADbConn/*=NULL*/)
{
    bool result = false;
    String resultVal;
    String key = "abcdefg1234567890x";
    String apiUri = g_AppCfg.sRestApiUri + "/cs-api/vms/common/monitoring/insert/" + key + "/" + String(FMonitoringType);
    TMcJsonItem *ObjList = new TMcJsonItem();

    try {
        ObjList->Add("monitoring_type")->AsInteger = FMonitoringType;
        ObjList->Add("monitoring_nm")->AsString    = AMonitoringName;
        ObjList->Add("monitoringSeq")->AsInteger   = ASeq;
        ObjList->Add("vms_ctlr_nmbr")->AsInteger   = ACtlrNmbr.ToIntDef(0);

        result = TRestObjectManager::RequestPost(apiUri, ObjList->AsJSON, resultVal);
        if (!result) {
            return false;
        }

    } __finally {
    }

    return result;
}
//---------------------------------------------------------------------------

