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

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

#include "VMS0201MF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxDropDownEdit"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxTextEdit"
#pragma link "dxSkinBlack"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinsCore"
#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 "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 "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 "cxImage"
#pragma link "cxLabel"
#pragma link "cxStyles"
#pragma link "dxSkinscxPCPainter"
#pragma resource "*.dfm"

//TVMS0201M *VMS0201M = NULL;

//---------------------------------------------------------------------------
__fastcall TVMS0201M::TVMS0201M(TComponent* AOwner, HWND AHandle, String ACaption, int AScreenIdx)
    : TForm(AOwner)
{           
    LangTrans->Translate(this, ITSDb_GetConnection());

    FListForm = new TList();
    FScreenName  = ACaption;
    FScreenIndex = AScreenIdx;
    FPlay        = false;

    FMinWidth  = 1000;
    FMinHeight = 1000;
    FMaxWidth  = 0;
    FMaxHeight = 0;

    FWndParent = AHandle;
    Caption    = ACaption;

    FDisplayMode = 0;
    FViewWidth   = 320;

    TvVmsMsg->OptionsView->DataRowHeight  = 0;
    TvVmsMsg->OptionsView->CellAutoHeight = true;
    TvVmsMsg->OptionsView->Indicator      = true;

    TvVmsMsg->OptionsCustomize->ColumnMoving  = false;
    TvVmsMsg->OptionsCustomize->ColumnSorting = false;

    //TvVmsMsg->Columns[0]->Width = 40;
    //TvVmsMsg->Columns[0]->Options->HorzSizing = false;
    for (int ii = 0; ii < INT_VMS_MAX_FORM; ii++)
    {
        TvVmsMsg->Columns[2+ii]->Caption = "Phase " + String(ii+1);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::FormCreate(TObject *Sender)
{
    ScrollBox->ParentColor = true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::FormShow(TObject *Sender)
{
    Refresh();
    ScrollBox->Align = alClient;
    TmrShow->Enabled = true;
}
//---------------------------------------------------------------------------

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

void __fastcall TVMS0201M::ScrollBoxClick(TObject *Sender)
{
    ScrollBox->SetFocus();    
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::ScrollBoxMouseWheel(TObject *Sender, TShiftState Shift, int WheelDelta, TPoint &MousePos, bool &Handled)
{
    ScrollBox->VertScrollBar->Position -= WheelDelta;    
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::SetPlay(bool APlay)
{
#if 0
    try
    {
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
            pForm->Play = APlay;
        }
    }
    catch(Exception &e)
    {
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::FormResize(TObject *Sender)
{
    RecalFormPosition();
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::RecalFormPosition()
{
#if 0
    int h = 0;
    for (int ii = 0; ii < FpnlForm->ControlCount; ii++)
    {
        if (FpnlForm->Controls[ii]->BoundsRect.Bottom > h)
            h = FpnlForm->Controls[ii]->BoundsRect.Bottom;
    }
    FpnlForm->Height = h+50;
    ScrollBox->HorzScrollBar->Range = 0;
    ScrollBox->VertScrollBar->Range = FpnlForm->Height;
#endif
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::RefreshScenario()
{
    CMM_ClearGridTableView(TvVmsMsg);

    int ii, jj;
    int nCols = 0;

    TcxDataController *pGDC = TvVmsMsg->DataController;
    try
    {
        LockWindowUpdate(Handle);

        int nRow;
        TvVmsMsg->BeginUpdate();
        pGDC->BeginUpdate();

        for (ii = 0; ii < INT_VMS_MAX_FORM; ii++)
        {
            TvVmsMsg->Columns[2+ii]->Visible = false;
            TvVmsMsg->Columns[2+ii]->Width   = FMinWidth;
        }

        for(jj = 0; jj < FListForm->Count; jj++)
        {
            TVmsCtlr *pObj = (TVmsCtlr*)FListForm->Items[jj];

            nRow = pGDC->AppendRecord();

            //pObj->VMS_CTLR_NMBR;
            String sVms = String(pObj->VMS_CTLR_NMBR);
            sVms += " [" + pObj->VMS_CTLR_ID + "]";
            sVms += "\r\n" + pObj->VMS_NM + "\r\n";
            sVms += ITSUtil_FormatStr(pObj->VmsMsg->OFFER_DT, STR_DATETIME);
            sVms += "\r\n" + VmsOperMode(pObj);

            pGDC->Values[nRow][ColSch01->Index] = sVms;
            pGDC->Values[nRow][ColSch02->Index] = pObj->VmsMsg->OFFER_YN ? "Y" : "N";

            pObj->Lock();

            if (pObj->VmsMsg->Total > nCols) nCols = pObj->VmsMsg->Total;

            for(ii = 0; ii < pObj->VmsMsg->Total && ii < INT_VMS_MAX_FORM; ii++)
            {
                //TvVmsMsg->Columns[2+ii]->Width = FMaxWidth;

                Graphics::TBitmap *pBitmap = pObj->VmsMsg->PHASE[ii].pVmsBmp;
                TStream	*pImgStream = new TMemoryStream();
                try
                {
                    pBitmap->SaveToStream(pImgStream);
                    void    *p;
                    Variant  v;
                    int aBound[]={0, pImgStream->Size - 1};
                    v = VarArrayCreate(aBound, 1, varByte);
                    p = VarArrayLock(v);
                    pImgStream->Position = 0;
                    pImgStream->Read(p, pImgStream->Size);
                    VarArrayUnlock(v);
                    pGDC->Values[nRow][2+ii] = v;
                } catch(Exception &e) {}
                SAFE_DELETE(pImgStream);
            }
            pObj->UnLock();
        }

        for (ii = 0; ii < nCols; ii++)
        {
            TvVmsMsg->Columns[2+ii]->Visible = true;
        }
    }
    __finally
    {
        pGDC->EndUpdate();
        TvVmsMsg->EndUpdate();
        //TvVmsMsg->ApplyBestFit(NULL, false, false);
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

bool __fastcall TVMS0201M::AddVmsForm(String AVmsNmbr)
{
    TVmsCtlr *pObj = VmsCtlrManager->FLists.Find(AVmsNmbr);
    if (!pObj) return false;

    if (pObj->WIDTH  > FMaxWidth)  FMaxWidth  = pObj->WIDTH;
    if (pObj->WIDTH  < FMinWidth)  FMinWidth  = pObj->WIDTH;
    if (pObj->HEIGHT > FMaxHeight) FMaxHeight = pObj->HEIGHT;
    if (pObj->HEIGHT < FMinHeight) FMinHeight = pObj->HEIGHT;

#if 0
    TVMSFORM0 *pForm = new TVMSFORM0(this, pObj);
    pForm->SetFormSize(pObj->WIDTH);
    pForm->Parent     = FpnlForm;
    pForm->FScrollBox = ScrollBox;
    pForm->FWndParent = Handle;
    pForm->Show();
    FListForm->Add(pForm);
#else
    FListForm->Add(pObj);
#endif
    return true;
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::FpnlFormClick(TObject *Sender)
{
    ScrollBox->SetFocus();
}
//---------------------------------------------------------------------------

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

void __fastcall TVMS0201M::FormDestroy(TObject *Sender)
{
    ClearFormList();
    SAFE_DELETE(FListForm);
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::SetFormSize(int AWidth)
{
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
            pForm->SetFormSize(AWidth);
            FViewWidth = pForm->Width;
        }
    }
    __finally
    {
        LockWindowUpdate(0);
    }
}
//---------------------------------------------------------------------------

void __fastcall TVMS0201M::ClearFormList()
{
#if 0
    try
    {
        LockWindowUpdate(Handle);
        for(int ii = 0; ii < FListForm->Count; ii++)
        {
            TVMSFORM0 *pForm = (TVMSFORM0*)FListForm->Items[ii];
            pForm->Hide();
            SAFE_DELETE(pForm);
        }
        FListForm->Clear();
    }
    __finally
    {
        LockWindowUpdate(0);
    }
#else
        LockWindowUpdate(Handle);
        FListForm->Clear();
#endif
}
//---------------------------------------------------------------------------


void __fastcall TVMS0201M::ColSch01CustomDrawCell(TcxCustomGridTableView *Sender,
          TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
          bool &ADone)
{
    if (AViewInfo)
    {
        String sOffer = AViewInfo->GridRecord->DisplayTexts[ColSch02->Index];

        if (sOffer == "N")
        {
            ACanvas->Canvas->Font->Color = clRed;
        }
    }
}
//---------------------------------------------------------------------------

