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

#pragma hdrstop

#include "CDSCctvF.h"
#include "AppGlobalF.h"

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

#pragma package(smart_init)

TXCamera::TXCamera()
{
}
//---------------------------------------------------------------------------
TXCamera::~TXCamera()
{
}
//---------------------------------------------------------------------------


TXView::TXView()
{
    FForm = NULL;
    FCount= 0;
}
//---------------------------------------------------------------------------
TXView::~TXView()
{
}
//---------------------------------------------------------------------------
void TXView::InitCamera()
{
    for (int kk = 0; kk < MAX_VMS_CAMERA; kk++)
    {
        FCamera[kk].Installed = false;
        FCamera[kk].Id        = "";
        FCamera[kk].Play      = true;
    }
}
//---------------------------------------------------------------------------


//---------------------------------------------------------------------------
TXViewManager *XViewManager = NULL;
//---------------------------------------------------------------------------

/*
* XCctv Manager
*/
TXViewManager::TXViewManager()
{
}
//---------------------------------------------------------------------------
TXViewManager::~TXViewManager()
{
}
//---------------------------------------------------------------------------
bool TXViewManager::LoadFromDb(TADOConnection *ADbConn/*=NULL*/)
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT MONITORING_NM, MONITORING_SEQ, VMS_CTLR_NMBR  \r\n"
           "  FROM TB_VMS_MONITORING                             \r\n"
           " WHERE MONITORING_TYPE = 3                           \r\n"
           " ORDER BY MONITORING_NM DESC, MONITORING_SEQ         \r\n";

    TXView *pView = NULL;

    try
    {
        FLists.Lock();
        FLists.RemoveAll();

        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = (NULL != ADbConn) ? ADbConn : ITSDb_GetConnection();
			pADO->SQL->Clear();
			pADO->SQL->Text = sQry;

			pADO->Open();
            //int nRows = pADO->RecordCount;
            for( ; !pADO->Eof; pADO->Next())
            {
                String sScrName = pADO->FieldByName("MONITORING_NM")->AsString;
                String sScrSeq  = pADO->FieldByName("MONITORING_SEQ")->AsString;
                String sVmsNmbr = pADO->FieldByName("VMS_CTLR_NMBR")->AsString;

                pView = FLists.Find(sScrName);
                if (!pView)
                {
                    pView = new TXView();

                    pView->Id       = sScrName;
                    pView->Name     = sScrName;
                    pView->Selected = false;

                    FLists.Push(pView->Id, pView);
                }
                if (pView->FCount < MAX_VMS_CAMERA)
                {
                    TXCamera *pCam = &pView->FCamera[pView->FCount++];
                    pCam->Installed = true;
                    pCam->Id        = sVmsNmbr;
                    pCam->Play      = true;
                }
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS영상모니터링화면보조회", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &exception)
        {
            DBERRORMSG("VMS영상모니터링화면보조회", String(exception.ClassName()), exception.Message, sQry);
            throw Exception(String(exception.ClassName()) + exception.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }

        FOR_STL(TXView*, pObj, FLists)
        {
            switch(pObj->FCount)
            {
            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      (pObj->FCount <= 42)  pObj->Layout = 10; //7*6
                else if (pObj->FCount <= 56)  pObj->Layout = 11; //8*7
                else                          pObj->Layout = 12; //8*8
                break;
            }
        }

        FLists.UnLock();
    }

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

