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

#include <vcl.h>
#include "ITSSkinF.h"
#include "FrmInitializeF.h"
#include "DMDbF.h"
#pragma hdrstop

#include "CDSMonitoringObjF.h"
#include "FrmCrsCamViewerF.h"
#include "FrmCameraFullScreenF.h"
#include "FrmCameraPlayerF.h"
#include "FrmCameraScreenF.h"
#include "FrmCameraScreenManagerF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxProgressBar"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinsForm"
#pragma link "cxLabel"
#pragma link "cxDropDownEdit"
#pragma link "cxMaskEdit"
#pragma link "cxTextEdit"
#pragma link "dxSkinMcSkin"
#pragma resource "*.dfm"

TFrmCrsCamViewer *FrmCrsCamViewer = NULL;

//---------------------------------------------------------------------------
__fastcall TFrmCrsCamViewer::TFrmCrsCamViewer(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    //DoubleBuffered = true;

    IsLoading = false;

    if (g_AppCfg.sLang != "kr")
    {
        Caption = "Cross Camera Monitoring";
        Font->Name = "Tahoma";
        BtnMonitoringMngr->Font->Name = "Tahoma";
        BtnMonitoringMngr->Caption = "Management";
        BtnMonitoringMngr->Hint    = "Monitoring Screen management";

        LblScreenList->AutoSize = false;
        LblScreenList->Style->Font->Name = "Tahoma";
        LblScreenList->Caption = " Screen List: ";
        LblScreenList->AutoSize = true;

        CbScreenList->Style->Font->Name = "Tahoma";
    }

    g_AppCfg.lMainWinHandle = (long)Handle;

    String sLang = g_AppCfg.sLang;
    ITSSkin_Initialize(Application, sLang.UpperCase());
    ITSSkin_Load(this);
    ITSSkin_Caption(false);
    CMM_LoadForm(g_sFormsDir, this);
    if (WindowState == wsMinimized)
    {
        WindowState = wsMaximized;
    }
    if (WindowState == wsMaximized)
    {
        Width  = 1375;
        Height = 851;
        WindowState = wsMaximized;
    }

    SetColorScheme(g_AppCfg.sSkinName);

    //PnlBack->ParentColor = false;
    //PnlBack->Color = clBlack;
    //PnlBack->Font->Color = clWhite;

    IsFormResizing = false;
    FOldWidth = Width;
    FOldHeight= Height;

    FCurrPage = NULL;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::FormCreate(TObject *Sender)
{
    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    g_AppCfg.lMainWinHandle = (long)Handle;

    FrmCameraScreen = new TFrmCameraScreen(PnlBack);
    FrmCameraScreen->Parent = PnlBack;
    FrmCameraScreen->Show();

    Application->ShowMainForm = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::FormShow(TObject *Sender)
{
    Application->ProcessMessages();
    Refresh();
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmCrsCamViewer::CommClose()
{
    try
    {
        ITSSkin_Term();
        CMM_SaveForm(g_sFormsDir, this);
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::SetColorScheme(String ASkinName)
{
    dxSkinController1->SkinName = ASkinName;
    dxSkinController1->Refresh();
}
//---------------------------------------------------------------------------

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

void __fastcall TFrmCrsCamViewer::FormInit()
{
    ObjScreenManager = new TMonitoringScreenManager();
    ObjCtlrManager   = new TMonitoringCtlrManager(enMonitoringCrsCamera);

    FrmInitialize = new TFrmInitialize(this);
    FrmInitialize->ShowModal();
    FrmInitialize = NULL;

    IsLoading = true;
    CreateMonitoringScreen();

    PnlBack->ParentColor = true;
    //BtnMonitoringMngr->Visible = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::ApplicationEvents1Minimize(TObject *Sender)
{
    //영상표출을 정지한다.
    if (FrmCameraScreen && IsLoading) FrmCameraScreen->Stop();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::ApplicationEvents1Restore(TObject *Sender)
{
    //영상표출을 재상영한다.
    if (FrmCameraScreen && IsLoading) FrmCameraScreen->Play();
}
//---------------------------------------------------------------------------

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

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

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

void __fastcall TFrmCrsCamViewer::RecalFormSize()
{
    int nNewW = Width;
    int nNewH = Height;

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

    Application->ProcessMessages();
    FOldWidth = nNewW;
    FOldHeight= nNewH;

    if (FrmCameraScreen && IsLoading) FrmCameraScreen->RecalFormSize();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::CreateMonitoringScreen()
{
    Application->ProcessMessages();
    CbScreenList->Properties->OnChange = NULL;
    try
    {
        LockWindowUpdate(Handle);
        CbScreenList->Properties->Items->Clear();
        FOR_STL(TMonitoringScreen*, pObj, ObjScreenManager->FLists)
        {
			CbScreenList->Properties->Items->Add(" " + pObj->NAME + " ");
        }
        CbScreenList->ItemIndex = 0;
    }
    __finally
    {
        LockWindowUpdate(0);
        CbScreenList->Properties->OnChange = CbScreenListPropertiesChange;
        ResetScreenForm();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::CbScreenListPropertiesChange(TObject *Sender)
{
    EdFocus->SetFocus();
    ResetScreenForm();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::ResetScreenForm()
{
    if (FrmCameraScreen && IsLoading) FrmCameraScreen->ResetScreenForm(CbScreenList->Text.Trim());
}
//---------------------------------------------------------------------------

void __fastcall TFrmCrsCamViewer::BtnMonitoringMngrClick(TObject *Sender)
{
    if (!DMDb->Connect())
    {
        if (g_AppCfg.sLang != "kr")
        {
            Application->MessageBox(L"Database connection failed.\r\n\r\nPlease restart the program.",
                                    L"Database connection error !!!",
                                    MB_OK|MB_ICONERROR);
        }
        else
        {
            Application->MessageBox(L"데이터베이스 연결에 실패하였습니다.\r\n\r\n프로그램을 다시 시작하십시요.",
                                    L"데이터베이스 연결 오류!!!",
                                    MB_OK|MB_ICONERROR);
        }
        return;
    }

    TFrmCameraScreenManager *pForm = new TFrmCameraScreenManager(this);
    pForm->ShowModal();
    bool bUpdate = pForm->FUpdate;
    if(pForm)
    {
        delete pForm;
        pForm = NULL;
    }

    if (bUpdate)
    {
        ObjCtlrManager->LoadMonitoringFormFromDb(DMDb->GetConnection());
        CreateMonitoringScreen();
    }

    DMDb->Close();

    EdFocus->SetFocus();
}
//---------------------------------------------------------------------------

