#include "maplayerwidget.h"

#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGroupBox>
#include <QTreeView>
#include <QListWidget>
#include <QScrollArea>
#include <QPushButton>
#include <QToolButton>
#include <QSplitter>
#include <QDebug>

#include "treemodel.h"
#include "treeitem.h"

MapLayerWidget::MapLayerWidget(QWidget *parent)
    :QWidget(parent)
{
    initUI();

    connects();
}

void MapLayerWidget::setRootItem(TreeItem *item)
{
    mMapLayerTreeModel->setRootItem(item);
    mMapLayerTreeModel->updateData();
}

void MapLayerWidget::initUI()
{
    mVSplitter = new QSplitter;
    mVSplitter->setOrientation(Qt::Vertical);

    mMainHLayout = new QHBoxLayout;
    mMainHLayout->addWidget(mVSplitter);

    mMapLayerWidget = new QWidget;
    mMapLayerVLayout = new QVBoxLayout;
    mMapLayerWidget->setLayout(mMapLayerVLayout);

    mMapLayerTreeView = new QTreeView;
    // mMapLayerTreeView->setRootIsDecorated(false);
    const QStringList headers({ "节点" });
    mMapLayerTreeModel = new TreeModel(headers, "");
    mMapLayerTreeView->setModel(mMapLayerTreeModel);
    mMapLayerVLayout->addWidget(mMapLayerTreeView);

    mLayerSelectHLayout = new QHBoxLayout;
    mSelectAllButton = new QPushButton("全选");
    mSelectNoAllButton = new QPushButton("全不选");
    mLayerSelectHLayout->addWidget(mSelectAllButton);
    mLayerSelectHLayout->addWidget(mSelectNoAllButton);
    mMapLayerVLayout->addLayout(mLayerSelectHLayout);


    mVSplitter->addWidget(mMapLayerWidget);

    this->setLayout(mMainHLayout);

}

void MapLayerWidget::connects()
{
    connect(mMapLayerTreeModel, &QAbstractItemModel::dataChanged,
            this, &MapLayerWidget::onTreeDataChanged);

    connect(mSelectAllButton, &QPushButton::clicked, this, [=](){
        TreeItem* rootItem = mMapLayerTreeModel->getRootItem();
        int count = rootItem->childCount();
        for(int i = 0; i < count; i++)
        {
            TreeItem* item = rootItem->child(i);
            item->setChecked(true);
            QString name = item->data(0).toString();
            emit mapLayerVisible(name, item->isChecked());
        }

        mMapLayerTreeModel->updateData();
    });

    connect(mSelectNoAllButton, &QPushButton::clicked, this, [=](){
        TreeItem* rootItem = mMapLayerTreeModel->getRootItem();
        int count = rootItem->childCount();
        for(int i = 0; i < count; i++)
        {
            TreeItem* item = rootItem->child(i);
            item->setChecked(false);
            QString name = item->data(0).toString();
            emit mapLayerVisible(name, item->isChecked());
        }

        mMapLayerTreeModel->updateData();
    });

}

void MapLayerWidget::onTreeDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
{
    TreeItem *item = static_cast<TreeItem*>(topLeft.internalPointer());
    if (item->checkable(topLeft.column()))
    {
        QString name = item->data(0).toString();
//        if (item->isChecked())
//        {
//            qDebug() << "checked:" << name;
//        }
//        else
//        {
//            qDebug() << "unchecked:" << name;
//        }
        emit mapLayerVisible(name, item->isChecked());
    }
}
