#include <qmessagebox.h>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "global.h"
#include "serialport.h"
#include "dataparser.h"
#include "filemanager.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
      ui(new Ui::MainWindow),
      _centralWidget(new CentralWidget(this)),
      _leftWidget(new LeftWidget(this)),
      _expertmentSettingForm(new ExperimentSettingForm(this)),
      _realTimeDataForm(new RealTimeDataForm(this)),
      _rightWidget(new QDockWidget(this))
    ,_analysisSettingWidget(new AnalysisSettingForm(this))
    ,_contextMenu(new QMenu(this))
{
    ui->setupUi(this);
    this->setToolTip(".....");

    setCentralWidget(_centralWidget);
    addDockWidget(Qt::LeftDockWidgetArea, _leftWidget);
    addDockWidget(Qt::RightDockWidgetArea, _rightWidget);

    _rightWidget->setWidget(_analysisSettingWidget);
    _rightWidget->hide();
    //
    ui->statusbar->showMessage("showMessage show temp message!");
    // permenent show
    QLabel *permenentLabel = new QLabel(this);
    permenentLabel->setText("Permanent show!");
    ui->statusbar->addPermanentWidget(permenentLabel);
    //
    connections();
    //
    _expertmentSettingForm->setWindowFlags(_expertmentSettingForm->windowFlags()| Qt::Dialog);
    _realTimeDataForm->setWindowFlags(_realTimeDataForm->windowFlags()| Qt::Dialog);

    //    _realTimeDataForm->show();
    setActionEnable(true);

}

MainWindow::~MainWindow()
{
    // 删除非QObject派生类的对象
    // 假设 Ui::MainWindow 不是 QObject 派生类
    delete ui;

    FileManager::close();
}

void MainWindow::slotContextMenuShow(const QPoint point)
{
    _contextMenu->exec(point);
}

void MainWindow::connections()
{
    // ui
    connect(_expertmentSettingForm, &ExperimentSettingForm::sigDeliverData,
            SerialPort::instance(), &SerialPort::slotDeliverData);
#if 1
    // dynamic data
    connect(SerialPort::instance(), &SerialPort::sigSendCommonData,
            _centralWidget, &CentralWidget::slotRecvCommonData);
    connect(SerialPort::instance(), &SerialPort::sigSendCommonData,
            _realTimeDataForm, &RealTimeDataForm::slotRevCommonData);

    connect(SerialPort::instance(), &SerialPort::sigSendCommonDataToRealDataForm,
            _realTimeDataForm, &RealTimeDataForm::slotRevCommonData);

    connect(SerialPort::instance(), &SerialPort::sigSendPhaseInfo,
            _expertmentSettingForm, &ExperimentSettingForm::slotRecvPhaseInfo);

#endif

    // mode
    connect(Global::instance(), &Global::sigModeModify,
            _centralWidget, &CentralWidget::slotModeModify);
    //analysis
    connect(_leftWidget,&LeftWidget::sigSendAnalysisFileName,
            _centralWidget,&CentralWidget::slotRecvAnalysisFileName);
    connect(_centralWidget,&CentralWidget::sigSendLineXCoord,
            _analysisSettingWidget,&AnalysisSettingForm::slotRecvLineXCoord);

    connect(_analysisSettingWidget,&AnalysisSettingForm::sigApply,
            _centralWidget,&CentralWidget::slotAnalysisSettingApply);
    connect(_analysisSettingWidget,&AnalysisSettingForm::sigConfirm,
            _centralWidget,&CentralWidget::slotAnalysisSettingConfirm);
    connect(_analysisSettingWidget,&AnalysisSettingForm::sigUndo,
            _centralWidget,&CentralWidget::slotAnalysisSettingUndo);
    connect(_analysisSettingWidget,&AnalysisSettingForm::sigCancel,
            _centralWidget,&CentralWidget::slotAnalysisSettingCancel);

    connect(_centralWidget,&CentralWidget::sigRightDockWidgetHide,
            [&](){ _rightWidget->hide(); });
}

void MainWindow::setActionEnable(const bool flag)
{
    if (flag)
    {
        ui->actionNew->setEnabled(true);
        ui->actionStart->setEnabled(true);
        ui->actionStop->setEnabled(true);
        ui->actionRealTimeWidget->setEnabled(true);
    }
    else
    {
        ui->actionNew->setEnabled(false);
        ui->actionStart->setEnabled(false);
        ui->actionStop->setEnabled(false);
        ui->actionRealTimeWidget->setEnabled(false);
    }
}

void MainWindow::on_actionStop_triggered()
{
    QByteArray ba = DataParser::setDeviceStartStop(DeviceStartMode::Stop);
    SerialPort::instance()->slotSendData(ba);

    FileManager::close();

    Global::instance()->setMode(Global::Mode::Analysis);
}

void MainWindow::on_actionNew_triggered()
{
    _expertmentSettingForm->show();
}

void MainWindow::on_actionStart_triggered()
{
    QByteArray ba = DataParser::setDeviceStartStop(DeviceStartMode::Start);

    QString hexData = ba.toHex(' '); // ' ' 作为分隔符，可选参数
    qDebug() << "on_actionStart_triggered info (hex):" << hexData;

    SerialPort::instance()->slotSendData(ba);
    //
    FileManager::createExperimentFile();

    Global::instance()->setMode(Global::Mode::ExperimentStart);
}

void MainWindow::on_actionReadOnly_triggered()
{
    Global::instance()->setMode(Global::Mode::ExperimentStart);
    SerialPort::instance()->openSp();
}

void MainWindow::on_actionRealTimeWidget_triggered()
{
    _realTimeDataForm->show();
}

void MainWindow::on_actionConnectToDev_triggered()
{
    if (SerialPort::instance()->openSp())
    {
        setActionEnable(true);
        Global::instance()->setMode(Global::Mode::ConnectedToDev);

        QByteArray ba = DataParser::inquirePhaseInfo();
        SerialPort::instance()->sendData(ba);
    }
    else
    {
        QMessageBox::warning(this, "warnning", "Serial Port open failed.");
    }
}

void MainWindow::on_actionStartPoint_triggered()
{
    _rightWidget->show();
    _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::StartPoint);
}

void MainWindow::on_actionStopPoint_triggered()
{
    _rightWidget->show();
    _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::StopPoint);
}

void MainWindow::on_actionNumericalLabel_triggered()
{
    _rightWidget->show();
    _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::NumericalLabel);
}

void MainWindow::on_actionPeakSynthesisAnalysis_triggered()
{
    //峰综合分析
    _rightWidget->show();
    _centralWidget->setAnalysisMode(CentralWidget::AnalysisMode::PeakSynthesisAnalysis);
}

void MainWindow::on_actionClearAllData_triggered()
{
    _centralWidget->clearAllData();
}
