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

#include <vcl.h>
#include "ITSSkinF.h"
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "FrmResourceF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

//---------------------------------------------------------------------------
#include "CDSItsRoadInfoF.h"
//---------------------------------------------------------------------------
#include "FRAME_ItsRoadF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCalc"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxInplaceContainer"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTextEdit"
#pragma link "cxTL"
#pragma link "cxTLdxBarBuiltInMenu"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma resource "*.dfm"
//TFRAMEItsRoad *FRAMEItsRoad;
//---------------------------------------------------------------------------
__fastcall TFRAMEItsRoad::TFRAMEItsRoad(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    FCompleted = false;
    tlItsRoad->Clear();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::InitItsRoad()
{
    FCompleted = false;
    try
    {
        tlItsRoad->BeginUpdate();
        tlItsRoad->Clear();
        TcxTreeListNode *pRoot = tlItsRoad->Root;

        FOR_STL(TRSido*, pObjSido, RLinkManager->FLists)
        {
            if (pObjSido->FRoads.Size() == 0) continue;
            if (!pObjSido->FShow) continue;

            TcxTreeListNode *pCenter = pRoot->AddChild();
            pCenter->CheckGroupType = ncgCheckGroup;

            pCenter->Values[colItsRoad->ItemIndex] = pObjSido->NAME;
            pCenter->Values[colMem->ItemIndex] = 0;

            FOR_STL(TRRoad*, pObjRoad, pObjSido->FRoads)
            {
                if (!pObjRoad->FShow) continue;

                TcxTreeListNode *pRoad = pCenter->AddChild();
                pRoad->CheckGroupType = ncgCheckGroup;

                pRoad->Values[colItsRoad->ItemIndex] = pObjRoad->NAME;
                pRoad->Values[colMem->ItemIndex] = 0;//(int)pObjRoad;

                FOR_STL(TRLink*, pObjLink, pObjRoad->FLinks)
                {
                    if (!pObjLink->FShow) continue;

                    TcxTreeListNode *pLink = pRoad->AddChild();
                    pLink->CheckGroupType = ncgCheckGroup;

                    pLink->Values[colItsRoad->ItemIndex] = pObjLink->LINKID +  " : " + pObjLink->STNODENAME + " → " + pObjLink->EDNODENAME;
                    pLink->Values[colMem->ItemIndex] = (int)pObjLink;
                }
            }
        }
    }
    __finally
    {
        tlItsRoad->EndUpdate();
        FCompleted = true;
    }
    //ShowMessage("Node count = " + IntToStr(tlItsRoad->AbsoluteCount));
    //tlItsRoad->FullExpand;
}
//---------------------------------------------------------------------------

class TcxTreeListAccess : public TcxTreeList
{
};
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::InitFilter(bool AShow)
{
    FOR_STL(TRSido*, pObjSido, RLinkManager->FLists)
    {
        pObjSido->FShow = AShow;

        FOR_STL(TRRoad*, pObjRoad, pObjSido->FRoads)
        {
            pObjRoad->FShow = AShow;

            FOR_STL(TRLink*, pObjLink, pObjRoad->FLinks)
            {
                pObjLink->FShow = AShow;
            }
        }
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::FilterItsRoad(String AFilter)
{
    if (AFilter.IsEmpty())
    {
        InitFilter(true);
        InitItsRoad();
        return;
    }

    InitFilter(false);
    FOR_STL(TRSido*, pObjSido, RLinkManager->FLists)
    {
        if (pObjSido->NAME.Pos(AFilter) > 0)
        {
            pObjSido->FShow = true;
            FOR_STL(TRRoad*, pObjRoad, pObjSido->FRoads)
            {
                pObjRoad->FShow = true;

                FOR_STL(TRLink*, pObjLink, pObjRoad->FLinks)
                {
                    pObjLink->FShow = true;
                }
            }
        }
        else
        {
            FOR_STL(TRRoad*, pObjRoad, pObjSido->FRoads)
            {
                if (pObjRoad->NAME.Pos(AFilter) > 0)
                {
                    pObjSido->FShow = true;
                    pObjRoad->FShow = true;

                    FOR_STL(TRLink*, pObjLink, pObjRoad->FLinks)
                    {
                        pObjLink->FShow = true;
                    }
                }
                else
                {
                    FOR_STL(TRLink*, pObjLink, pObjRoad->FLinks)
                    {
                        if (pObjLink->LINKID.Pos(AFilter)     > 0 ||
                            pObjLink->STNODENAME.Pos(AFilter) > 0 ||
                            pObjLink->EDNODENAME.Pos(AFilter) > 0 )
                        {
                            pObjSido->FShow = true;
                            pObjRoad->FShow = true;
                            pObjLink->FShow = true;
                        }
                    }
                }
            }
        }
    }
    InitItsRoad();
    tlItsRoad->FullExpand();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::FilterItsRoadA(String AFilter)
{
    if (AFilter.IsEmpty())
    {
        UnFilterItsRoad();
        return;
    }

    //TcxTreeListAccess *pTlList = (TcxTreeListAccess*)tlItsRoad;
    //pTlList->ChangesLocked = true;

    int ii;
    tlItsRoad->BeginUpdate();
    try
    {
        //그룹을 모두 숨긴다.
        for (ii = 0; ii < tlItsRoad->AbsoluteCount; ii++)
        {
            if (tlItsRoad->AbsoluteItems[ii]->IsGroupNode)
            {
                tlItsRoad->AbsoluteItems[ii]->Visible = false;
            }
        }

        for (ii = 0; ii < tlItsRoad->AbsoluteCount; ii++)
        {
            if (tlItsRoad->AbsoluteItems[ii]->IsGroupNode) continue;

            tlItsRoad->AbsoluteItems[ii]->CheckState = cbsUnchecked;

            String sItsRoadName = tlItsRoad->AbsoluteItems[ii]->Texts[colItsRoad->ItemIndex];
            if (sItsRoadName.Pos(AFilter) == 0)
            {
                tlItsRoad->AbsoluteItems[ii]->Visible = false;
            }
            else
            {
                TcxTreeListNode *pCenter = tlItsRoad->AbsoluteItems[ii]->Parent;
                if (pCenter)
                {
                    pCenter->Visible = true;
                }
                tlItsRoad->AbsoluteItems[ii]->Visible = true;
            }
        }
    }
    __finally
    {
        tlItsRoad->EndUpdate();
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::UnFilterItsRoad()
{
    int ii;
    tlItsRoad->BeginUpdate();
    try
    {
        for (ii = 0; ii < tlItsRoad->AbsoluteCount; ii++)
        {
            if (!tlItsRoad->AbsoluteItems[ii]->IsGroupNode)
            {
                tlItsRoad->AbsoluteItems[ii]->CheckState = cbsUnchecked;
            }
            tlItsRoad->AbsoluteItems[ii]->Visible = true;
        }
    }
    __finally
    {
        tlItsRoad->EndUpdate();
    }
}
//---------------------------------------------------------------------------

AnsiString __fastcall TFRAMEItsRoad::GetHTMLString()
{
    AnsiString sHtml = "";
    AnsiString sTemp;
    if (!FCompleted) return lblAll->Caption;//"전체";

    int ii, jj;
    tlItsRoad->BeginUpdate();
    try
    {
        TcxTreeListNode *pRoot = tlItsRoad->Root->GetNext();
        while(pRoot)
        {
            if (pRoot->CheckState == cbsUnchecked ||
                pRoot->Visible == false)
            {
                pRoot = pRoot->GetNext();
                continue;
            }

            for (ii = 0; ii < pRoot->Count; ii++)
            {
                TcxTreeListNode *pRoad = pRoot->Items[ii];
                if (!pRoad->Visible ||
                    pRoad->CheckState == cbsUnchecked)
                {
                    continue;
                }

                int nChecked = 0;
                for (jj = 0; jj < pRoad->Count; jj++)
                {
                    if (!pRoad->Items[jj]->Visible)
                        continue;
                    if (pRoad->Items[jj]->Checked) nChecked++;
                }
                if (nChecked > 0)
                {
                    sTemp = pRoad->Texts[colItsRoad->ItemIndex] + "(" + String(nChecked) + ")";
                    if (sHtml.IsEmpty() == false)
                    {
                        sHtml += ", ";
                    }
                    sHtml += sTemp;
                }
            }
            pRoot = pRoot->GetNext();
        }
    }
    __finally
    {
        tlItsRoad->EndUpdate();
    }

    if (sHtml.IsEmpty()) sHtml = lblAll->Caption;//"전체";
    return sHtml;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::GetCondition(STCodeCondition& ACondition)
{
	ACondition.bUse = false;
	ACondition.Codes.clear();

    FSelNm = lblAll->Caption;//"전체";

    int ii, jj;
    tlItsRoad->BeginUpdate();
    try
    {
        int nMemIdx;

        TcxTreeListNode *pRoot = tlItsRoad->Root->GetNext();
        while(pRoot)
        {
            if (pRoot->CheckState == cbsUnchecked ||
                pRoot->Visible == false)
            {
                pRoot = pRoot->GetNext();
                continue;
            }
            for (ii = 0; ii < pRoot->Count; ii++)
            {
                TcxTreeListNode *pRoad = pRoot->Items[ii];
                if (!pRoad->Visible ||
                    pRoad->CheckState == cbsUnchecked)
                {
                    continue;
                }

                for (jj = 0; jj < pRoad->Count; jj++)
                {
                    if (!pRoad->Items[jj]->Visible) continue;
                    if (!pRoad->Items[jj]->Checked) continue;

                    String sMemIdx = pRoad->Items[jj]->Texts[colMem->ItemIndex];
                    nMemIdx = StrToIntDef(sMemIdx, 0);
                    if (nMemIdx != 0)
                    {
                        try {
                            TRLink* pObj = (TRLink*)nMemIdx;
                            ACondition.Codes.push_back(pObj->LINKID);
                            if (FSelNm == lblAll->Caption)//"전체")
                            {
                                FSelNm = pObj->LINKID;
                            }
                        } catch(...) {}
                    }
                }
            }
            pRoot = pRoot->GetNext();
        }
    }
    __finally
    {
        tlItsRoad->EndUpdate();
    }

	ACondition.bUse = !ACondition.Codes.empty();
}
//------------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::EdFilterKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
	if (VK_RETURN == Key)
	{
        btnFilterClick(NULL);
	}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::btnFilterClick(TObject *Sender)
{
    String sFilter = EdFilter->Text.Trim();
    FilterItsRoad(sFilter);
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEItsRoad::btnNoFilterClick(TObject *Sender)
{
    EdFilter->Text = "";
	UnFilterItsRoad();
}
//---------------------------------------------------------------------------

