#include "MainWindow.h"

#include <DateTime/DatePicker.h>
#include <QFramelessWindow.h>
#include <TitleBar.h>

#include <Navigation/NavigationInterface.h>
#include <Widgets/StackedWidget.h>

#include <QDebug>

StackedWidget::StackedWidget(QWidget *parent) : QFrame(parent) {
  m_hBoxLayout = new QHBoxLayout(this);
  m_view = new PopUpAniStackedWidget(this);

  m_hBoxLayout->setContentsMargins(0, 0, 0, 0);
  m_hBoxLayout->addWidget(m_view);

  connect(
      m_view, &PopUpAniStackedWidget::currentChanged, this,
      [this](int index) { emit currentWidgetChanged(m_view->widget(index)); });
}

void StackedWidget::addWidget(QWidget *widget) { m_view->addWidget(widget); }

///
/// @todo widget.verticalScrollBar().setValue(0)
///
void StackedWidget::setCurrentWidget(QWidget *widget, bool popOut) {
  if (popOut) {
    m_view->setCurrentWidget(widget, true, false, 200, QEasingCurve::InQuad);
  } else {
    m_view->setCurrentWidget(widget, false, true, 300);
  }
}

void StackedWidget::setCurrentIndex(int index, bool popOut) {
  setCurrentWidget(m_view->widget(index), popOut);
}

PopUpAniStackedWidget *StackedWidget::view() const { return m_view; }

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
  initWindow();
  initLayout();
}

MainWindow::~MainWindow() {}

void MainWindow::initWindow() {

  QWidget *w = new QWidget();
  m_hBoxLayout = new QHBoxLayout(w);
  setCentralWidget(w);

  m_widgetLayout = new QHBoxLayout();
  m_stackWidget = new StackedWidget(this);
  m_navigationInterface = new NavigationInterface(true, true, this);

  QFramelessHelper *helper = new QFramelessHelper(this);
  m_titleBar = new StandardTitleBar(this);
  m_titleBar->setGeometry(QRect(46, 0, width() - 46, m_titleBar->height()));
  helper->setTitleBar(m_titleBar);

  QIcon icon = QIcon(":/resource/logo.png");
  setWindowIcon(icon);
  m_titleBar->setIcon(icon);

  setWindowTitle("专家评定数据分析软件");
  setMinimumSize(QSize(800, 600));
}

void MainWindow::initLayout() {
  m_hBoxLayout->setSpacing(0);
  m_hBoxLayout->setContentsMargins(0, 0, 0, 0);
  m_hBoxLayout->addWidget(m_navigationInterface);
  m_hBoxLayout->addLayout(m_widgetLayout);
  m_hBoxLayout->setStretchFactor(m_widgetLayout, 1);

  m_widgetLayout->addWidget(m_stackWidget);
  m_widgetLayout->setContentsMargins(0, 32, 0, 0);
}

void MainWindow::resizeEvent(QResizeEvent *event) {
  m_navigationInterface->resize(m_navigationInterface->width(), height());
  m_titleBar->resize(width() - 46, m_titleBar->height());
}