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

#include <vcl.h>
#pragma hdrstop

#include "FrmCameraScreenF.h"
#include "FrmCameraFullScreenF.h"
#include "AppGlobalF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFrmCameraScreen *FrmCameraScreen = NULL;
//---------------------------------------------------------------------------
__fastcall TFrmCameraScreen::TFrmCameraScreen(TComponent* Owner)
    : TForm(Owner)
{
    FTitle = Caption;
    if (g_AppCfg.sLang != "kr") FTitle = "Camera monitoring";

    screeForm.screenCnt = 0;
    for(int ii = 0; ii < MAX_CAMERA_PER_SCREEM; ii++)
    {
        screeForm.info[ii].show      = false;
        screeForm.info[ii].installed = false;
        screeForm.info[ii].CTLR_NMBR = 0;
        screeForm.info[ii].frmCam    = NULL;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreen::SetLayout(int ALayout)
{
    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;
    FScreenCnt = FCols * FRows;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreen::ResetScreenForm(String AScreenName)
{
    TMonitoringScreen* pObj = ObjScreenManager->FLists.Find(AScreenName);
    if (!pObj)
    {
        String sMsg;
        if (g_AppCfg.sLang != "kr") sMsg = "Change monitoring screen\r\nNot found screen information.\r\n[" + AScreenName + "]";
        else                        sMsg = "모니터링 화면 변경\r\n화면정보를 찾을 수 없습니다..\r\n[" + AScreenName + "]";
        Application->MessageBox(sMsg.c_str(), FTitle.c_str(), MB_OK|MB_ICONERROR|MB_APPLMODAL);
		return;
    }

    SetLayout(pObj->LAYOUT);

    int ii;
    for(ii = 0; ii < screeForm.screenCnt && ii < MAX_CAMERA_PER_SCREEM; ii++)
    {
        screeForm.info[ii].show      = false;
        screeForm.info[ii].installed = false;
        screeForm.info[ii].CTLR_NMBR = 0;
        if (screeForm.info[ii].frmCam)
        {
            screeForm.info[ii].frmCam->Stop();
        }
    }

    screeForm.screenCnt = FScreenCnt;
    for(ii = 0; ii < screeForm.screenCnt && ii < MAX_CAMERA_PER_SCREEM; ii++)
    {
        screeForm.info[ii].show = true;
        if (screeForm.info[ii].frmCam == NULL)
        {
            screeForm.info[ii].frmCam = new TFrmCameraPlayer(this);
            screeForm.info[ii].frmCam->Parent = PnlCamera;
        }
        screeForm.info[ii].frmCam->Tag = ii;
        screeForm.info[ii].installed = false;
        screeForm.info[ii].CTLR_NMBR = 0;

        String NAME           = " --Empty--";
        String STRM_ADDR      = "";
        String FULL_STRM_ADDR = "";
        int    VIEW_MODE      = 0;

        TScreenCtlr *pScr = pObj->FLists.Find(ii+1);
        if (pScr)
        {
            TMonitoringCtlr *pCtlr = ObjCtlrManager->FLists.Find(pScr->CTLR_NMBR);
            if (pCtlr)
            {
                screeForm.info[ii].installed = true;
                screeForm.info[ii].CTLR_NMBR = pScr->CTLR_NMBR;

                NAME           = pCtlr->NAME;
                STRM_ADDR      = pCtlr->STRM_ADDR;
                FULL_STRM_ADDR = pCtlr->FULL_STRM_ADDR;
            }
        }
        screeForm.info[ii].frmCam->InitCamera(screeForm.info[ii].installed,
                                              screeForm.info[ii].CTLR_NMBR,
                                              NAME,
                                              STRM_ADDR,
                                              FULL_STRM_ADDR,
                                              VIEW_MODE);
        screeForm.info[ii].frmCam->AutoPlay();
    }

    for(ii = 0; ii < MAX_CAMERA_PER_SCREEM; ii++)
    {
        if (screeForm.info[ii].show == false)
        {
            if (screeForm.info[ii].frmCam)
            {
                screeForm.info[ii].frmCam->Hide();
            }
        }
    }
    RecalFormSize();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreen::RecalFormSize()
{
    int ii;
    Application->ProcessMessages();

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

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

    try
    {
        LockWindowUpdate(Handle);

        for(ii = 0; ii < screeForm.screenCnt && ii < MAX_CAMERA_PER_SCREEM; ii++)
        {
            if (!screeForm.info[ii].frmCam)
                continue;

            screeForm.info[ii].frmCam->Width = nWidth;
            screeForm.info[ii].frmCam->Height= nHeight;

            nTotTop  = (nRows * screeForm.info[ii].frmCam->Height)+(nTop*(nRows+1));
            nTotLeft = (nCols * screeForm.info[ii].frmCam->Width)+(nLeft*(nCols+1));
            screeForm.info[ii].frmCam->Top = nTotTop;
            screeForm.info[ii].frmCam->Left= nTotLeft;

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

            if (!screeForm.info[ii].frmCam->Showing)
            {
                screeForm.info[ii].frmCam->Show();
            }
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreen::Play()
{
    int ii;
    for(ii = 0; ii < screeForm.screenCnt && ii < MAX_CAMERA_PER_SCREEM; ii++)
    {
        if (!screeForm.info[ii].frmCam)
            continue;
        screeForm.info[ii].frmCam->AutoPlay();
    }
}
//---------------------------------------------------------------------------
void __fastcall TFrmCameraScreen::Stop()
{
    int ii;
    for(ii = 0; ii < screeForm.screenCnt && ii < MAX_CAMERA_PER_SCREEM; ii++)
    {
        if (!screeForm.info[ii].frmCam)
            continue;
        screeForm.info[ii].frmCam->Stop();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCameraScreen::TmrFullScreenTimer(TObject *Sender)
{
    TmrFullScreen->Enabled = false;
    if (FFullScreenIdx < screeForm.screenCnt && FFullScreenIdx < MAX_CAMERA_PER_SCREEM)
    {
        if (screeForm.info[FFullScreenIdx].frmCam)
        {
            Stop();

            TFrmCameraFullScreen *pFrmFullScreen = new TFrmCameraFullScreen(this);
            pFrmFullScreen->BoundsRect = this->Monitor->BoundsRect;
            pFrmFullScreen->PnlStream->Align = alNone;
            pFrmFullScreen->PnlStream->Align = alClient;

            pFrmFullScreen->RTSP_ADDR = screeForm.info[FFullScreenIdx].frmCam->FULL_STRM_ADDR;

            pFrmFullScreen->ShowModal();
            PnlCamera->Parent = this;
            PnlCamera->Tag = (int)this;

            delete pFrmFullScreen;
            pFrmFullScreen = NULL;

            Play();
        }
    }
}
//---------------------------------------------------------------------------

