#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "handle/seriport/serialbasehandle.h"

#include "widget/shortcutconfig.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow), config(nullptr)
{
    ui->setupUi(this);
    connect(ui->widgetLogin,&LoginForm::loginFinish,this,&MainWindow::backMenu);

    connect(ui->pageSelectValue,&SelectValueForm::selectProject,this,&MainWindow::FixedAndUncertain);
    connect(ui->pageSelectValue,&SelectValueForm::back,this,&MainWindow::backMenu);
    connect(ui->pageSelectValue,&SelectValueForm::backFandU,this,&MainWindow::backFAndU);

    connect(ui->pageSettings,&SettingsForm::back,this,&MainWindow::backMenu);
    connect(ui->pagePackDetail,&PackDetailForm::back,this,&MainWindow::backMenu);
    connect(ui->pageAddNew,&AddNewSpecsForm::back,this,&MainWindow::backMenu);

    //定重页面
    connect(ui->dingzhong,&FixedWeightPackForm::backup,this,&MainWindow::backSelect);
    connect(ui->dingzhong,&FixedWeightPackForm::NoSyncList,this,&MainWindow::changeToPackDetail);
    connect(ui->dingzhong,&FixedWeightPackForm::buding,this,&MainWindow::on_buding);
    connect(ui->dingzhong,&FixedWeightPackForm::back,this,&MainWindow::backMenu);

    //不定页面
    connect(ui->buding,&UncertainWeightPackForm::back,this,&MainWindow::backMenu);
    connect(ui->buding,&UncertainWeightPackForm::dingzhong,this,&MainWindow::on_dingzhong);
    connect(ui->buding,&UncertainWeightPackForm::backup,this,&MainWindow::backSelect);
    connect(ui->buding,&UncertainWeightPackForm::NoSyncList,this,&MainWindow::changeToPackDetail);

    //模板页面
    connect(ui->pageSelectModel,&ModelSelectForm::back,this,&MainWindow::backMenu);
    connect(ui->pageSelectModel,&ModelSelectForm::addBoxModel,this,&MainWindow::changeToShowXiangDan);
    connect(ui->pageSelectModel,&ModelSelectForm::showBaseBoxModel,this,&MainWindow::changeToShowXiangDan);
    connect(ui->pageSelectModel,&ModelSelectForm::editBoxModeled,this,&MainWindow::changeToShowXiangDan);

    connect(ui->pageSelectModel,&ModelSelectForm::addPackModel,this,&MainWindow::changeToShowMaDan);
    connect(ui->pageSelectModel,&ModelSelectForm::showBasePackModel,this,&MainWindow::changeToShowMaDan);
    connect(ui->pageSelectModel,&ModelSelectForm::editPackModeled,this,&MainWindow::changeToShowMaDan);

    connect(ui->pageShowXiangDan,&ShowXiangDanLabelForm::back,this,&MainWindow::changeToShowBoxModel);

    connect(ui->pageShowMaDan,&ShowMaDanLabelForm::back,this,&MainWindow::changeToShowPackModel);


    connect(ui->actionSetting,&QAction::triggered,this,&MainWindow::changeToSettings);
    connect(ui->actionAddNew,&QAction::triggered,this,&MainWindow::changeToAddNew);
    connect(ui->actionPack,&QAction::triggered,this,&MainWindow::changeToPackDetail);
    connect(ui->actionSelectvalue,&QAction::triggered,this,&MainWindow::backSelect);
    connect(ui->actionLogout,&QAction::triggered,this,&MainWindow::logout);
    connect(ui->actionXiangDan,&QAction::triggered,this,&MainWindow::changeToShowBoxModel);
    connect(ui->actionMaDan,&QAction::triggered,this,&MainWindow::changeToShowPackModel);

    ui->stackedWidget->setCurrentWidget(ui->pageLogin);
    ui->menu_5->setVisible(false);
    ui->actionXiangDan->setVisible(true);
    ui->actionMaDan->setVisible(true);
    ui->actionXiangDan->setData(1);


//    QDesktopWidget* desktopWidget =QApplication::desktop();
//     QRect deskRect =desktopWidget->availableGeometry();   //获取可用桌面大小
//     QRect screenRect =desktopWidget->screenGeometry();  //获取设备屏幕大小


//    ui->bg_widget->setAutoFillBackground(true);
//    QImage image;
//    image.load(":/images/backImage.jpg");
//    QPalette palette;
//    palette.setBrush(QPalette::Background, QBrush(image.scaled(
//        QSize(deskRect.width(),deskRect.height()),
//        Qt::IgnoreAspectRatio,
//        Qt::SmoothTransformation)));
//    ui->bg_widget->setPalette(palette);
}

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

void MainWindow::on_pushAuto_clicked()
{
    backSelect();
}

void MainWindow::on_pushList_clicked()
{
    changeToPackDetail();
}

void MainWindow::on_pushConfig_clicked()
{
    changeToSettings();
}

void MainWindow::changeToSettings()
{
    ui->stackedWidget->setCurrentWidget(ui->pageSettings);
}

