#include "mainwindow.h"
#include "cal.h"
#include "ui_mainwindow.h"
#include <QClipboard>
#include <QDebug>
#include <QMessageBox>
#include <QScrollBar>
static QString displayText;
static QString calcLog;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
  ui->setupUi(this);
  result = 0.0;
  displayText += '\n';
  isFinished = false;
  connectSlots();
}

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

//错误处理
void MainWindow::abortOperation() {
  result = 0.0;
  QMessageBox::warning(this, "运算错误", "算式不合法");
}

//数字被点击
void MainWindow::normalClicked() {
  QPushButton *digitBtn = static_cast<QPushButton *>(sender());
  if (isFinished) {
    displayText += '\n';
    isFinished = 0;
  }
  displayText += digitBtn->text();
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());

  return;
}

void MainWindow::on_clearBtn_clicked() {
  //将当前显示的数归零
  if (displayText[displayText.length() - 1] == '\n')
    return;
  displayText.chop(1);
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());
}

void MainWindow::on_equalBtn_clicked() {
  if (isFinished)
    return;
  try {
    QString s = inToPost(ui->lineEdit->toPlainText().right(
        ui->lineEdit->toPlainText().size() -
        (ui->lineEdit->toPlainText().lastIndexOf("\n") + 1)));
    std::cout << s.toStdString() << std::endl;
    result = compute(s);

  } catch (...) {
    abortOperation();
  }
  calcLog += ui->lineEdit->toPlainText().right(
                 ui->lineEdit->toPlainText().size() -
                 (ui->lineEdit->toPlainText().lastIndexOf("\n") + 1)) +
             '=' + QString::number(result) + '\n';
  displayText += '=' + QString::number(result);
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());

  isFinished = true;
}

//槽链接
void MainWindow::connectSlots() {
  QPushButton *digitBtns[21] = {
      ui->digitBtn0, ui->digitBtn1,   ui->digitBtn2, ui->digitBtn3,
      ui->digitBtn4, ui->digitBtn5,   ui->digitBtn6, ui->digitBtn7,
      ui->digitBtn8, ui->digitBtn9,   ui->addBtn,    ui->subtractionBtn,
      ui->mulBtn,    ui->divisionBtn, ui->squareBtn, ui->pointBtn,
      ui->LBtn,      ui->RBtn,        ui->cBtn,      ui->sBtn,
      ui->sciBtn};
  for (auto btn : digitBtns)
    connect(btn, &QPushButton::clicked, this, &MainWindow::normalClicked);
}

void MainWindow::on_actionPaste_triggered() {
  QClipboard *board = QApplication::clipboard();
  QString text = board->text();
  ui->lineEdit->setPlainText(text);
}

void MainWindow::on_actionClearHistroy_triggered() {
  displayText.clear();
  displayText += '\n';
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());
}

void MainWindow::on_actionShow_History_H_triggered() {
  ui->lineEdit->setPlainText(calcLog);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());
}

void MainWindow::on_clearLine_clicked() {
  isFinished = false;
  displayText.chop(displayText.size() - displayText.lastIndexOf('\n') - 1);
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());

  result = 0.0;
}

void MainWindow::on_ansBtn_clicked() {
  if (isFinished) {
    isFinished = false;
    displayText += '\n';
  }
  displayText += QString::number(result);
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
  switch (event->key()) {
  case Qt::Key_0:
    emit ui->digitBtn0->clicked();
    break;
  case Qt::Key_1:
    emit ui->digitBtn1->clicked();
    break;
  case Qt::Key_2:
    emit ui->digitBtn2->clicked();
    break;
  case Qt::Key_3:
    emit ui->digitBtn3->clicked();
    break;
  case Qt::Key_4:
    emit ui->digitBtn4->clicked();
    break;
  case Qt::Key_5:
    emit ui->digitBtn5->clicked();
    break;
  case Qt::Key_6:
    emit ui->digitBtn6->clicked();
    break;
  case Qt::Key_7:
    emit ui->digitBtn7->clicked();
    break;
  case Qt::Key_8:
    emit ui->digitBtn8->clicked();
    break;
  case Qt::Key_9:
    emit ui->digitBtn9->clicked();
    break;
  case Qt::Key_Plus:
    emit ui->addBtn->clicked();
    break;
  case Qt::Key_Minus:
    emit ui->subtractionBtn->clicked();
    break;
  case Qt::Key_Asterisk:
    emit ui->mulBtn->clicked();
    break;
  case Qt::Key_Slash:
    emit ui->divisionBtn->clicked();
    break;
  case Qt::Key_Period:
    emit ui->pointBtn->clicked();
    break;
  case Qt::Key_AsciiCircum:
    emit ui->squareBtn->clicked();
    break;
  case Qt::Key_ParenLeft:
    emit ui->LBtn->clicked();
    break;
  case Qt::Key_ParenRight:
    emit ui->RBtn->clicked();
    break;
  case Qt::Key_A:
    emit ui->ansBtn->clicked();
    break;
  case Qt::Key_S:
    emit ui->sBtn->clicked();
    break;
  case Qt::Key_C:
    emit ui->cBtn->clicked();
    break;
  case Qt::Key_Equal:
  case Qt::Key_Enter:
    emit ui->equalBtn->clicked();
    break;
  case Qt::Key_Backspace:
    emit ui->clearBtn->clicked();
    break;
  case Qt::Key_Delete:
  case Qt::Key_Escape:
    emit ui->clearLine->clicked();
    break;

  default:
    break;
  }
}

void MainWindow::on_actionc_triggered() {
  if (isFinished) {
    isFinished = false;
    displayText += '\n';
  }
  displayText += "299792458";
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());
}

void MainWindow::on_actionpi_triggered() {
  if (isFinished) {
    isFinished = false;
    displayText += '\n';
  }
  displayText += "3.1415926535";
  ui->lineEdit->setPlainText(displayText);
  ui->lineEdit->verticalScrollBar()->setValue(
      ui->lineEdit->verticalScrollBar()->maximum());
}
