#include "clistwidget.h"

#include <QVBoxLayout>
#include <QLabel>
#include <QListWidget>
#include <QListWidgetItem>
#include <QMouseEvent>
#include <QMimeData>
#include <QDrag>
#include <QPainter>
#include <QListWidgetItem>

#include <QDebug>

CListWidget::CListWidget(QWidget *parent)
    : QListWidget(parent)
{
    initUI();
}

void CListWidget::mousePressEvent(QMouseEvent *event)
{
    QListWidgetItem *item = itemAt(event->pos());

    qDebug() << "widget:" << item->text();

    if(!item)
        return;


    QPixmap pixmap = item->icon().pixmap(30, 30);
    //QPoint point = event->pos();

    QByteArray itemData;
    QDataStream dataStream(&itemData, QIODevice::WriteOnly);
    dataStream << pixmap << QPoint(15, 15) << item->text();

    QMimeData *mimeData = new QMimeData;
    mimeData->setData("application/x-dnditemdata", itemData);

    QDrag *drag = new QDrag(this);
    drag->setMimeData(mimeData);
    drag->setPixmap(pixmap);
    drag->setHotSpot(QPoint(15, 15));

    QPixmap tempPixmap = pixmap;
    QPainter painter;
    painter.begin(&tempPixmap);
    painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
    painter.end();

    QLabel* child = new QLabel;
    child->setPixmap(tempPixmap);

    if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction) {
        child->close();
    } else {
//        child->show();
//        child->setPixmap(pixmap);
        child->close();
    }

    QListWidget::mousePressEvent(event);
}

void CListWidget::initUI()
{
    mIconNames << "船1(蓝)" << "船2(蓝)" << "船3(蓝)" << "船4(蓝)" << "船5(蓝)" << "船6(蓝)"
               << "障碍物1(蓝)" << "障碍物2(蓝)" << "障碍物3(蓝)" << "障碍物4(蓝)" << "障碍物5(蓝)" << "障碍物6(蓝)" << "障碍物7(蓝)"
               << "船1(红)" << "船2(红)" << "船3(红)" << "船4(红)" << "船5(红)" << "船6(红)"
               << "障碍物1(红)" << "障碍物2(红)" << "障碍物3(红)" << "障碍物4(红)" << "障碍物5(红)" << "障碍物6(红)" << "障碍物7(红)"
               << "一级机场" << "二级机场" << "三级机场" << "洞库机场" << "特级机场"
               << "无人驾驶电子干扰机" << "无人驾驶飞机" << "侦察机" << "反辐射飞机" << "反辐射飞机1" << "中远程轰炸机" << "歼击轰炸机" << "电子干扰机"
               << "电子干扰机1"<<  "空中指挥机"<<  "空中指挥机1" <<  "预警指挥机" << "预警机" << "预警机1" <<  "轰炸机"
               << "加油机" << "运输机"<< "空中机群" << "空中机群（军标可替换）"
               << "引导雷达"<< "机动警戒雷达"<< "测高雷达" << "炮瞄雷达" << "警戒雷达" << "预警雷达" << "地波超视距雷达" << "地空导弹制导雷达"
               <<  "伪导弹部队部署阵地" << "导弹发射阵地" << "导弹营野战发射阵地" << "导弹部队部署地域（军标可替换）" << "导弹部队部署阵地"
               << "导弹部队部署阵地（军标可替换）" <<  "野战机场" << "预设导弹发射场坪";

//    mIconListWidget->setViewMode(QListView::IconMode);
//    mIconListWidget->setResizeMode(QListView::Adjust);
//    mIconListWidget->setFlow(QListView::LeftToRight);
    this->setDragEnabled(true);

    for(int i = 0; i < 6; i++)
    {
        QListWidgetItem* item = new QListWidgetItem;
        item->setIcon(QIcon(QString(":/images/ship_obstacle/blue_ship0%1.png").arg(i + 1)));
        item->setText(mIconNames.at(i));
        this->addItem(item);
    }

    for(int i = 6; i < 13; i++)
    {
        QListWidgetItem* item = new QListWidgetItem;
        item->setIcon(QIcon(QString(":/images/ship_obstacle/blue_obstacle0%1.png").arg(i + 1 - 6)));
        item->setText(mIconNames.at(i));
        this->addItem(item);
    }

    for(int i = 13; i < 19; i++)
    {
        QListWidgetItem* item = new QListWidgetItem;
        item->setIcon(QIcon(QString(":/images/ship_obstacle/red_ship0%1.png").arg(i + 1 - 13)));
        item->setText(mIconNames.at(i));
        this->addItem(item);
    }

    for(int i = 19; i < 26; i++)
    {
        QListWidgetItem* item = new QListWidgetItem;
        item->setIcon(QIcon(QString(":/images/ship_obstacle/red_obstacle0%1.png").arg(i + 1 - 19)));
        item->setText(mIconNames.at(i));
        this->addItem(item);
    }

    for(int i = 26; i < mIconNames.size(); i++)
    {
        QListWidgetItem* item = new QListWidgetItem;
        item->setIcon(QIcon(QString(":/images/military_icon/%1.png").arg(i + 1 - 26)));
        item->setText(mIconNames.at(i));
        this->addItem(item);
    }
}