void MainWindow::backMenu()
{
    if(GlobalInfo::this_()->user().userName == ""){
        QMessageBox::information(this,tr("注意"),tr("请登录"));
        ui->stackedWidget->setCurrentWidget(ui->pageLogin);
        return;
    }
    ui->stackedWidget->setCurrentWidget(ui->pageMain);
}

void MainWindow::backSelect(int i)
{
    if(GlobalInfo::this_()->user().userName == ""){
        QMessageBox::information(this,tr("注意"),tr("请登录"));
        return;
    }

    ui->pageSelectValue->setStatus(i);
    ui->pageSelectValue->Start();
    ui->stackedWidget->setCurrentWidget(ui->pageSelectValue);
}

void MainWindow::FixedAndUncertain(QString batchno)
{
    ui->stackedWidget->setCurrentWidget(ui->pageAutoPack);
//    ui->dingzhongButton->setChecked(true);
    ui->dingzhong->setProInfo(GlobalInfo::this_()->project().value(batchno));
    ui->dingzhong->changeSpecs();
    ui->buding->setProInfo(GlobalInfo::this_()->project().value(batchno));
    ui->buding->changeSpecs();

}

void MainWindow::backFAndU()
{
    ui->stackedWidget->setCurrentWidget(ui->pageAutoPack);
}

void MainWindow::changeToAddNew()
{
    if(GlobalInfo::this_()->user().userName == ""){
        QMessageBox::information(this,tr("注意"),tr("请登录"));
        return;
    }
//    if(GlobalInfo::this_()->isNoLine() == false){
//        QMessageBox::information(this,tr("注意"),tr("在线不能添加规格"));
//        return;
//    }
//    if(GlobalInfo::this_()->isNoLine() == true){
//        QMessageBox::information(this,tr("注意"),tr("离线不能添加规格"));
//        return;
//    }
    ui->stackedWidget->setCurrentWidget(ui->pageAddNew);
}

void MainWindow::changeToPackDetail()
{
    if(GlobalInfo::this_()->user().userName == ""){
        QMessageBox::information(this,tr("注意"),tr("请登录"));
        return;
    }
    QDate tm = QDate::currentDate();
    QList<PackSaveInfo> saveinfo = GlobalInfo::this_()->packInfoManger()->getPackInfoSync(tm);
    QList<PackSaveInfo> save;
    for(int i = saveinfo.size()-1;i>=0;i--){
        save.append(saveinfo.at(i));
    }
    ui->pagePackDetail->setSaveInfo(save);
    ui->pagePackDetail->days = 0;
    ui->stackedWidget->setCurrentWidget(ui->pagePackDetail);
    ui->pagePackDetail->changeTreeWidget(0,5);
}

void MainWindow::changeToShowMaDan(QString baseModelName,int status ,QString modelName)
{
    ui->pageShowMaDan->init(baseModelName,status,modelName);
    ui->stackedWidget->setCurrentWidget(ui->pageShowMaDan);
}

void MainWindow::changeToShowBoxModel()
{
    if(GlobalInfo::this_()->user().userName == ""){
        QMessageBox::information(this,tr("注意"),tr("请登录"));
        return;
    }
    ui->pageSelectModel->changeStackedWidget(1);
    ui->stackedWidget->setCurrentWidget(ui->pageSelectModel);
}

void MainWindow::changeToShowPackModel()
{
    if(GlobalInfo::this_()->user().userName == ""){
        QMessageBox::information(this,tr("注意"),tr("请登录"));
        return;
    }
    ui->pageSelectModel->changeStackedWidget(2);
    ui->stackedWidget->setCurrentWidget(ui->pageSelectModel);
}

void MainWindow::changeToShowXiangDan(QString baseModelName,int status,QString modelName)
{
    ui->pageShowXiangDan->init(baseModelName,status,modelName);
    ui->stackedWidget->setCurrentWidget(ui->pageShowXiangDan);
}

void MainWindow::on_dingzhong()
{
    ui->stackedWidget_2->setCurrentWidget(ui->dingzhong);
//    ui->dingzhong->init();
}

void MainWindow::on_buding()
{
    ui->stackedWidget_2->setCurrentWidget(ui->buding);
//    ui->buding->init();
}


void MainWindow::closeEvent(QCloseEvent *event)
{
    ui->dingzhong->save();
    ui->buding->save();
    QMainWindow::closeEvent(event);
}

void MainWindow::logout()
{
    ui->dingzhong->save();
    ui->buding->save();
    GlobalInfo::this_()->logOuted();
    UserInfo user;
    GlobalInfo::this_()->start(user,true);
    GlobalInfo::this_()->setIslogin(false);
    ui->stackedWidget->setCurrentWidget(ui->pageLogin);
}


void MainWindow::on_backButton_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pageMain);
}

void MainWindow::on_action_2_triggered()
{
    if(!config) {
        config = new ShortcutConfig;
        connect(config, &ShortcutConfig::setShortcutConfig,ui->dingzhong,&FixedWeightPackForm::setShortcutConfig);
        connect(config, &ShortcutConfig::setShortcutConfig,ui->buding, &UncertainWeightPackForm::setShortcutConfig);
    }
    config->setWindowModality(Qt::ApplicationModal);
    config->show();
}
