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

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

#include "FRAME_OpenMapF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCheckBox"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxTextEdit"
#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 resource "*.dfm"
//TFRAMEOpenMap *FRAMEOpenMap;
//---------------------------------------------------------------------------
__fastcall TFRAMEOpenMap::TFRAMEOpenMap(TComponent* Owner)
    : TFrame(Owner), MapCallbackFunc(NULL)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load((TForm*)this);
    LoadLocalSkin();

    fJSExec = new TJSExec(WebBrowser1);
    reMsg->Lines->Clear();

    FLayerIdx = 0;
    FMapStart = false;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::LoadLocalSkin()
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEOpenMap::OpenMap()
{
    TmrMapOpen->Enabled = true;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::TmrMapOpenTimer(TObject *Sender)
{
    TmrMapOpen->Enabled = false;
    LoadMap();
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::LoadMap()
{
    FHtmlFileName = "";
    try {
        FHtmlFileName = ITSMap_MapOpen(g_sMapDir);
        if (FHtmlFileName != "")
        {
            reMsg->Lines->Add(FHtmlFileName);
            WebBrowser1->Navigate(FHtmlFileName.c_str());
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::OnCloseQuery(bool &CanClose)
{
    try
    {
        if (fJSExec)
        {
            fJSExec->Free();
        }
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------
bool __fastcall TFRAMEOpenMap::ExecScript(String AScript)
{
    if (FMapStart == false) return false;

    try
    {
        fJSExec->RunJSFn(AScript);
    }
    catch(Exception &exception)
    {
        WriteLog("ExecScript: [%s]", AnsiString(exception.ClassName()+exception.Message).c_str());
        return false;
    }
    return true;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::SelectObjectByPos(double dPosX, double dPosY, bool bArrow/*=false*/)
{
}
//---------------------------------------------------------------------------
bool __fastcall TFRAMEOpenMap::SelectLinkByPos(int X, int Y, String &ALinkId)
{
    return false;
}
//---------------------------------------------------------------------------
bool __fastcall TFRAMEOpenMap::GetLevelLinkId(int ALinkLevel, String &sLinkId, int X, int Y)
{
    return false;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::SelGisItem(int AType, String AId)
{
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::WebBrowser1DownloadComplete(TObject *Sender)
{
	// 웹페이지 스크롤바 없애기
#if 0
    try
    {
        Variant vBody;
        Variant vObj = WebBrowser1->OleObject;
        String strDocCompatMode = vObj.OlePropertyGet("document").OlePropertyGet("compatMode");
        if (SameText(strDocCompatMode, "CSS1Compat"))
            vBody = vObj.OlePropertyGet("Document").OlePropertyGet("documentElement");
        else
            vBody = vObj.OlePropertyGet("Document").OlePropertyGet("body");

        vBody.OlePropertyGet("style").OlePropertySet("overflow", "hidden");
    }
    catch(Exception &e)
    {
    }
#endif
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnMapToJpgClick(TObject *Sender)
{
    // 지도 이미지 저장
    try
    {
        SaveDialog1->FileName = Now().FormatString("YYYYMMDDHHNNSS") + ".jpg";
        if (SaveDialog1->Execute())
        {
        }
    } catch(Exception &e) { }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnMoveMapToFitClick(TObject *Sender)
{
    // 지도객체영역을 전체영역으로
    String sJs;
    sJs.printf(L"extentLayer(%d)", FLayerIdx);
    ExecScript(sJs);
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnMoveDefBoundsClick(TObject *Sender)
{
    // 지도 기본영역으로 이동
    ExecScript("stdExtent()");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnZoomInClick(TObject *Sender)
{
    // 지도 확대
    ExecScript("zoomIn()");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnZoomOutClick(TObject *Sender)
{
    // 지도 축소
    ExecScript("zoomOut()");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnZoomPrevClick(TObject *Sender)
{
    // 지도 이전영역 이동
    ExecScript("mapPrev()");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnZoomNextClick(TObject *Sender)
{
    // 지도 다음영역 이동
    ExecScript("mapNext()");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnRefreshClick(TObject *Sender)
{
    // 지도 새로고침
    ExecScript("refreshMap()");
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::ChkShowNmbrClick(TObject *Sender)
{
    String sJs;
    sJs.printf(L"showLayerText(%d, %d)", FLayerIdx, (ChkShowNmbr->Checked) ? 1: 0);
    ExecScript(sJs);
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnMapClickClick(TObject *Sender)
{
    String sJs;
    sJs.printf(L"setMouseClick(%d)", (BtnMapClick->SpeedButtonOptions->Down) ? 1: 0);
    ExecScript(sJs);
    EdMapPos->Visible = BtnMapClick->SpeedButtonOptions->Down;
    EdMapPos->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::PnlGisToolDblClick(TObject *Sender)
{
    if (EdMapPos->Visible)
    {
        ExecScript("setMouseClick(0)");
    }
    else
    {
        ExecScript("setMouseClick(1)");
    }
    EdMapPos->Visible = !EdMapPos->Visible;
    EdMapPos->Text = "";
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::BtnMapSelectClick(TObject *Sender)
{
//
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEOpenMap::WebBrowser1BeforeNavigate2(TObject *ASender, const IDispatch *pDisp, OleVariant &URL, OleVariant &Flags,
          OleVariant &TargetFrameName, OleVariant &PostData, OleVariant &Headers, WordBool &Cancel)
{
    bool bFirst  = false;
    bool bCancel = false;
    String sUrl, sFunc, sArgs;

    try
    {
        sUrl = URL.operator UnicodeString();
        URL.VOleStr = NULL;

        int nFuncIdx = sUrl.Pos(":");
        sFunc = sUrl.SubString(0,          nFuncIdx-1).LowerCase();
        sArgs = sUrl.SubString(nFuncIdx+1, sUrl.Length()).LowerCase();

        WriteLog("mapEvent: %s, %s", AnsiString(sFunc).c_str(), AnsiString(sArgs).c_str());
        Application->ProcessMessages();
        if (nFuncIdx > 0 && sFunc.Pos("onmap"))
        {
            TStringList *pStringList = new TStringList;
            pStringList->CommaText = sArgs;

            try
            {
                String sZoom      = pStringList->Strings[0];
                String sCenterLat = pStringList->Strings[1];
                String sCenterLng = pStringList->Strings[2];

                FMapZoom = sZoom.ToIntDef(0);
                FCenterX = sCenterLat.ToDouble();
                FCenterY = sCenterLng.ToDouble();
                LblZoom->Caption = "Zoom: " + sZoom;
            }
            catch(Exception &e)
            {
            }
            delete pStringList;

            if (sFunc == "onmapzoomchanged")
            {
            }
            else
            if (sFunc == "onmapdragstart")
            {
            }
            else
            if (sFunc == "onmapdragend")
            {
            }
             else
            if (sFunc == "onmapcompleted")
            {
                FMapStart = true;
                PnlToolbar->Enabled = true;
                bFirst = true;
                try
                {
                    DeleteFile(FHtmlFileName);
                    FHtmlFileName = "";
                }
                catch(Exception &e) {}
            }
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onmouseclick"))
        {
            bCancel = true;
            EdMapPos->Text = sArgs;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onfacilityclick"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onfacilityrightclick"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onfacilitydragstart"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onfacilitydragmove"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onfacilitydragend"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onlinkclick"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onlinkrightclick"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("ondebug"))
        {
            bCancel = true;
        }
        else
        if (nFuncIdx > 0 && sFunc.Pos("onlayervisiblechanged"))
        {
            bCancel = true;
        }
    }
    catch(Exception &e)
    {
    }
    Cancel = bCancel;

    try
    {
        if (bCancel && MapCallbackFunc)
        {
            MapCallbackFunc(ASender, sFunc, sArgs, bFirst);
        }
    }
    catch(Exception &exception) {}

}
//---------------------------------------------------------------------------
void TFRAMEOpenMap::WriteLog(char *AFmt, ...)
{
    if (!reMsg->Visible) return;

    va_list ap;
    int     cnt;
    char    szFmtData[2048];

    try
    {
        va_start(ap, AFmt);
        cnt = vsprintf(szFmtData, AFmt, ap);
        va_end(ap);

        reMsg->Lines->Add(Now().FormatString("nn:ss.zzz") + ", " + String(szFmtData));
    }
    catch(Exception &exception)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEOpenMap::LblZoomDblClick(TObject *Sender)
{
    reMsg->Visible = !reMsg->Visible;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEOpenMap::reMsgDblClick(TObject *Sender)
{
    reMsg->Lines->Clear();
}
//---------------------------------------------------------------------------

