#include "mainwindow.h"

#include <QHBoxLayout>
#include <QToolButton>
#include <QLabel>
#include <QFileDialog>
#include <QDockWidget>
#include <QLabel>
#include <QStatusBar>
#include <QSplitter>

#include "global.h"
#include "cmapview.h"
#include "menu.h"
#include "layercontrolwidget.h"
#include "infodisplaywidget.h"
#include "maplayerwidget.h"
#include "iconlayerwidget.h"
#include "entityproperty.h"
#include "entitywidget.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
    :QMainWindow(parent, flags)
{
    initUI();
    connects();
}

MainWindow::~MainWindow()
{

}

void MainWindow::setMenuVisiable()
{
    this->menuBar()->setVisible(!this->menuBar()->isVisible());
}

void MainWindow::openLocalFile()
{
    if(mLastDirPath.isEmpty())
    {
        mLastDirPath = Global::Instance()->rootDirPath();
    }
    QString fileName = QFileDialog::getOpenFileName(this,
                                            "打开本地地图", mLastDirPath, "地图文件 (*.shp *.000 *.tpk)");
    if(!fileName.isEmpty())
    {
        mMapView->openLocalMapFile(fileName);

        QFileInfo info(fileName);
        mLastDirPath = info.dir().path();
    }
}

void MainWindow::initUI()
{
    initMenu();

    LayerParam p = Global::Instance()->layerParam();
    mMapView = new CMapView;
    mMapView->setLayerParam(p);
    mMapView->updateLayer();

    mLayerControlWidget = new LayerControlWidget;
    mLayerControlWidget->getMapLayerWidget()->setRootItem(mMapView->mapLayerTreeRootItem());
    mLayerControlDockWidget = new QDockWidget("层控制", new QWidget);
    mLayerControlDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea) ;
    mLayerControlDockWidget->setWidget(mLayerControlWidget);
    this->addDockWidget(Qt::LeftDockWidgetArea, mLayerControlDockWidget);

    mInfoDisplayWidget = new InfoDisplayWidget;
    mInfoDisplayDockWidget = new QDockWidget("信息显示", new QWidget);
    mInfoDisplayDockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
    mInfoDisplayDockWidget->setWidget(mInfoDisplayWidget);
    mInfoDisplayDockWidget->setMinimumWidth(360);
    this->addDockWidget(Qt::RightDockWidgetArea, mInfoDisplayDockWidget);
    mInfoDisplayDockWidget->hide();


    this->setCentralWidget(mMapView);

    mStatusLabel = new QLabel;
    this->statusBar()->addPermanentWidget(mStatusLabel);
}



void MainWindow::initMenu()
{
    QMenu* fileMenu = this->menuBar()->addMenu("文件");
    QMenu* settingMenu = this->menuBar()->addMenu("设置");
    QMenu* aboutMenu = this->menuBar()->addMenu("关于");

    mMenu = new Menu(fileMenu, settingMenu, aboutMenu);
    this->menuBar()->hide();
}

void MainWindow::connects()
{
    connect(mMenu->openFileAction, &QAction::triggered, this, &MainWindow::openLocalFile);
    connect(mMenu->exitAction, &QAction::triggered, this, &MainWindow::quitProgram);

    connect(mMenu->layerControlAction, &QAction::triggered, this, [=](){
        mLayerControlDockWidget->setVisible(!mLayerControlDockWidget->isVisible());
    });
    connect(mMenu->mapxVisibleAction, &QAction::triggered, mMapView, &CMapView::setMapXWidgetVisible);
    connect(mMenu->navigationVisibleAction, &QAction::triggered, mMapView, &CMapView::setNavigationWidgetVisible);

    connect(mMenu->infoControlAction, &QAction::triggered, this, [=](){
        mInfoDisplayDockWidget->setVisible(!mInfoDisplayDockWidget->isVisible());
    });

    connect(mLayerControlWidget->getMapLayerWidget(), &MapLayerWidget::mapLayerVisible, mMapView, &CMapView::mapLayerVisible);
    connect(mLayerControlWidget->getIconLayerWidget(), &IconLayerWidget::clearAllIcon, mMapView, &CMapView::clearAllIcon);

    connect(mMapView, &CMapView::mousePosToLLA, this, [=](QPointF p){
        mStatusLabel->setText(QString("位置: (%1, %2)").arg(p.x()).arg(p.y()));
    });

    connect(mMapView->armyIconLayer(), &ArmyIconLayer::selectedEntity,
            mInfoDisplayWidget->getEntityPropertyWidget(), &EntityProperty::setID);

    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::allEntityInfo, mMapView->armyIconLayer(), &ArmyIconLayer::setEntityList);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::allLongitudeLatitudeAltitude, mMapView->armyIconLayer(), &ArmyIconLayer::moveEntityList);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::allLongitudeLatitudeAltitude, mInfoDisplayWidget->getEntityPropertyWidget(), &EntityProperty::setEntityRealTimeInfo);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::entityVisible, mMapView->armyIconLayer(), &ArmyIconLayer::setEntityVisible);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::entityLocation, mMapView->armyIconLayer(), &ArmyIconLayer::setEntityLocation);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::entityRouteVisible, mMapView->armyIconLayer(), &ArmyIconLayer::setEntityRouteVisible);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::entityNameVisible, mMapView->armyIconLayer(), &ArmyIconLayer::setEntityNameVisible);
    connect(mLayerControlWidget->getEntityWidget(), &EntityWidget::entityFocusing, mMapView->armyIconLayer(), &ArmyIconLayer::setEntityFocusing);


}

void MainWindow::quitProgram()
{
    //std::exit(0);
    this->close();
    emit signalQuit();
}

