#include "InputObject.h"
#include "ui_inputobject.h"
//#include "qdesktopwidget.h"
#include <QDebug>
#include <QEvent>
#include <QKeyEvent>
#include <QComboBox>
#include <QDateEdit>
#include <QtVirtualKeyboard>

#include <QLineEdit>
#include <QAbstractSpinBox>

InputObject::InputObject(QWidget* parent) :
	QWidget(parent),
	ui(new Ui::InputObject)
{
	ui->setupUi(this);

	initUi();

	this->setVisible(false);
	this->setWindowFlags(Qt::Tool | Qt::FramelessWindowHint);


}

InputObject::~InputObject()
{
	delete ui;
}

void InputObject::initUi()
{
	//#3c3c3c
#ifdef CUTE_STYLE
	QString style =
		"QWidget#WiBack{background-image: url(:/icons/bg.jpeg);}"
		"QWidget#WiBo{background-color: black;}"
		"QLineEdit,QDateEdit{background-color: rgba(0,0,0,0.5);\
		min-height:100px;max-height:100px; min-width:500px;max-width:500px;\
		border: 2px solid #ef9cba; border-radius:20px;\
		color:white;margin:0;font-size:36px;}"
		"QTextEdit{background-color: rgba(0,0,0,0.5);\
		min-height:300px;max-height:300px; min-width:700px;max-width:700px;\
		border: 2px solid #ef9cba; border-radius:20px;\
		font-family:Arial; color:white;margin:0;font-size:36px;}"
		"QDateEdit::up-button{image: url(:/up.png);height:15px;}"
		"QDateEdit::down-button{image:url(:/down.png);height:15px;}"
		;
#else
	QString style =
		"QWidget#WiBack{background-color: #383533;}"
		"QWidget#WiBo{background-color: black;}"
		"QLineEdit,QDateEdit,QTextEdit{\
		 background:qlineargradient(x1:0,y1:0,x2:1,y2:1,stop: 0.0 silver, stop: 1.0 grey);\
		 border: 1px solid #1e1b18; border-radius:20px;\
		 color:black;margin:0;font-size:36px;}"

		"QLineEdit,QDateEdit{min-height:100px; max-height:100px; min-width:500px; max-width:500px;}"
		"QTextEdit{min-height:300px;max-height:300px; min-width:700px;max-width:700px;}"

		"QDateEdit::up-button{image: url(:/up.png);height:15px;}"
		"QDateEdit::down-button{image:url(:/down.png);height:15px;}"
		;
#endif
	setStyleSheet(style);

	ui->dateEdit->setVisible(false);
	ui->dateEdit->setMaximumDate(QDate::currentDate());
	ui->dateEdit->setDisplayFormat("yyyy/MM/dd");
	ui->dateEdit->setStyleSheet(style);
	//this->setWindowOpacity(0.8);

	connect(QGuiApplication::inputMethod(), &QInputMethod::visibleChanged, [=]() {
		if (QGuiApplication::inputMethod()->isVisible()) return;
		//qDebug() << "HidePanel";
		//qDebug() << "focusWidget" << qApp->focusWidget();
		this->HidePanel();
		this->hide();

		});
}

bool InputObject::eventFilter(QObject* obj, QEvent* event)
{

	if (event->type() == QEvent::MouseButtonPress)
	{
		emit touchScreen();

		//qDebug() << "touchScreen()";
	}
	//mouse button release means setfocus is useless
	if (event->type() == QEvent::MouseButtonRelease)
	{
		//qDebug() << obj << event->type();

		if (obj->objectName() == QString("qt_spinbox_lineedit"))
		{
			QDateEdit* _date = qobject_cast<QDateEdit*>(obj->parent());
			if (_date == ui->dateEdit)return true;
			if (_date->isEnabled())
			{
				this->ShowPanel();
				return true;
			}
		}
		if (obj->objectName() == QString("qt_scrollarea_viewport"))
		{
			QTextEdit* _text = qobject_cast<QTextEdit*>(obj->parent());
			if (!_text) return false;
			if (_text == ui->textEdit) return true; //
			if (_text->isEnabled())
			{
				this->ShowPanel();
				return true;
			}
		}

		if (obj->inherits("QLineEdit")) {
			QLineEdit* tbx = qobject_cast<QLineEdit*>(obj);
			//you have to tell whether it is coming from qdateedit!!!
			if (ui->lineEdit != tbx && tbx->isEnabled())
			{
				//qDebug() << "ShowPanel";
				//qDebug() << "focusWidget" << qApp->focusWidget();
				//qDebug() << "lineEdit" << ui->lineEdit;
				//qDebug() << "obj" << obj;
				this->ShowPanel();
				return true;
			}
		}





	}
	return QObject::eventFilter(obj, event);
}

