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

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

#include "FRAME_FlashMapF.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ImageBtn"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGroupBox"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "dxSkinBlack"
#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 "dxSkinMcSkin"
#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 "dxSkinsCore"
#pragma link "dxSkinSeven"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "ShockwaveEx"
#pragma link "ShockwaveFlashObjects_TLB"
#pragma resource "*.dfm"
TFRAMEFlashMap *FRAMEFlashMap;

//---------------------------------------------------------------------------
__fastcall TFRAMEFlashMap::TFRAMEFlashMap(TComponent* Owner)
    : TFrame(Owner)
{
    FlashCallbackFunc = NULL;
    LoadLocalSkin();
    FLinkLevel = 1;
    FDrawMode  = 1;
    FMapInit = false;
    MapStart = false;
    ModeSelect = false;
    LoadTraffic= true;

    ShockwaveFlash1->Align = alClient;

    GrpTool->Enabled = false;
    LoadMap();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::LoadLocalSkin()
{
    try
    {
        ImgPan->NormalPicture->Assign(ImgPan_c->Picture);
        ImgPan->Picture->Assign(ImgPan_c->Picture);
        ImgPan->TogglePicture->Assign(ImgPan_n->Picture);
        ImgPan->ClickedPicture->Assign(ImgPan_c->Picture);

        ImgZoomIn->NormalPicture->Assign(ImgZoomIn_n->Picture);
        ImgZoomIn->Picture->Assign(ImgZoomIn_n->Picture);
        ImgZoomIn->TogglePicture->Assign(ImgZoomIn_c->Picture);
        ImgZoomIn->ClickedPicture->Assign(ImgZoomIn_n->Picture);

        ImgZoomOut->NormalPicture->Assign(ImgZoomOut_n->Picture);
        ImgZoomOut->Picture->Assign(ImgZoomOut_n->Picture);
        ImgZoomOut->TogglePicture->Assign(ImgZoomOut_c->Picture);
        ImgZoomOut->ClickedPicture->Assign(ImgZoomOut_n->Picture);

        ImgSel->NormalPicture->Assign(ImgSel_n->Picture);
        ImgSel->Picture->Assign(ImgSel_n->Picture);
        ImgSel->TogglePicture->Assign(ImgSel_c->Picture);
        ImgSel->ClickedPicture->Assign(ImgSel_n->Picture);

        ImgStnd->NormalPicture->Assign(ImgStnd_n->Picture);
        ImgStnd->Picture->Assign(ImgStnd_n->Picture);
        ImgStnd->TogglePicture->Assign(ImgStnd_c->Picture);
        ImgStnd->ClickedPicture->Assign(ImgStnd_n->Picture);

        ImgRefresh->NormalPicture->Assign(ImgRefresh_n->Picture);
        ImgRefresh->Picture->Assign(ImgRefresh_n->Picture);
        ImgRefresh->TogglePicture->Assign(ImgRefresh_c->Picture);
        ImgRefresh->ClickedPicture->Assign(ImgRefresh_n->Picture);
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::LoadGisMap()
{
    FMapInit = true;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::SetLinkLevel(int ALinkLevel)
{
    switch(ALinkLevel)
    {
    case 1:
        break;
    case 2:
        break;
    case 3:
        break;
    default: return;
    }

    FLinkLevel = ALinkLevel;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEFlashMap::SetDrawMode(int ADrawMode)
{
    FDrawMode = ADrawMode;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::OnGisOperationClick(TObject *Sender)
{
    if (!MapStart) return;

    TImageBtn *pMenu = (TImageBtn*)Sender;
    switch(pMenu->Tag)
    {
    case 3: //pan
        try
        {
            ImgPan->NormalPicture->Assign(ImgPan_c->Picture);
            ImgPan->Picture->Assign(ImgPan_c->Picture);
            ImgPan->TogglePicture->Assign(ImgPan_n->Picture);
            ImgPan->ClickedPicture->Assign(ImgPan_n->Picture);

            ImgSel->NormalPicture->Assign(ImgSel_n->Picture);
            ImgSel->Picture->Assign(ImgSel_n->Picture);
            ImgSel->TogglePicture->Assign(ImgSel_c->Picture);
            ImgSel->ClickedPicture->Assign(ImgSel_c->Picture);
        } catch(Exception &e) {}
        TCommFlash::CallFunc(ShockwaveFlash1, "SelectMode", "0");
        ShockwaveFlash1->Cursor = crDefault;//Drag;
        ModeSelect = false;
        break;
    case 1: //zoomin
        TCommFlash::CallFunc(ShockwaveFlash1, "ZoomIn", "");
        break;
    case 2: //zoomout
        TCommFlash::CallFunc(ShockwaveFlash1, "ZoomOut", "");
        break;
    case 5: //select
        try
        {
            ImgPan->NormalPicture->Assign(ImgPan_n->Picture);
            ImgPan->Picture->Assign(ImgPan_n->Picture);
            ImgPan->TogglePicture->Assign(ImgPan_c->Picture);
            ImgPan->ClickedPicture->Assign(ImgPan_n->Picture);

            ImgSel->NormalPicture->Assign(ImgSel_c->Picture);
            ImgSel->Picture->Assign(ImgSel_c->Picture);
            ImgSel->TogglePicture->Assign(ImgSel_n->Picture);
            ImgSel->ClickedPicture->Assign(ImgSel_n->Picture);
        } catch(Exception &e) {}
        TCommFlash::CallFunc(ShockwaveFlash1, "SelectMode", "1");
        ShockwaveFlash1->Cursor = crHelp;
        ModeSelect = true;
        break;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::ImgStndClick(TObject *Sender)
{
    //stand resion
    if (!MapStart) return;
    TCommFlash::CallFunc(ShockwaveFlash1, "StdExtent", "");
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::ImgRefreshClick(TObject *Sender)
{
    //refresh
    if (!MapStart) return;
    TCommFlash::CallFunc(ShockwaveFlash1, "RefreshMap", "");
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::SelectObjectByPos(double dPosX, double dPosY, bool bArrow/*=false*/)
{
    if (!MapStart) return;
    TCommFlash::CallFunc(ShockwaveFlash1, "SelectPoint", String(dPosX) + "," + String(dPosY) + "|3");
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::SelectObjectByPos()
{
#if 0
    TPoint ptTmp;

    ImgSelect->Parent = xclntMap;
    ImgSelect->Visible = false;
    TmrSelect->Enabled = false;
    TmrSelect->Interval = 3000;

    ptTmp = xclntMap->RealToScreen(m_ptSelect);

    ImgSelect->Left = ptTmp.x - (ImgSelect->Width/2);
    ImgSelect->Top  = ptTmp.y - (ImgSelect->Height)-10;
    TmrSelect->Enabled = true;
    ImgSelect->Visible = true;
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::TmrSelectTimer(TObject *Sender)
{
    TmrSelect->Enabled = false;
    ImgSelect->Visible = false;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::OnCloseQuery(bool &CanClose)
{
    ShockwaveFlash1->StopPlay();
    ShockwaveFlash1->Stop();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::SelGisItem(int AType, String AId)
{
#if 0
    try
    {
        switch(AType)
        {
        case 0: TXiMapUtil::MoveNode(xclntMap, AId);        break;
        case 1: TXiMapUtil::MoveLink(xclntMap, AId);        break;
        case 2: TXiMapUtil::MoveServiceLink(xclntMap, AId); break;
        case 3: TXiMapUtil::MoveRoad(xclntMap, AId);        break;
        default: return;
        }
    }
    catch(...)
    {
    }
#endif
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::ShockwaveFlash1FlashCall(TObject *ASender, const WideString request)
{
    try
    {
        String sTemp;
        String sFunc;
        String sArgs;

        //<invoke name="Debug" returntype="xml"><arguments><string>KKK|LinkMap 1</string></arguments></invoke>
        sTemp = request;

        int nFuncStrIdx = sTemp.Pos("\"");
        int nFuncEndIdx = sTemp.Pos("\" returntype");
        int nArgsStrIdx = sTemp.Pos("string>");
        int nArgsEndIdx = sTemp.Pos("</string");

        sFunc = sTemp.SubString(nFuncStrIdx + 1, nFuncEndIdx - (nFuncStrIdx + 1));
        sArgs = sTemp.SubString(nArgsStrIdx + 7, nArgsEndIdx - (nArgsStrIdx + 7));

        if (Memo1->Visible) Memo1->Lines->Add("[" + sFunc + "]  [" + sArgs + "]");

        if (sFunc == "OnMapStart")
        {
            StatusBar->Panels->Items[0]->Text = "레벨: " + sArgs;
            MapStart = true;
            TmrInit->Enabled = true;
        }
        else
        if (sFunc == "OnZoomChanged")
        {
            StatusBar->Panels->Items[0]->Text = "레벨: " + sArgs;
        }
        else
        if (sFunc == "OnMousePos")
        {
            StatusBar->Panels->Items[1]->Text = "클릭좌표: " + sArgs;
        }
        else
        if (sFunc == "OnNodeLoad")
        {
        }
        else
        if (sFunc == "OnLinkLoad")
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = sArgs;

            String sResult = pStringList->Strings[0];
            String sLevel  = pStringList->Strings[1];

            delete pStringList;

            TCommFlash::CallFunc(ShockwaveFlash1, "LoadLinkVertex", sLevel);
        }
        else
        if (sFunc == "OnLinkVertexLoad")
        {
            //RefreshId();
        }
        else
        if (sFunc == "OnFindLink")
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = sArgs;

            String sResult = pStringList->Strings[0];   //Find resule
            String sLinkId = pStringList->Strings[1];   //Link Id

            delete pStringList;
        }
        else
        if (sFunc == "OnSelLink")
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = sArgs;

            String sLevl = pStringList->Strings[0]; //링크레벨
            String sId   = pStringList->Strings[1]; //링크아이디
            String sZoom = pStringList->Strings[2]; //줌레벨
            String sPosX = pStringList->Strings[3]; //x
            String sPosY = pStringList->Strings[4]; //y
            String sMapX = pStringList->Strings[5]; //lat
            String sMapY = pStringList->Strings[6]; //lng

            delete pStringList;

            //StatusBar->Panels->Items[1]->Text = "클릭좌표: " + sMapX + "," + sMapY;

            //FPosX = sMapX.ToDouble();
            //FPosY = sMapY.ToDouble();
            if (true)
            {
                //UpdateSelect(sId, FPosX, FPosY);
            }
        }
    }
    catch(Exception &exception)
    {
    }

    try
    {
        if (FlashCallbackFunc)
        {
            FlashCallbackFunc(ASender, request);
        }
    }
    catch(Exception &exception) {}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::FrameResize(TObject *Sender)
{
    ShockwaveFlash1->Align = alClient;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::LoadMap()
{
    try {
        ShockwaveFlash1->Stop();
        String sFileName = g_AppCfg.flashmap.sLinkMap + "&link="+FLinkLevel;
        ShockwaveFlash1->Movie = sFileName;
        ShockwaveFlash1->Play();
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::TmrInitTimer(TObject *Sender)
{
    TmrInit->Enabled = false;
    if (FMapInit == false || !MapStart)
    {
        TmrInit->Enabled = true;
        return;
    }

    TCommFlash::CallFunc(ShockwaveFlash1, "SetDrawMode",  String(FDrawMode));
    TCommFlash::CallFunc(ShockwaveFlash1, "SetLinkLevel", String(FLinkLevel));
    //TCommFlash::CallFunc(ShockwaveFlash1, "MultiSelect",  FMultiSelect ? "1" : "0");

    //StatusBar->Panels->Items[0]->Text = "레벨: " + sArgs;
    //TCommFlash::CallFunc(ShockwaveFlash1, "LoadNode", "1");
    if (LoadTraffic)
    {
        TCommFlash::CallFunc(ShockwaveFlash1, "LoadLink", String(FLinkLevel));
    }
    GrpTool->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::GrpToolDblClick(TObject *Sender)
{
Memo1->Visible = !Memo1->Visible;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEFlashMap::Memo1DblClick(TObject *Sender)
{
Memo1->Lines->Clear();
}
//---------------------------------------------------------------------------

