#include <QMainWindow>
#include <QWidget>
#include <QToolButton>
#include <qboxlayout.h>
#include <qlabel.h>
#include <qlistview.h>
#include <qnamespace.h>
#include <qstandarditemmodel.h>
#include <qstringlistmodel.h>
#include <qtoolbutton.h>
#include <Qt>
#include <qwidget.h>
#include <QStandardItemModel>
#include <QDropEvent>

#include "trainWindow.h"

QWidget     *wMaster;	
QVBoxLayout *hMaster;

QWidget     *questionBox;	
QVBoxLayout *vQuestionBox;
QLabel      *lQuestionText;
QLabel      *answerText;

QVBoxLayout *vButtonBox;
QWidget     *actionButtons;
QHBoxLayout *hButtons;
QToolButton *btnPrev;
QToolButton *btnShowAnswer;
QToolButton *btnNext;

QMainWindow *trainWindow;
QWidget     *trainWidget;
QVBoxLayout *vTrainWidget;

class CustomListView : public QListView
{
    Q_OBJECT

public:
    explicit CustomListView(QWidget *parent = nullptr) : QListView(parent) {}

protected:
    void dropEvent(QDropEvent *event) override
    {
        QModelIndex sourceIndex = currentIndex();
        QModelIndex targetIndex = indexAt(event->pos());

        if (sourceIndex.isValid() && targetIndex.isValid()) {
            QStandardItemModel *model = qobject_cast<QStandardItemModel *>(this->model());
            if (model) {
                QVariant sourceData = model->data(sourceIndex);
                QVariant targetData = model->data(targetIndex);
                model->setData(sourceIndex, targetData);
                model->setData(targetIndex, sourceData);
                event->ignore();
            }
        } else {
			event->ignore();
        }
    }
};
#include "trainWindow.moc"

void initTrainWindow() {
    // # Types of question
	//
    // - AnswerQuestion:
    //  	One answer (could be on multiple lines).
    // - ChoiceQuestion:
    //      One or many correct answers that are showed to user.
    // - OrderQuestion:
	//      Many items that should be ordered in a list.
    // - MatchQuestion:
	//      Match some items to other items.

    trainWindow  = new QMainWindow();
	trainWidget  = new QWidget();
	vTrainWidget = new QVBoxLayout();

	/*wMaster       = new QWidget();*/
	/*hMaster       = new QVBoxLayout();*/
	trainWidget->setLayout(vTrainWidget);
    trainWindow->setCentralWidget(trainWidget);
	trainWidget->setLayout(vTrainWidget);
	vTrainWidget->setAlignment(Qt::AlignCenter);
	trainWidget->setObjectName("answer-question-widget");

	{ // Make the question box.
		vQuestionBox  = new QVBoxLayout();
		questionBox   = new QWidget();
		questionBox->setLayout(vQuestionBox);
		questionBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Expanding);
		vQuestionBox->setAlignment(Qt::AlignCenter);
	}

	{ // Make question text. 
		lQuestionText = new QLabel();
		lQuestionText->setText("What is the capital of Latvia?");
		lQuestionText->setWordWrap(true);
		lQuestionText->setStyleSheet(QString(
			"QLabel {"
				"font-size: 20px;"
			"}"
		));
		lQuestionText->setTextInteractionFlags(Qt::TextSelectableByMouse);
		vQuestionBox->addWidget(lQuestionText);
	}

	{ // Make answer text.
		answerText = new QLabel();
		answerText->setText("- Riga\n- Second line");
		answerText->setWordWrap(true);
		answerText->hide();
		answerText->setStyleSheet(QString(
			"QLabel {"
				"font-size: 15px;"
			"}"
		));
		answerText->setTextInteractionFlags(Qt::TextSelectableByMouse);
		vQuestionBox->addWidget(answerText);
	}


	auto listStyle = 
			"QListView {"
			"   background-color: white;"
			"   border: 1px solid black;"
			"}"
			"QListView::item::first {"
			"   padding: 5px 0 0 0;"
			"}"
			"QListView::item {"
			"   color: blue;"
			"   padding: 5px;"
			"   border: 1px solid gray;"
			"   background-color: white;"
			"}"
			"QListView::item:selected {"
			"}"
			"QListView::item:hover {"
			"   background-color: lightyellow;"
			"}";

	{ // Make multi-choice list.
		auto *multiChoiceModel = new QStandardItemModel();
		QListView *listView = new QListView();
		listView->setModel(multiChoiceModel);

		auto *firstItem = new QStandardItem(); 
		firstItem->setText("Item 1");
		firstItem->setCheckable(true);
		auto *secondItem = new QStandardItem(); 
		secondItem->setText("Item 2");
		secondItem->setCheckable(true);
		multiChoiceModel->setItem(0, firstItem);
		multiChoiceModel->setItem(1, secondItem);

		listView->setStyleSheet(listStyle);

		questionBox->layout()->addWidget(listView);
		listView->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
		listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
	}

	{ // Make order list.
		auto *orderModel = new QStandardItemModel();
		QListView *orderList = new CustomListView();
		orderList->setModel(orderModel);

		auto *firstItem = new QStandardItem(); 
		firstItem->setText("Item 1");
		auto *secondItem = new QStandardItem(); 
		secondItem->setText("Item 2");
		auto *thirdItem = new QStandardItem(); 
		thirdItem->setText("Item 3");
		orderModel->setItem(0, firstItem);
		orderModel->setItem(1, secondItem);
		orderModel->setItem(2, thirdItem);
		 

		questionBox->layout()->addWidget(orderList);
		orderList->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
		orderList->setEditTriggers(QAbstractItemView::NoEditTriggers);
		orderList->setDragDropMode(QAbstractItemView::InternalMove);
		orderList->setFocusPolicy(Qt::NoFocus);

		orderList->setStyleSheet(listStyle);

		// Connect to handle the drop event properly
		QObject::connect(
			 orderModel,
			 &QStandardItemModel::rowsMoved,
			 [orderModel](const QModelIndex &, int sourceRow, int, const QModelIndex &, int destinationRow) {
			     if (sourceRow != destinationRow - 1) {
					 auto *movedItem = orderModel->takeItem(sourceRow);
					 orderModel->insertRow(destinationRow > sourceRow ? destinationRow - 1 : destinationRow, movedItem);
				 }
			}
		);
	}

	{ // Make buttons menu. There are left middle and right buttons.
		vButtonBox    = new QVBoxLayout();
		actionButtons = new QWidget();
		hButtons      = new QHBoxLayout();
		btnPrev       = new QToolButton();
		btnShowAnswer = new QToolButton();
		btnNext       = new QToolButton();

		hButtons->addWidget(btnPrev);
		hButtons->addWidget(btnShowAnswer);
		hButtons->addWidget(btnNext);
		vButtonBox->addWidget(actionButtons);
		actionButtons->setLayout(hButtons);
		btnPrev->setText("Previous");
		btnShowAnswer->setText("Show answer");
		btnNext->setText("Next");
		QObject::connect(btnShowAnswer, &QToolButton::clicked, [](bool checked) {
			answerText->show();
			btnShowAnswer->hide();
		});		
		actionButtons->setStyleSheet(QString(
			"QToolButton {"
				"padding: 4px 5px;"
				"font-size: 15px;"
			"}"
		));
		questionBox->setObjectName("question-box");
	}


	vTrainWidget->addWidget(questionBox);
	vTrainWidget->addWidget(actionButtons);
	vTrainWidget->addWidget(wMaster);
}
