/****************************************************************************
 * @source      : 
 * @description : 
 ****************************************************************************
 * DATE         AUTHOR    DESCRIPTION
 * --------------------------------------------------------------------------
 * 2016/07/24   정승호    최초작성
 *
 ****************************************************************************/
//---------------------------------------------------------------------------

#include <vcl.h>
#include "AppGlobalF.h"
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "FrmVmsOprMainF.h"
#include "CDSCctvF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include "VMS0400MF.h"
#include "VMSVIEWMF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSplitter"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "cxClasses"
#pragma link "cxCustomData"
#pragma link "cxData"
#pragma link "cxDataStorage"
#pragma link "cxFilter"
#pragma link "cxGrid"
#pragma link "cxGridCustomTableView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxGridTableView"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxGridCardView"
#pragma link "cxGridCustomLayoutView"
#pragma link "cxCheckBox"
#pragma link "cxDropDownEdit"
#pragma link "cxImage"
#pragma link "cxButtons"
#pragma link "FRAME_VmsStateF"
#pragma link "FRAME_VmsMsgListF"
#pragma resource "*.dfm"
TVMS0400M *VMS0400M = NULL;

//---------------------------------------------------------------------------
__fastcall TVMS0400M::TVMS0400M(TComponent* Owner)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);
    LoadLocalSkin();

    if (!XViewManager)
    {
        XViewManager = new TXViewManager();
    }

    FMonitoringType = enMonitoringCamera;
    TsList01->TabVisible = false;

    if (FormStyle != fsMDIChild) CMM_LoadForm(g_sFormsDir, this);

	FListForm = new TList;
    FSelVmsObj= NULL;
    FSelForm  = NULL;

    PnlInfoRight->Width = 370;
    PnlInfoRight->Visible = false;
    SplitterLeft->Visible = false;

    //PgVmsState->Properties->HideTabs = true;
    //PgVmsIfscTraf->Properties->HideTabs = true;

    IsFormResizing = false;
    FOldWidth = Width;
    FOldHeight= Height;
    FCurrPage = NULL;
    PgFormTab->OnPageChanging = NULL;

    FReady = false;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::FormResize(TObject *Sender)
{
    if (IsFormResizing == false)
    {
        RecalFormSize();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::WMEnterSizeMove(TMessage &Msg)
{
    IsFormResizing = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::WMExitSizeMove(TMessage &Msg)
{
    IsFormResizing = false;
    RecalFormSize();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::RecalFormSize()
{
    int nNewW = PgFormTab->Width;
    int nNewH = PgFormTab->Height;

    if (nNewW == FOldWidth && nNewH == FOldHeight)
    {
        return;
    }

    Application->ProcessMessages();
    FOldWidth = nNewW;
    FOldHeight= nNewH;
    try
    {
        for (int ii = 0 ; ii < PgFormTab->PageCount; ii++)
        {
            TcxTabSheet *pSheet = PgFormTab->Pages[ii];
            if (!pSheet) continue;

            TXView *pObj = (TXView*)pSheet->Tag;
            pObj->FForm->Left = 0;
            pObj->FForm->Top  = 0;
            pObj->FForm->Width = pSheet->Width;
            pObj->FForm->Height= pSheet->Height;

            pObj->FForm->RecalFormSize();
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::CommClose()
{
    try
    {
        SAFE_DELETE(FListForm);

        if (FormStyle != fsMDIChild) CMM_SaveForm(g_sFormsDir, this);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::FormCreate(TObject *Sender)
{
    InitMonitoringForm();
    XViewManager->LoadFromDb();
    CreateViewList();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::CreateViewList()
{
    FReady = false;
    try
    {
        LockWindowUpdate(Handle);

        for (int ii = PgFormTab->PageCount-1; ii >= 0 ; ii--)
        {
            TcxTabSheet *pSheet = PgFormTab->Pages[ii];
            if (pSheet)
            {
                delete pSheet;
            }
        }

        if (XViewManager->FLists.Size() == 0)
        {
            TXView *pObj = new TXView();

            pObj->Id     = 0;
            pObj->Name   = " Empty ";
            pObj->Layout = 1;
            pObj->Selected = 0;

            XViewManager->FLists.Push(pObj->Id, pObj);
            pObj->InitCamera();
        }

        int nActivePage = 0;
        FOR_STL(TXView*, pObj, XViewManager->FLists)
        {
            TcxTabSheet *pSheet = new TcxTabSheet(PgFormTab);
            pSheet->PageControl = PgFormTab;
            pSheet->Caption = " " + pObj->Name + " ";
            if (pObj->Selected >= 0)
                nActivePage = pObj->Selected;

            pSheet->Tag = (int)pObj;

            pObj->FForm = new TVMS0410M(this, Handle, pSheet, pObj);
            pObj->FForm->FView  = pObj;
            pObj->FForm->Parent = pSheet;
            pObj->FForm->FSheet = pSheet;
            pObj->FForm->Color = this->Color;

            pObj->FForm->InitCamera();

            pObj->FForm->Left = 0;
            pObj->FForm->Top  = 0;
            pObj->FForm->Width = pSheet->Width;
            pObj->FForm->Height= pSheet->Height;
            pObj->FForm->Show();

            pObj->FForm->Layout  = pObj->Layout;
        }

        PgFormTab->ActivePageIndex = nActivePage;

        FCurrPage = PgFormTab->ActivePage;
        TXView *pObj = (TXView*)FCurrPage->Tag;
        pObj->FForm->Play();
        PgFormTab->OnPageChanging = PgFormTabPageChanging;
    }
    __finally
    {
        LockWindowUpdate(0);
    }
    FReady = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::FormShow(TObject *Sender)
{
    Refresh();
    if (PgFormTab->PageCount == 1)
        PgFormTab->ActivePageIndex = 0;
    else
        PgFormTab->ActivePageIndex = 1;

    Application->ProcessMessages();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::FormActivate(TObject *Sender)
{
    ActivateForm(true);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::FormDeactivate(TObject *Sender)
{
    ActivateForm(false);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::LoadLocalSkin()
{
}
//---------------------------------------------------------------------------

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

void __fastcall TVMS0400M::FormClose(TObject *Sender, TCloseAction &Action)
{
    try {
        CommClose();

        VMS0400M = NULL;
        Action = caFree;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::OnMainFormMessage(TMessage &Msg)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::OnVmsStateRefreshMessage(TMessage &Msg)
{
    for(int ii = 0; ii < FListForm->Count; ii++)
    {
        TVMS0410M *pForm = (TVMS0410M*)FListForm->Items[ii];
        //pForm->RefreshVmsStatus();
    }

    FRAMEVmsState1->SelVmsObj = FSelVmsObj;

    if (FSelVmsObj)
    {
        FRAMEVmsMsgList1->LoadVmsMsgScenario(String(FSelVmsObj->VMS_CTLR_NMBR));
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::OnVmsFormRefreshMessage(TMessage &Msg)
{
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::OnVmsSelectMessage(TMessage &Msg)
{
    try {
        FSelVmsObj = (TVmsCtlr*)Msg.WParam;
        FRAMEVmsState1->SelVmsObj = FSelVmsObj;

        if (!FSelVmsObj) return;

        TVMSWCAM0 *pSelForm = (TVMSWCAM0*)Msg.LParam;
        if (pSelForm == FSelForm) return;

        if (FSelForm) FSelForm->SelectForm(false);

        FSelForm = pSelForm;
        if (FSelForm) FSelForm->SelectForm(true);

        FRAMEVmsMsgList1->LoadVmsMsgScenario(String(FSelVmsObj->VMS_CTLR_NMBR));
    } catch(...) {};
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::BtnMonitoringMngrClick(TObject *Sender)
{
    TVMSVIEWM *pForm = new TVMSVIEWM(this);
    pForm->FMonitoringType = FMonitoringType;
    pForm->ShowModal();
    bool bUpdate = pForm->FUpdate;
    SAFE_DELETE(pForm);

    if (bUpdate)
    {
        if (FSelForm) FSelForm->SelectForm(false);
        FSelVmsObj = NULL;
        FSelForm   = NULL;

        FRAMEVmsState1->SelVmsObj = FSelVmsObj;
        FRAMEVmsMsgList1->ClearVmsMsg();

        XViewManager->LoadFromDb();
        CreateViewList();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::InitMonitoringForm()
{
    String sQry;
    TADOQuery *pADO = NULL;

    sQry = "SELECT COUNT(1) AS CNT          \r\n"
           "  FROM TB_VMS_MONITORING        \r\n"
           " WHERE MONITORING_TYPE = :p01   \r\n";

    try
    {
        int nString(0), nFigure(0), nFormSeq(0);

        VmsManager->FLists.Lock();
        try
        {
            pADO = new TADOQuery(NULL);
			pADO->Close();
            pADO->Connection = ITSDb_GetConnection();

            ITSDb_SQLText(pADO, sQry);
            ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
            ITSDb_SQLOpen(pADO);
            int nFormCnt = pADO->FieldByName("CNT")->AsInteger;
            if (nFormCnt > 0) return;

    sQry = "INSERT INTO TB_VMS_MONITORING (     \r\n"
           "                MONITORING_TYPE,    \r\n"
           "                MONITORING_NM,      \r\n"
           "                MONITORING_SEQ,     \r\n"
           "                VMS_CTLR_NMBR       \r\n"
           "                )                   \r\n"
           "  VALUES (                          \r\n"
           "                :p01,               \r\n"
           "                :p02,               \r\n"
           "                :p03,               \r\n"
           "                :p04                \r\n"
           "                )                   \r\n";


            ITSDb_SQLText(pADO, sQry);

            FOR_STL(TVmsCtlr*, pObj, VmsManager->FLists)
            {
                if (pObj->DEL_YN == "Y") continue;

                String sFormName = "VMS 문자식";
                if (pObj->VMS_USAG_TYPE_CD == "VUTN")   //문자식
                {
                    nFormSeq = ++nString;
                }
                else
                if (pObj->VMS_USAG_TYPE_CD == "VUTP")   //도형식
                {
                    sFormName = "VMS 도형식";
                    nFormSeq = ++nFigure;
                }
                else continue;

                ITSDb_SQLBind(pADO, "p01", String(FMonitoringType));
                ITSDb_SQLBind(pADO, "p02", sFormName);
                ITSDb_SQLBind(pADO, "p03", String(nFormSeq));
                ITSDb_SQLBind(pADO, "p04", String(pObj->VMS_CTLR_NMBR));

                ITSDb_SQLExec(pADO);
            }
        }
        catch(EDatabaseError &E)
        {
            DBERRORMSG("VMS Cam Monitoring screen query", String(E.ClassName()), E.Message, sQry);
            throw Exception(String(E.ClassName()) + E.Message);
        }
        catch(Exception &e)
        {
            DBERRORMSG("VMS Cam Monitoring screen query", String(e.ClassName()), e.Message, sQry);
            throw Exception(String(e.ClassName()) + e.Message);
        }
    }
    __finally
    {
        if (pADO)
        {
            pADO->Close();
            delete pADO;
        }
        VmsManager->FLists.UnLock();
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::ActivateForm(bool AActivate)
{
    try
    {
        int nTabIndex = PgFormTab->ActivePageIndex;
//AnsiString sTrace = "TVMS0400M: ActivateForm:ActivePageIndex = " + String(nTabIndex) + "  " + String(PgFormTab->PageCount);
//ITSUtil_Trace(sTrace.c_str());

        nTabIndex--;    //ActivePageIndex 으로 구간값은 숨어있는 탭을 포함한 인덱스
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMS0410M *pForm = (TVMS0410M*)FListForm->Items[ii];
#if 0
            if (AActivate)
            {
                if (ii == nTabIndex) pForm->Play = true;
                else                 pForm->Play = false;
            }
            else
            {
                //비활성이면 모든 화면 비활성으로
                pForm->Play = false;
            }
#endif
        }
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::PgFormTabPageChanging(TObject *Sender, TcxTabSheet *NewPage, bool &AllowChange)
{
    if (!FReady) return;

    if (FCurrPage)
    {
        TXView *pObj = (TXView*)FCurrPage->Tag;
        pObj->FForm->Stop();
    }
    FCurrPage = NewPage;
    TXView *pObj = (TXView*)FCurrPage->Tag;
    pObj->FForm->Play();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::MnuEditScreenClick(TObject *Sender)
{
    BtnMonitoringMngrClick(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::MnuViewMsgClick(TObject *Sender)
{
    if (MnuViewMsg->Tag == 0)
    {
        MnuViewMsg->Tag = 1;
        MnuViewMsg->Caption = "표출메시지 닫기";
        PnlInfoRight->Visible = true;
        SplitterLeft->Visible = false;
    }
    else
    {
        MnuViewMsg->Tag = 0;
        MnuViewMsg->Caption = "표출메시지 보기";
        PnlInfoRight->Visible = false;
        SplitterLeft->Visible = false;
    }
    RecalFormSize();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0400M::SplitterLeftAfterClose(TObject *Sender)
{
    MnuViewMsg->Tag = 0;
    MnuViewMsg->Caption = "표출메시지 보기";
    PnlInfoRight->Visible = false;
    SplitterLeft->Visible = false;
}
//---------------------------------------------------------------------------

