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

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

#include "FrmFullScreenF.h"
#include "FrmCrsStreamPlayerF.h"
#include "FrmCrsCmraF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

//TFrmCrsStreamPlayer *FrmCrsStreamPlayer;
//---------------------------------------------------------------------------
__fastcall TFrmCrsStreamPlayer::TFrmCrsStreamPlayer(TComponent* Owner, TCross *ACrossObj)
    : TForm(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());

    FCross = ACrossObj;
    FParent = this->Parent;
	FFormList = new TObjectList();

    PnlCmra->ParentColor = false;
    PnlCmra->Color = clBlack;

    int defW = 400;
    int defH = 300;
    int cntY = 1;
    int cntX = 1;
    int totForm = 1;

    if (FCross != NULL) {
        Caption = FCross->CROSS_NM;

        int camCnt = FCross->FLists.Size();

        switch(camCnt) {
        case 1: case 2:
            cntY = 1;
            cntX = camCnt;
            break;
        case 3: case 4:
            cntY = 2;
            cntX = 2;
            break;
        case 5: case 6:
            cntY = 2;
            cntX = 3;
            break;
        case 7: case 8:
            cntY = 2;
            cntX = 4;
            break;
        default:
            cntY = 3;
            cntX = 3;
            break;
        }
        totForm = (cntX * cntY);

        ClientWidth = (cntX * defW) + cntX-1;
        ClientHeight = (cntY * defH) + cntY-1;

        int L, T;
        camCnt = 0;
        FOR_STL(TCrossCam *, pCrsCam, FCross->FLists)
        {
            L = camCnt % cntX;
            T = camCnt / cntY;

            TFrmCrsCamra *pCmraForm = new TFrmCrsCamra(this, pCrsCam);
            pCmraForm->Parent = FpnlForm;
            //pCmraForm->Left = L * defW;
            //pCmraForm->Top  = T * defH;
            FFormList->Add(pCmraForm);

            camCnt++;
            if (camCnt >= (cntX * cntY))
            {
                break;
            }
        }

        for (int ii = camCnt; ii < totForm; ii++)
        {
            L = camCnt % cntX;
            T = camCnt / cntY;

            TFrmCrsCamra *pCmraForm = new TFrmCrsCamra(this, NULL);
            pCmraForm->Parent = FpnlForm;
            //pCmraForm->Left = L * defW;
            //pCmraForm->Top  = T * defH;
            FFormList->Add(pCmraForm);
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmCrsStreamPlayer::FormShow(TObject *Sender)
{
    Refresh();
	for (int ii = 0; ii < FFormList->Count; ii++)
    {
        TFrmCrsCamra *pCmraForm = (TFrmCrsCamra*)FFormList->Items[ii];
    	pCmraForm->Show();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsStreamPlayer::FormClose(TObject *Sender, TCloseAction &Action)
{
    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsStreamPlayer::FormDestroy(TObject *Sender)
{
    ClearFormList();
    if (FCross) FCross->FData1 = NULL;
}
//---------------------------------------------------------------------------
void __fastcall TFrmCrsStreamPlayer::ClearFormList()
{
	while(FFormList->Count)
    {
        TFrmCrsCamra *pCmraForm = (TFrmCrsCamra*)FFormList->Items[0];
    	FFormList->Delete(0);
    }
    FFormList->Clear();
}
//---------------------------------------------------------------------------

