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

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

#include "VMS0410MF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxControls"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma resource "*.dfm"
//TVMS0410M *VMS0410M;
//---------------------------------------------------------------------------
__fastcall TVMS0410M::TVMS0410M(TComponent* Owner, HWND AHandle, TcxTabSheet *ASheet, TXView *AObj)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load(this);

    FLayout = -1;
    FSheet  = ASheet;
    FView   = AObj;
    FWndParent = AHandle;

    Caption = ASheet->Caption;
    for (int ii = 0; ii < MAX_VMS_CAMERA; ii++)
    {
        FFrmCamera[ii] = new TVMSWCAM0(this, NULL);
        FFrmCamera[ii]->Parent = PnlClient;
        FFrmCamera[ii]->FTitle = ASheet->Caption;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0410M::SetLayout(int ALayout)
{
    if (FLayout == ALayout) return;

    switch(ALayout)
    {
    case  0:/* (1 * 1) */ FCols = 1; FRows = 1; break;
    case  1:/* (2 * 2) */ FCols = 2; FRows = 2; break;
    case  2:/* (3 * 3) */ FCols = 3; FRows = 3; break;
    case  3:/* (3 * 2) */ FCols = 3; FRows = 2; break;
    case  4:/* (4 * 3) */ FCols = 4; FRows = 3; break;
    case  5:/* (4 * 4) */ FCols = 4; FRows = 4; break;
    case  6:/* (4 * 5) */ FCols = 4; FRows = 5; break;
    case  7:/* (5 * 4) */ FCols = 5; FRows = 4; break;
    case  8:/* (6 * 4) */ FCols = 6; FRows = 4; break;
    case  9:/* (6 * 5) */ FCols = 6; FRows = 5; break;
    case 10:/* (7 * 6) */ FCols = 7; FRows = 6; break;
    case 11:/* (8 * 7) */ FCols = 8; FRows = 7; break;
    case 12:/* (8 * 8) */ FCols = 8; FRows = 8; break;
    default: ALayout = 0; FCols = 1; FRows = 1; break;
    }
    FLayout = ALayout;
    if (FSheet)
    {
        TXView *pObj = (TXView*)FSheet->Tag;
        if (pObj) pObj->Layout = FLayout;
    }
    RecalFormSize();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0410M::RecalFormSize()
{
    int ii;
    int nMaxForm = FCols * FRows;

    for (ii = nMaxForm; ii < MAX_VMS_CAMERA; ii++)
    {
        TVMSWCAM0 *pFrmCam = FFrmCamera[ii];
        if (pFrmCam->Visible)
        {
            pFrmCam->CamStop();
            if (pFrmCam->Showing)
            {
                pFrmCam->TmrStop->Enabled = true;
                pFrmCam->Hide();
            }
        }
    }

    int nRows = 0;
    int nCols = 0;
    int nTop  = 0;
    int nLeft = 0;
    int nTotTop, nTotLeft;

    int nWidth = (PnlClient->Width - FCols+1) / FCols;
    int nHeight= (PnlClient->Height- FRows+1) / FRows;

    try
    {
        LockWindowUpdate(Handle);

        for (ii = 0; ii < nMaxForm; ii++)
        {
            TVMSWCAM0 *pFrmCam = FFrmCamera[ii];
            if (!pFrmCam)
                continue;

            pFrmCam->Width = nWidth;
            pFrmCam->Height= nHeight;

            nTotTop = (nRows * pFrmCam->Height)+(nTop*(nRows+1));
            nTotLeft = (nCols * pFrmCam->Width)+(nLeft*(nCols+1));
            pFrmCam->Top = nTotTop;
            pFrmCam->Left= nTotLeft;

            if ((ii%FCols) == (FCols-1))
            {
                nRows++;
                nCols = 0;
            }
            else
            {
                nCols++;
            }

            if (!pFrmCam->Showing)
            {
                pFrmCam->FWndParent = Handle;
                pFrmCam->Show();
                //pFrmCam->TmrPlay->Enabled = true;
            }
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0410M::InitCamera()
{
    if (!FView) return;

    for (int ii = 0; ii < MAX_VMS_CAMERA; ii++)
    {
        if (!FView->FCamera[ii].Installed) continue;

        TVmsCtlr *pVms = VmsManager->FLists.Find(FView->FCamera[ii].Id);
        if (!pVms) continue;

        FFrmCamera[ii]->InitCamera((void*)pVms, FView->FCamera[ii].Play);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0410M::UpdateCctvMasterInfo()
{
    for (int ii = 0; ii < MAX_VMS_CAMERA; ii++)
    {
        //FFrmCamera[ii]->UpdateCctvMasterInfo();
    }
}
//---------------------------------------------------------------------------
void __fastcall TVMS0410M::Play()
{
    for (int ii = 0; ii < MAX_VMS_CAMERA; ii++)
    {
        if (FFrmCamera[ii]->FAutoPlay)
        {
            FFrmCamera[ii]->TmrPlay->Enabled = true;
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TVMS0410M::Stop()
{
    for (int ii = 0; ii < MAX_VMS_CAMERA; ii++)
    {
        FFrmCamera[ii]->TmrStop->Enabled = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0410M::OnVmsSelectMessage(TMessage &Msg)
{
    if (FWndParent)
    {
        POST_MSG(FWndParent, WM_VMS_SELECT, (WPARAM)Msg.WParam, (LPARAM)Msg.LParam);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0410M::OnVmsSelectDblClickMessage(TMessage &Msg)
{
    if (FWndParent)
    {
        POST_MSG(FWndParent, WM_VMS_SELECT_DBLCLICK, (WPARAM)Msg.WParam, (LPARAM)Msg.LParam);
    }
}
//---------------------------------------------------------------------------


