#include "gui/dialog/settings.h"
#include "core/session.h"
#include "gui/widgets/sidebar.h"
#include "gui/widgets/text.h"
#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
#include <QStackedWidget>
#include <QVBoxLayout>
#include <QWidget>
#ifdef WIN32
#	include "sys/win32/dark_theme.h"
#endif

SettingsPage::SettingsPage(QWidget* parent, QString title) : QWidget(parent) {
	setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
	page_title = new QLabel(title, this);
	page_title->setWordWrap(false);
	page_title->setFrameShape(QFrame::Panel);
	page_title->setFrameShadow(QFrame::Sunken);

	QFont font(page_title->font());
	font.setPixelSize(12);
	font.setWeight(QFont::Bold);
	page_title->setFont(font);

	{
		QPalette pal = page_title->palette();
		pal.setColor(QPalette::Window, QColor(0xAB, 0xAB, 0xAB));
		pal.setColor(QPalette::WindowText, Qt::white);
		page_title->setPalette(pal);
	}

	page_title->setAutoFillBackground(true);

	page_title->setFixedHeight(23);
	page_title->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
	page_title->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);

	tab_widget = new QTabWidget(this);
	tab_widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);

	QVBoxLayout* layout = new QVBoxLayout(this);
	layout->setContentsMargins(0, 0, 0, 0);
	layout->addWidget(page_title);
	layout->addWidget(tab_widget);
}

void SettingsPage::SetTitle(QString title) {
	page_title->setText(title);
}

void SettingsPage::AddTab(QWidget* tab, QString title) {
	tab_widget->addTab(tab, title);
}

void SettingsPage::SaveInfo() {
	// no-op... child classes will implement this
}

void SettingsDialog::OnOK() {
	for (int i = 0; i < stacked->count(); i++) {
		reinterpret_cast<SettingsPage*>(stacked->widget(i))->SaveInfo();
	}
	QDialog::accept();
}

void SettingsDialog::showEvent(QShowEvent* event) {
	QDialog::showEvent(event);
#ifdef WIN32
	win32::SetTitleBarsToBlack(session.config.theme.IsInDarkTheme());
#endif
}

SettingsDialog::SettingsDialog(QWidget* parent) : QDialog(parent) {
	setFixedSize(755, 566);
	setWindowTitle(tr("Settings"));
	setWindowFlags(Qt::Dialog | Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);

	QVBoxLayout* full_layout = new QVBoxLayout(this);

	{
		QWidget* widget = new QWidget(this);
		widget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
		QHBoxLayout* layout = new QHBoxLayout(widget);

		{
			sidebar = new SideBar(widget);

			sidebar->setCurrentItem(sidebar->AddItem(tr("Services"), SideBar::CreateIcon(":/icons/24x24/globe.png")));
			sidebar->AddItem(tr("Library"), SideBar::CreateIcon(":/icons/24x24/inbox-film.png"));
			sidebar->AddItem(tr("Application"), SideBar::CreateIcon(":/icons/24x24/application-sidebar-list.png"));
			sidebar->AddItem(tr("Recognition"), SideBar::CreateIcon(":/icons/24x24/question.png"));
			// sidebar->AddItem(tr("Sharing"), SideBar::CreateIcon(":/icons/24x24/megaphone.png"));
			sidebar->AddItem(tr("Torrents"), SideBar::CreateIcon(":/icons/24x24/feed.png"));
			// sidebar->AddItem(tr("Advanced"), SideBar::CreateIcon(":/icons/24x24/gear.png"));

			sidebar->setIconSize(QSize(24, 24));
			sidebar->setFrameShape(QFrame::Box);

			QPalette pal(sidebar->palette());
			sidebar->SetBackgroundColor(pal.color(QPalette::Base));

			sidebar->setFixedWidth(158);
			sidebar->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
			layout->addWidget(sidebar);
		}

		{
			stacked = new QStackedWidget(widget);
			stacked->addWidget(new SettingsPageServices(stacked));
			stacked->addWidget(new SettingsPageLibrary(stacked));
			stacked->addWidget(new SettingsPageApplication(stacked));
			stacked->addWidget(new SettingsPageRecognition(stacked));
			stacked->addWidget(new SettingsPageTorrents(stacked));
			stacked->setCurrentIndex(0);

			connect(sidebar, &QListWidget::currentRowChanged, stacked, &QStackedWidget::setCurrentIndex);

			layout->addWidget(stacked);
		}

		layout->setContentsMargins(0, 0, 0, 0);
		full_layout->addWidget(widget);
	}

	{
		QDialogButtonBox* button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
		connect(button_box, &QDialogButtonBox::accepted, this, &SettingsDialog::OnOK);
		connect(button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
		full_layout->addWidget(button_box);
	}
}
