//
// Created by HW on 2023/07/27.
//

// You may need to build the project (run Qt uic code generator) to get "ui_MainScreen.h" resolved

#include "mainscreen.h"
#include "ui_MainScreen.h"
#include "netio.h"
#include <QPixmap>
#include <exception>
#include <QResizeEvent>
#include "globalvariables.h"
#include "mythread.h"
#include "config.h"


MainScreen::MainScreen(QWidget *parent) :
        QWidget(parent), ui(new Ui::MainScreen) {
    ui->setupUi(this);
	setContentsMargins(0, 0, 0, 0);
	widget = new QWidget(this);
	layout_left = new QVBoxLayout(widget);
	button = new QPushButton(widget);
	layout_left->addStretch(4);
	layout_left->addWidget(button,1);
	layout_left->setAlignment(Qt::AlignCenter);
	button->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
	button->setMinimumSize(100,50);
	button->setMaximumSize(200,100);
	widget->setLayout(layout_left);
	widget->setHidden(true);
	button->setStyleSheet("border-style:none;padding:10px;border-radius:5px;background-color:#09bb07;color:white;font-family:SimHei");
	//Plus Math icon by Icons8
	QString dir = QApplication::applicationDirPath();
	button->setText(QString::fromLocal8Bit("打开微信"));
	//button->setStyleSheet("text-color:white");
	layout = new QHBoxLayout;
	miniblink = new QMiniBlink(this);
	layout->addWidget(widget, 1);
	layout->addWidget(miniblink, 4);
	this->setLayout(layout);
	layout->setMargin(0);
	//miniblink->setAttribute(Qt::WA_DeleteOnClose);
	miniblink->show();
	//miniblink->setSizePolicy(sizePolicy);
	//thread = new MyThread(this,&miniblink);
	//thread->start();
	//miniblink->moveToThread(thread);
	connect(this, &MainScreen::ondestroy, miniblink, &QMiniBlink::ondestroy);
	connect(button, &QPushButton::pressed, this, &MainScreen::startWeChat);
	connect(this, &MainScreen::started, miniblink, &QMiniBlink::init);
	//connect(miniblink, SIGNAL(finish()), miniblink, SLOT(deleteLater()));
	//connect(miniblink, SIGNAL(destroyed()), thread, SLOT(quit()));
	//connect(thread, &QThread::finished, thread, &QThread::deleteLater);
	while(miniblink==nullptr){}
	connect(this, &MainScreen::changeUrl, miniblink, &QMiniBlink::switchUrl);
	emit started();
}

MainScreen::~MainScreen() {
	layout_left->removeWidget(button);
	widget->setLayout(nullptr);
	delete layout_left;
	delete button;
	emit ondestroy();
	layout->removeWidget(miniblink);
	layout->removeWidget(widget);
	delete miniblink;
	delete widget;
	delete layout;
	delete ui;
}
void MainScreen::firstUrl(QString url)
{
	QString url_full = BASE_URL + url + url_param;
	emit changeUrl(url_full);
}
void MainScreen::clickButton1(QString text, QString url) {
	QString url_full = BASE_URL + url + url_param;
	if (text == QString::fromLocal8Bit("微信多开")) {
		widget->setHidden(false);
		miniblink->setHidden(false);
		emit changeUrl(url_full);
	}
	else {
		widget->setHidden(true);
		miniblink->setHidden(false);
		emit changeUrl(url_full);
	}
}
void MainScreen::startWeChat() {
	QString path = QApplication::applicationDirPath();
	QString exe = path + "/app/wxdk.exe";
	path.replace("/", "\\");
	ShellExecute(GetDesktopWindow(), L"open", exe.toStdWString().c_str(), L"79c86fb12b36dfa33d1a537c9af100b4c7928a9c", path.toStdWString().c_str(), SW_SHOW);
}
//void MainScreen::resizeEvent(QResizeEvent *event) {
//	QWidget::resizeEvent(event);
//	//broswer->resize(event->size().width() / 5 * 4, event->size().height());
//	miniblink->resize(event->size().width() / 5 * 4, event->size().height());
//	miniblink->update();
//}
