#include "MainWindow.h"
#include <qfiledialog.h>
#include <qsettings.h>
#include <qdatastream.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qmessagebox.h>
#include <QTextStream>

#include "MessageException.h"
#include "TableHelper.h"
#include "DebugUtil.h"
#include "WzExcel.h"

MainWindow::MainWindow(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);

	QSettings setting("config.ini", QSettings::IniFormat);
	ui.modelPath->setText(setting.value("modelPath").toString());
	ui.servicePath->setText(setting.value("servicePath").toString());
	ui.widgetPath->setText(setting.value("widgetPath").toString());
}

void MainWindow::onModelPath()
{
	QString path = QFileDialog::getExistingDirectory(this);
	ui.modelPath->setText(path);
	QSettings setting("config.ini", QSettings::IniFormat);
	setting.setValue("modelPath", path);
}

void MainWindow::onServicePath()
{
	QString path = QFileDialog::getExistingDirectory(this);
	ui.servicePath->setText(path);
	QSettings setting("config.ini", QSettings::IniFormat);
	setting.setValue("servicePath", path);
}

void MainWindow::onWidgetPath()
{
	QString path = QFileDialog::getExistingDirectory(this);
	ui.widgetPath->setText(path);
	QSettings setting("config.ini", QSettings::IniFormat);
	setting.setValue("widgetPath", path);
}

void MainWindow::onGenerate()
{
	QString entityName = ui.entityName->text();
    if(entityName.isEmpty()){
        ui.entityName->setFocus();
        return;
    }
	QString modelName = ui.modelName->text();
	QString serviceName = ui.serviceName->text();
	QString widgetName = ui.widgetName->text();
	if (modelName.isEmpty())
		 modelName = entityName + "Model";
	if (serviceName.isEmpty())
		 serviceName = entityName + "Service";
	if (widgetName.isEmpty())
		widgetName = entityName + "Widget";

	createModel(modelName, entityName);
	createService(serviceName, entityName);
	if (ui.MultipleSelect->isChecked())
		createMultipleWidget(widgetName, modelName, serviceName, entityName,ui.name->text().trimmed());
	else
		createWidget(widgetName, modelName, serviceName, entityName, ui.name->text().trimmed());
    QMessageBox::information(this, "", "success");
}

void MainWindow::onTableFilePath()
{
    QString filename = QFileDialog::getExistingDirectory(this);
    ui.tableFilePath->setText(filename);
    QSettings setting("config.ini", QSettings::IniFormat);
    setting.setValue("TableFilePath",filename);
    setting.sync();
}

void MainWindow::onExcelPath()
{
    QString path = QFileDialog::getOpenFileName(this);
    ui.excelPath->setText(path);
}

void MainWindow::onGenerateTable()
{
    WzExcel excelHelper(ui.excelPath->text().trimmed());
    try
    {
        excelHelper.open();
    }
    catch (MessageException &e)
    {
        excelHelper.close();
        QMessageBox::information(this,"提示",e.getMessage());
        return;
    }  
    QString tablename = excelHelper.getValue(0,1).toString().toUpper();
    Debug(tablename);
    int rows = excelHelper.getRows();

    QList<QPair<QString,QString>> fieldAndTypes;
    QList<QPair<QString,QString>> fieldAndComments;
    for (int row = 2 ; row < rows ; row++)
    {
        QString copmment = excelHelper.getValue(row,0).toString();
        QString name = excelHelper.getValue(row,1).toString().toUpper();
        QString type = excelHelper.getValue(row,2).toString().toUpper();
        fieldAndTypes << qMakePair(name,type);
        fieldAndComments << qMakePair(name,copmment);
    }
    excelHelper.close();
    QStringList contents;
    contents << TableHelper::createTable(tablename,fieldAndTypes)
             << TableHelper::createPrimaryAlter(tablename)
             << TableHelper::createComment(tablename,fieldAndComments)
             << TableHelper::createSeq(tablename)
             << TableHelper::createTrigger(tablename);
    write(ui.tableFilePath->text().trimmed() + "/" + tablename + ".txt",contents.join("\n"));
    ui.textEdit->setText(contents.join("\n\n"));

    QString description = TableHelper::createDescription(ui.entityname->text().trimmed(), fieldAndComments);
    write(ui.tableFilePath->text().trimmed() + "/" + tablename + "_QxOrm.txt",description);
    ui.description->setText(description);
}

