#include "mainwindow.h"

#include "ui_mainwindow.h"

#include "../productswidget/productswidget.h"

#include <QMainWindow>

#include <QToolBar>

#include <QLineEdit>

#include <QDebug>

void MainWindow::createToolBar() {
  auto toolBar = new QToolBar;
  toolBar->setObjectName("m_toolBar");

  toolBar->setIconSize(QSize(32, 32));
  toolBar->setMovable(true);
  toolBar->setFloatable(false);
  toolBar->setToolButtonStyle(Qt::ToolButtonStyle::ToolButtonTextUnderIcon);

  toolBar->addAction(m_ui->m_actionWorkplace);
  toolBar->addAction(m_ui->m_actionReport);
  toolBar->addAction(m_ui->m_actionProducts);
  toolBar->addAction(m_ui->m_actionPrices);
  toolBar->addAction(m_ui->m_actionSuppliers);
  addToolBar(Qt::ToolBarArea::LeftToolBarArea, toolBar);
}

void MainWindow::configureCentralWidget() {
  m_ui->m_innerWidget->addWidget(new QLabel("Workplace"));
  m_ui->m_innerWidget->addWidget(new QLabel("Reports"));
  m_ui->m_innerWidget->addWidget(new ProductsWidget);
  m_ui->m_innerWidget->addWidget(new QLabel("Prices"));
  m_ui->m_innerWidget->addWidget(new QLabel("Suppliers"));
}

void MainWindow::configureConnections() {
#define CONNECT_ACTION_SLOT(value)                                             \
  connect(m_ui->m_action##value, &QAction::triggered, this,                    \
          [this]() { setCentralWidget(CentralWidget::value); });

  CONNECT_ACTION_SLOT(Workplace);
  CONNECT_ACTION_SLOT(Report);
  CONNECT_ACTION_SLOT(Products);
  CONNECT_ACTION_SLOT(Prices);
  CONNECT_ACTION_SLOT(Suppliers);

#undef CONNECT_ACTION_SLOT
}

void MainWindow::configureText() {
  auto toolBarToggleViewAction =
      findChild<QToolBar *>("m_toolBar")->toggleViewAction();
  toolBarToggleViewAction->setText("Show tool bar");
}

void MainWindow::configureIcons() {
  QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
  QIcon::setThemeName("custom");

  using Path = const char *;
  constexpr Path windowIconFallback = ":/icons/custom/32x32/coffee-beans.png";
  constexpr Path workplaceIconFallback = ":/icons/custom/32x32/cash.png";
  constexpr Path suppliersFallback = ":/icons/custom/32x32/supplier.png";
  constexpr Path productsIconFallback =
      ":/icons/custom/32x32/grocery-shelf.png";
  constexpr Path pricesIconFallback = ":/icons/custom/32x32/price-tag.png";
  constexpr Path reportIconFallback = ":/icons/tango/32x32/text-x-generic.png";

  setWindowIcon(QIcon::fromTheme("coffee-beans", QIcon(windowIconFallback)));

  auto setIcon = [](QAction *action, const char *fromTheme,
                    const char *fallback) -> void {
    action->setIcon(QIcon::fromTheme(fromTheme, QIcon(fallback)));
  };

  setIcon(m_ui->m_actionWorkplace, "cash", workplaceIconFallback);
  setIcon(m_ui->m_actionSuppliers, "supplier", suppliersFallback);
  setIcon(m_ui->m_actionProducts, "grocery-shelf", productsIconFallback);
  setIcon(m_ui->m_actionPrices, "price-tag", pricesIconFallback);
  setIcon(m_ui->m_actionReport, "text-x-generic", reportIconFallback);
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), m_ui(new Ui::MainWindow) {
  m_ui->setupUi(this);
  createToolBar();
  configureCentralWidget();
  configureConnections();
  configureText();
  configureIcons();
  emit m_ui->m_actionWorkplace->triggered();
}

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

void MainWindow::setCentralWidget(MainWindow::CentralWidget index) {
  m_ui->m_innerWidget->setCurrentIndex(int(index));
  QString labelText = qobject_cast<QAction *>(sender())->iconText() + ":";
  m_ui->m_topLabel->setText(labelText);
}
