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

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

#include "FRAME_GoogleMapF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "GMClasses"
#pragma link "GMMap"
#pragma link "GMMapVCL"
#pragma link "GMLinkedComponents"
#pragma link "GMMarker"
#pragma link "GMMarkerVCL"
#pragma link "cxCheckBox"
#pragma link "cxButtons"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxLabel"
#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 resource "*.dfm"
TFRAMEGoogleMap *FRAMEGoogleMap;
//---------------------------------------------------------------------------
__fastcall TFRAMEGoogleMap::TFRAMEGoogleMap(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
    ITSSkin_Load((TForm*)this);
    LoadLocalSkin();

    FOnDrawFacilityClick = NULL;
    FOnZoomChangeClick   = NULL;

    memset((char*) & FPrev,   0x00, sizeof(FPrev));
    memset((char*) & FExtent, 0x00, sizeof(FExtent));
    FExtent.Extent = true;
    FExtent.TotCnt = 0;
    FExtent.CurIdx = 0;

    FMapZoom  = g_GisInfo.nZoomLevel;
    FBaseZoom = FMapZoom;
    GMMap1->RequiredProp->Zoom = FBaseZoom;
    GMMap1->RequiredProp->Center->Lat = g_GisInfo.dCenterX;
    GMMap1->RequiredProp->Center->Lng = g_GisInfo.dCenterY;

    FMapTypeId = mtTERRAIN;
    LblZoom->Caption = "Zoom: " + String(FMapZoom) + "  ";

    FCenLatLng = new TLatLng(GMMap1->RequiredProp->Center->Lat, GMMap1->RequiredProp->Center->Lng);
    FMapBounds = new TLatLngBounds(0, 0, 0, 0);

    FCenterLat = GMMap1->RequiredProp->Center->Lat;
    FCenterLng = GMMap1->RequiredProp->Center->Lng;
    //LoadGisMap();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::LoadLocalSkin()
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::LoadGisMap()
{
    if (!GMMap1->Active)
    {
        try {
            if (ImgLoading->Picture->Graphic->ClassName() == "TGIFImage")
                ((TGIFImage *)ImgLoading->Picture->Graphic)->Animate = true;
            Application->ProcessMessages();
            ImgLoading->Visible = true;
        } catch(...) {}
        GMMap1->Active = true;
    }
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::SetLinkLevel(int ALinkLevel)
{
    FLinkLevel = ALinkLevel;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::ShowHideTrafficLayer(bool AShow)
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::SelectObjectByPos(double dPosX, double dPosY, bool bArrow/*=false*/)
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::SelectObjectByPos()
{
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEGoogleMap::SelectLinkByPos(int X, int Y, String &ALinkId)
{
    return false;
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEGoogleMap::GetLevelLinkId(int ALinkLevel, String &sLinkId, int X, int Y)
{
    return false;
}
//---------------------------------------------------------------------------

int __fastcall TFRAMEGoogleMap::GetLayerIndex()
{
    return 0;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::SelGisItem(int AType, String AId)
{
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::OnCloseQuery(bool &CanClose)
{
}
//---------------------------------------------------------------------------

bool __fastcall TFRAMEGoogleMap::AddExtentQ(double ALat, double ALng, int FZoom)
{
    TMapExtent *pExt = &FExtent.extent[FExtent.TotCnt];
    pExt->Lat = ALat;
    pExt->Lng = ALng;
    pExt->Zoom = (short)FZoom;

    if (FPrev.Lat == pExt->Lat && FPrev.Lng == pExt->Lng && FPrev.Zoom == pExt->Zoom)
    {
        return false;
    }

    // MAPDEBUG("AddExtentQ: " + String(FExtent.TotCnt));
    FPrev.Lat = pExt->Lat;
    FPrev.Lat = pExt->Lng;
    FPrev.Zoom = pExt->Zoom;

    FExtent.TotCnt++;
    if (FExtent.TotCnt >= D_EXT_QUEUE_SIZE)
    {
        FExtent.TotCnt = D_EXT_QUEUE_SIZE;

        memcpy((char*) & FExtent.extent[0], (char*) & FExtent.extent[1], sizeof(TMapExtent) * D_EXT_QUEUE_SIZE);
    }

    FExtent.CurIdx = FExtent.TotCnt - 1;
    if (FExtent.TotCnt > 1)
    {
        // BtnZoomPrev->Enabled = true;
    }

    return true;
}
//---------------------------------------------------------------------------
/*!
 * @fn  	TFRMFC090000::PrevExtentQ
 * @brief   PrevExtentQ
 * @param   void
 * @return  bool   	결과
 */
bool __fastcall TFRAMEGoogleMap::PrevExtentQ()
{
    FExtent.Extent = false;

    if (FExtent.CurIdx <= 0)
    {
        FExtent.CurIdx = 0;
        return false;
    }

    FExtent.CurIdx--;
    if (FExtent.CurIdx == 0)
    {
        // BtnZoomPrev->Enabled = false;
    }
    try
    {
        TMapExtent *pExt = &FExtent.extent[FExtent.CurIdx];
        if (GMMap1->Active)
        {
            GMMap1->SetCenter(pExt->Lat, pExt->Lng);
            GMMap1->RequiredProp->Zoom = (int)pExt->Zoom;
        }
        // BtnZoomNext->Enabled = true;
    }
    catch(...)
    {
    }

    return true;
}
//---------------------------------------------------------------------------
bool __fastcall TFRAMEGoogleMap::NextExtentQ()
{
    FExtent.Extent = false;

    if (FExtent.CurIdx >= FExtent.TotCnt - 1)
    {
        FExtent.CurIdx = FExtent.TotCnt - 1;
        return false;
    }

    FExtent.CurIdx++;
    if (FExtent.CurIdx == FExtent.TotCnt - 1)
    {
        // BtnZoomNext->Enabled = false;
    }

    try
    {
        TMapExtent *pExt = &FExtent.extent[FExtent.CurIdx];
        if (GMMap1->Active)
        {
            GMMap1->SetCenter(pExt->Lat, pExt->Lng);
            GMMap1->RequiredProp->Zoom = (int)pExt->Zoom;
        }
        // BtnZoomPrev->Enabled = true;
    }
    catch(...)
    {
    }

    return true;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::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 TFRAMEGoogleMap::GMMap1AfterPageLoaded(TObject *Sender, bool First)
{
    if (First)
    {
        GMMap1->DoMap();
        GMMap1->Tag = 1;
        PnlToolbar->Enabled = true;
        ImgLoading->Visible = false;
        try {
            if (ImgLoading->Picture->Graphic->ClassName() == "TGIFImage")
                ((TGIFImage *)ImgLoading->Picture->Graphic)->Animate = false;
        } catch(...) {}
    }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::GMMap1BoundsChanged(TObject *Sender, TLatLngBounds *NewBounds)
{
    if (FMapBounds)
    {
        FMapBounds->SW->Lat = NewBounds->SW->Lat;
        FMapBounds->SW->Lng = NewBounds->SW->Lng;
        FMapBounds->NE->Lat = NewBounds->NE->Lat;
        FMapBounds->NE->Lng = NewBounds->NE->Lng;
    }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::GMMap1ZoomChanged(TObject *Sender, int NewZoom)
{
    FMapZoom = NewZoom;
    try
    {
        int nLineWidth;
        if (FMapZoom > 14)
            nLineWidth = 5;
        else
            nLineWidth = 3;

#if 0
        if (nLineWidth == FLineWidth)
            return;

        FLineWidth = nLineWidth;

        try
        {
            GMPolyline1->Map = NULL;
            int nPolylines = GMPolyline1->Count;
            for (int ii = nPolylines - 1; ii >= 0; ii--)
            {
                TPolyline *pTmpLine = GMPolyline1->Items[ii];
                pTmpLine->StrokeWeight = FLineWidth;
            }
        }
        __finally
        {
            GMPolyline1->Map = GMMap1;
        }
#endif

        if (FExtent.Extent)
        {
            AddExtentQ(GMMap1->RequiredProp->Center->Lat, GMMap1->RequiredProp->Center->Lng, NewZoom);
        }
        FExtent.Extent = true;
    }
    catch(...)
    {
    }
    LblZoom->Caption = "Zoom: " + String(FMapZoom) + "  ";;
    if (FOnZoomChangeClick) FOnZoomChangeClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::GMMap1DragEnd(TObject *Sender)
{
    if (FExtent.Extent)
    {
        AddExtentQ(GMMap1->RequiredProp->Center->Lat, GMMap1->RequiredProp->Center->Lng, GMMap1->RequiredProp->Zoom);
    }
    FExtent.Extent = true;
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnMapToJpgClick(TObject *Sender)
{
    try
    {
        if (GMMap1->Active)
        {
            SaveDialog1->FileName = Now().FormatString("YYYYMMDDHHNNSS") + ".jpg";
            if (SaveDialog1->Execute())
            {
                GMMap1->SaveToJPGFile(SaveDialog1->FileName);
            }
        }
    } catch(Exception &e) { }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnMoveMapToCenterClick(TObject *Sender)
{
    // 지도 중앙으로 이동
    try
    {
        if (GMMap1->Active)
        {
            int nZoom = GMMap1->VisualProp->ZoomCtrl->Position;
            GMMap1->SetCenter(FCenLatLng);

            if (FExtent.Extent)
            {
                AddExtentQ(GMMap1->RequiredProp->Center->Lat, GMMap1->RequiredProp->Center->Lng, GMMap1->RequiredProp->Zoom);
            }
            FExtent.Extent = true;
        }
    } catch(Exception &e) { }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnMoveDefBoundsClick(TObject *Sender)
{
    // 지도 기본영역으로 이동
    try
    {
        if (GMMap1->Active)
        {
            GMMap1->SetCenter(FCenLatLng);
            GMMap1->RequiredProp->Zoom = FBaseZoom;

            if (FExtent.Extent)
            {
                AddExtentQ(GMMap1->RequiredProp->Center->Lat, GMMap1->RequiredProp->Center->Lng, GMMap1->RequiredProp->Zoom);
            }
            FExtent.Extent = true;
        }
    } catch(Exception &e) { }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnZoomInClick(TObject *Sender)
{
    try
    {
        if (GMMap1->Active)
        {
            GMMap1->RequiredProp->Zoom = FMapZoom + 1;
        }
    } catch(Exception &e) { }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnZoomOutClick(TObject *Sender)
{
    try
    {
        if (GMMap1->Active)
        {
            if (FMapZoom > 0)
            {
                GMMap1->RequiredProp->Zoom = FMapZoom - 1;
            }
        }
    } catch(Exception &e) { }
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnZoomPrevClick(TObject *Sender)
{
    PrevExtentQ();
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEGoogleMap::BtnZoomNextClick(TObject *Sender)
{
    NextExtentQ();
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::GMMap1CenterChanged(TObject *Sender, TLatLng *LatLng,
          double X, double Y)
{
    FCenterLat = LatLng->Lat;
    FCenterLng = LatLng->Lng;
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEGoogleMap::RemoveMarker(TGMMarker *AMarker)
{
    TGMMarker *pObjMarker = (TGMMarker*)AMarker;
    try
    {
        pObjMarker->Map = NULL;
        int nObjs = pObjMarker->Count;
        for (int ii = nObjs - 1; ii >= 0; ii--)
        {
            pObjMarker->Items[ii]->Visible = false;
            pObjMarker->Delete(ii);
        }
        pObjMarker->Clear();
    }
    catch(Exception &e)
    {
    }
}
//---------------------------------------------------------------------------