QString MainWindow::readAll(const QString &filename)
{
	QFile file(filename);
	if (!file.open(QIODevice::ReadOnly))
		return "";
	QTextStream in(&file);
    in.setCodec("UTF-8");
	QString txt = in.readAll();
	file.close();
	return txt;
}

void MainWindow::write(const QString &filename, const QString &content)
{
	QFile file(filename);
	if (!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
		return ;
	QTextStream out(&file);
    out.setCodec("UTF-8");
	out << content;
	file.close();
}

void MainWindow::createModel(const QString &modelName, const QString &entityName)
{
	QString modelH = readAll("template/model.h.template");
	modelH = modelH.arg(modelName).arg(entityName);

	QString modelCpp = readAll("template/model.cpp.template");
	modelCpp = modelCpp.arg(modelName).arg(entityName);

	QString path = ui.modelPath->text().trimmed();
	QString modelHFilename = QString("%1/%2.h").arg(path).arg(modelName);
	write(modelHFilename, modelH);

	QString modelCppFilename = QString("%1/%2.cpp").arg(path).arg(modelName);
	write(modelCppFilename, modelCpp);
}

void MainWindow::createService(const QString &serviceName, const QString &entityName)
{
	QString serviceH = readAll("template/service.h.template");
	serviceH = serviceH.arg(serviceName);

	QString serviceCpp = readAll("template/service.cpp.template");
	serviceCpp = serviceCpp.arg(serviceName).arg(entityName);

	QString path = ui.servicePath->text().trimmed();
	QString modelHFilename = QString("%1/%2.h").arg(path).arg(serviceName);
	write(modelHFilename, serviceH);

	QString modelCppFilename = QString("%1/%2.cpp").arg(path).arg(serviceName);
	write(modelCppFilename, serviceCpp);
}

void MainWindow::createWidget(const QString &widgetName, const QString &modelName, const QString &serviceName, const QString &entityName, const QString &name)
{
	QString widgetH = readAll("template/widget.h.template");
	widgetH = widgetH.arg(widgetName).arg(modelName).arg(serviceName);

	QString widgetCpp = readAll("template/widget.cpp.template");
	widgetCpp = widgetCpp.arg(widgetName).arg(entityName).arg(modelName).arg(serviceName).arg(name);

	QString widgetUi = readAll("template/widget.ui.template");
	widgetUi = widgetUi.arg(widgetName);

	QString path = ui.widgetPath->text().trimmed();
	QString widgetHFilename = QString("%1/%2.h").arg(path).arg(widgetName);
	write(widgetHFilename, widgetH);

	QString widgetCppFilename = QString("%1/%2.cpp").arg(path).arg(widgetName);
	write(widgetCppFilename, widgetCpp);

	QString widgetUiFilename = QString("%1/%2.ui").arg(path).arg(widgetName);
	write(widgetUiFilename, widgetUi);
}

void MainWindow::createMultipleWidget(const QString &widgetName, const QString &modelName, const QString &serviceName, const QString &entityName,const QString &name)
{
	QString widgetH = readAll("template/MultipleWidget.h.template");
	widgetH = widgetH.arg(widgetName).arg(modelName).arg(serviceName).arg(entityName);

	QString widgetCpp = readAll("template/MultipleWidget.cpp.template");
	widgetCpp = widgetCpp.arg(widgetName).arg(entityName).arg(modelName).arg(serviceName).arg(name);

	QString widgetUi = readAll("template/MultipleWidget.ui.template");
	widgetUi = widgetUi.arg(widgetName);

	QString path = ui.widgetPath->text().trimmed();
	QString widgetHFilename = QString("%1/%2.h").arg(path).arg(widgetName);
	write(widgetHFilename, widgetH);

	QString widgetCppFilename = QString("%1/%2.cpp").arg(path).arg(widgetName);
	write(widgetCppFilename, widgetCpp);

	QString widgetUiFilename = QString("%1/%2.ui").arg(path).arg(widgetName);
	write(widgetUiFilename, widgetUi);
}