void InputObject::ShowPanel() {
	if (!qApp->focusWidget()) return;
	if (qApp->focusWidget()->inherits("QLineEdit"))
	{
		curLine = qobject_cast<QLineEdit*>(qApp->focusWidget());
		ui->dateEdit->setVisible(false);
		ui->lineEdit->setVisible(true);
		ui->textEdit->setVisible(false);
	}
	if (qApp->focusWidget()->inherits("QDateEdit"))
	{
		curDateEdit = qobject_cast<QDateEdit*>(qApp->focusWidget());
		ui->dateEdit->setVisible(true);
		ui->lineEdit->setVisible(false);
		ui->textEdit->setVisible(false);
	}

	if (qApp->focusWidget()->inherits("QTextEdit"))
	{
		curText = qobject_cast<QTextEdit*>(qApp->focusWidget());
		ui->dateEdit->setVisible(false);
		ui->lineEdit->setVisible(false);
		ui->textEdit->setVisible(true);
	}

	this->showFullScreen();
	this->setVisible(true);

	this->activateWindow(); //it is quite important!!!
	while (QApplication::activeWindow() != this)
	{
		qApp->processEvents();
	}

	if (curLine) {
		ui->lineEdit->setText(curLine->text());
		ui->lineEdit->setEchoMode(curLine->echoMode());
		ui->lineEdit->setInputMethodHints(Qt::InputMethodHint::ImhNoAutoUppercase);
		//ui->lineEdit->setFocus(Qt::MouseFocusReason);
		ui->lineEdit->setFocus();

		//while (QGuiApplication::focusObject() != ui->lineEdit)
		//{
		//	qApp->processEvents();
		//}

		QEvent event(QEvent::RequestSoftwareInputPanel);
		QApplication::sendEvent(ui->lineEdit, &event);

	}
	if (curDateEdit)
	{

		ui->dateEdit->setDateTime(curDateEdit->dateTime());
		ui->dateEdit->setFocus();
		while (QGuiApplication::focusObject() != ui->dateEdit)
		{
			qApp->processEvents();
		}
		qDebug() << QGuiApplication::focusObject();
		QEvent event(QEvent::RequestSoftwareInputPanel);
		QApplication::sendEvent(ui->dateEdit, &event);
	}

	if (curText)
	{
		ui->textEdit->setPlainText(curText->toPlainText());
		ui->textEdit->setFocus();
		while (QGuiApplication::focusObject() != ui->textEdit)
		{
			qApp->processEvents();
		}
		//qDebug() << QGuiApplication::focusObject();
		QEvent event(QEvent::RequestSoftwareInputPanel);
		QApplication::sendEvent(ui->textEdit, &event);
	}
}


void InputObject::HidePanel()
{
	if (curLine) {
		curLine->setText(ui->lineEdit->text());
		ui->lineEdit->clearFocus();
		ui->lineEdit->clear();
		//this->setVisible(false);
		curLine = nullptr;
	}
	else if (curDateEdit)
	{
		//curDateEdit->setDateTime(QDateTime::currentDateTime());
		curDateEdit->setDateTime(ui->dateEdit->dateTime());
		ui->dateEdit->clearFocus();
		ui->dateEdit->clear();
		//this->setVisible(false);
		curDateEdit = nullptr;
	}
	else if (curText)
	{
		curText->setText(ui->textEdit->toPlainText());
		ui->textEdit->clearFocus();
		ui->textEdit->clear();
		//this->setVisible(false);
		curText = nullptr;
	}
	else
	{
		return;
	}
}
