#ifndef MAPVIEW_H
#define MAPVIEW_H

#include "Map.h"
#include "MapGraphicsView.h"
#include "Map.h"
#include "MapGraphicsView.h"
#include "EncExchangeSet.h"
#include "EncLayer.h"
#include "EncDataset.h"
#include "EncCell.h"
#include "EncEnvironmentSettings.h"
#include "Envelope.h"
#include "GeometryEngine.h"

#include "armyiconlayer.h"

#include "global.h"

#include "treeitem.h"

class QDragEnterEvent;
class QDropEvent;

class MapXWidget;
class NavigationFloatItem;

class CMapView : public Esri::ArcGISRuntime::MapGraphicsView
{
    Q_OBJECT
public:
    explicit CMapView(QWidget* parent = nullptr);
    explicit CMapView(Esri::ArcGISRuntime::Map* map, QWidget* parent = nullptr);
    ~CMapView() override;

    void setLayerParam(LayerParam p);

    void updateLayer();

    void openLocalMapFile(const QString& fileName);

    void setMapXWidgetVisible();
    void setNavigationWidgetVisible();

    TreeItem* mapLayerTreeRootItem();


    void clearAllIcon();

    ArmyIconLayer *armyIconLayer();

public slots:
    void mapLayerVisible(const QString& name, bool state);

protected:
    bool event(QEvent* e) override;

protected:
    void dragEnterEvent(QDragEnterEvent *event) override;
    void dragMoveEvent(QDragMoveEvent *event) override;
    void dropEvent(QDropEvent *event) override;

    void mouseMoveEvent(QMouseEvent* event) override;

signals:
    /**
    * @brief 提示信号
    * @param txt 提示的内容
    * @param index 提示索引, 0普通信号 1警告信号 2 致命信号
    **/
    void signalTip(const QString& txt, int index);

    /**
    * 视点信号
    **/
    void viewpoint(QPointF P);

    /**
    * @brief 鼠标所在位置的经纬度
    **/
    void mousePosToLLA(QPointF p);

private:
    void initUI(Esri::ArcGISRuntime::Map* map = nullptr);
    void connects();

    /**
    * @brief 窗口界面中局部位置，转为经纬度
    **/
    QPointF localPointToQPoint(QPointF p);
    Esri::ArcGISRuntime::Point localPointToArcgisPoint(QPointF p);

private slots:
    void onMapViewAndScaleChanged();

private:
    Esri::ArcGISRuntime::Map* mMap;
    Esri::ArcGISRuntime::EncExchangeSet* mEncExchangeSet;
    QList<Esri::ArcGISRuntime::Geometry> m_extents;

    MapXWidget* mMapXWidget;
    NavigationFloatItem* mNavigationWidget;

    LayerParam mLayerParam;

    // 地图当前的视点
    Esri::ArcGISRuntime::Point            m_viewPoint;
    //地图显示的像素长宽
    int m_mapwidth;
    int m_mapheight;

    // 地图最大最小缩放比率
    qreal m_maxScale;
    qreal m_minScale;

    // 地图当前缩放大比例
    double m_currentScale;

    TreeItem* mMapLayerTreeRootItem;

    ArmyIconLayer* mArmyIconOverlay;
};

#endif // MAPVIEW_H
