#include "pageappearance.h"
#include "../../components/moderncombobox.h"
#include "../../components/modernmessagebox.h"
#include "../../components/moderntoast.h"
#include "../../components/modernswitch.h"
#include "../../components/settingscard.h"
#include "../../components/settingssubpanel.h"
#include "../../managers/searchhistorymanager.h"
#include "config/config_keys.h"
#include "config/configstore.h"
#include <QLabel>
#include <QPushButton>

PageAppearance::PageAppearance(QEmbyCore *core, QWidget *parent) : SettingsPageBase(core, tr("Appearance"), parent)
{
    
    auto *themeCombo = new ModernComboBox(this);
    themeCombo->addItem(tr("System Default"), "system");
    themeCombo->addItem(tr("Light Mode"), "light");
    themeCombo->addItem(tr("Dark Mode"), "dark");
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-theme.svg", tr("Theme Mode"),
                                             tr("Switch application theme (takes effect immediately)"), themeCombo,
                                             ConfigKeys::ThemeMode, this));

    
    auto *fontSizeCombo = new ModernComboBox(this);
    fontSizeCombo->addItem(tr("Small"), "small");
    fontSizeCombo->addItem(tr("Medium"), "medium");
    fontSizeCombo->addItem(tr("Large"), "large");
    fontSizeCombo->setCurrentIndex(1); 
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-font-size.svg", tr("Font Size"),
                                             tr("Adjust the overall font size of the interface"), fontSizeCombo,
                                             ConfigKeys::FontSize, this));

    
    auto *sidebarCombo = new ModernComboBox(this);
    sidebarCombo->addItem(tr("Left"), "left");
    sidebarCombo->addItem(tr("Right"), "right");
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-sidebar.svg", tr("Sidebar Position"),
                                             tr("Place the navigation sidebar on the left or right side"), sidebarCombo,
                                             ConfigKeys::SidebarPosition, this));

    
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-sidebar-pin.svg", tr("Pin Sidebar"),
                                             tr("Keep the sidebar always visible and integrated into the main layout"),
                                             new ModernSwitch(this), ConfigKeys::SidebarPinned, this));

    
    auto *customSidebarSwitch = new ModernSwitch(this);
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-sidebar-custom.svg", tr("Custom Sidebar"),
                                             tr("Customize which items appear in the navigation sidebar"),
                                             customSidebarSwitch, ConfigKeys::SidebarCustomEnabled, this,
                                             QVariant(false)));

    auto *hideSearchPanel = new SettingsSubPanel(":/svg/light/search.svg", this);
    auto *hideSearchLabel = new QLabel(tr("Hide Search Bar"), this);
    hideSearchLabel->setObjectName("SettingsCardDesc");
    auto *hideSearchSwitch = new ModernSwitch(this);
    hideSearchSwitch->setChecked(
        ConfigStore::instance()->get<bool>(ConfigKeys::SidebarHideSearch, false));
    hideSearchPanel->contentLayout()->addWidget(hideSearchLabel, 1);
    hideSearchPanel->contentLayout()->addWidget(hideSearchSwitch, 0, Qt::AlignVCenter);
    m_mainLayout->addWidget(hideSearchPanel);

    auto *hideHomePanel = new SettingsSubPanel(":/svg/light/home.svg", this);
    auto *hideHomeLabel = new QLabel(tr("Hide Home Item"), this);
    hideHomeLabel->setObjectName("SettingsCardDesc");
    auto *hideHomeSwitch = new ModernSwitch(this);
    hideHomeSwitch->setChecked(
        ConfigStore::instance()->get<bool>(ConfigKeys::SidebarHideHome, false));
    hideHomePanel->contentLayout()->addWidget(hideHomeLabel, 1);
    hideHomePanel->contentLayout()->addWidget(hideHomeSwitch, 0, Qt::AlignVCenter);
    m_mainLayout->addWidget(hideHomePanel);

    auto *hideFavPanel = new SettingsSubPanel(":/svg/light/heart.svg", this);
    auto *hideFavLabel = new QLabel(tr("Hide Favorites Item"), this);
    hideFavLabel->setObjectName("SettingsCardDesc");
    auto *hideFavSwitch = new ModernSwitch(this);
    hideFavSwitch->setChecked(
        ConfigStore::instance()->get<bool>(ConfigKeys::SidebarHideFavorites, false));
    hideFavPanel->contentLayout()->addWidget(hideFavLabel, 1);
    hideFavPanel->contentLayout()->addWidget(hideFavSwitch, 0, Qt::AlignVCenter);
    m_mainLayout->addWidget(hideFavPanel);

    if (ConfigStore::instance()->get<bool>(ConfigKeys::SidebarCustomEnabled, false))
    {
        hideSearchPanel->initExpanded();
        hideHomePanel->initExpanded();
        hideFavPanel->initExpanded();
    }

    connect(customSidebarSwitch, &ModernSwitch::toggled, hideSearchPanel, &SettingsSubPanel::setExpanded);
    connect(customSidebarSwitch, &ModernSwitch::toggled, hideHomePanel, &SettingsSubPanel::setExpanded);
    connect(customSidebarSwitch, &ModernSwitch::toggled, hideFavPanel, &SettingsSubPanel::setExpanded);

    connect(hideSearchSwitch, &ModernSwitch::toggled, this,
            [](bool checked) { ConfigStore::instance()->set(ConfigKeys::SidebarHideSearch, checked); });
    connect(hideHomeSwitch, &ModernSwitch::toggled, this,
            [](bool checked) { ConfigStore::instance()->set(ConfigKeys::SidebarHideHome, checked); });
    connect(hideFavSwitch, &ModernSwitch::toggled, this,
            [](bool checked) { ConfigStore::instance()->set(ConfigKeys::SidebarHideFavorites, checked); });

    
    auto *windowStateCombo = new ModernComboBox(this);
    windowStateCombo->addItem(tr("Normal Window"), "normal");
    windowStateCombo->addItem(tr("Maximized"), "maximized");
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-window-state.svg", tr("Startup Window State"),
                                             tr("Choose window state when the application launches"), windowStateCombo,
                                             ConfigKeys::StartupWindowState, this));

    
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-animations.svg", tr("Reduce Animations"),
                                             tr("Reduce page transitions and hover effects for better performance"),
                                             new ModernSwitch(this), ConfigKeys::UiAnimations, this));

    
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/appearance-animations.svg", tr("Snapshot Navigation"),
                                             tr("Use static screenshots during page transitions for smoother animation on complex views"),
                                             new ModernSwitch(this), ConfigKeys::SnapshotNavigation, this));

    
    auto *searchHistorySwitch = new ModernSwitch(this);
    m_mainLayout->addWidget(new SettingsCard(":/svg/dark/search.svg", tr("Enable Search History"),
                                             tr("Save recent searches locally for quick reuse"), searchHistorySwitch,
                                             ConfigKeys::SearchHistoryEnabled, this, QVariant(false)));

    auto *searchAutocompletePanel = new SettingsSubPanel(":/svg/dark/search-autocomplete.svg", this);
    auto *searchAutocompleteLabel = new QLabel(tr("Enable Search Autocomplete"), this);
    searchAutocompleteLabel->setObjectName("SettingsCardDesc");
    auto *searchAutocompleteSwitch = new ModernSwitch(this);
    searchAutocompleteSwitch->setChecked(
        ConfigStore::instance()->get<bool>(ConfigKeys::SearchAutocompleteEnabled, false));
    searchAutocompletePanel->contentLayout()->addWidget(searchAutocompleteLabel, 1);
    searchAutocompletePanel->contentLayout()->addWidget(searchAutocompleteSwitch, 0, Qt::AlignVCenter);
    m_mainLayout->addWidget(searchAutocompletePanel);

    auto *clearHistoryPanel = new SettingsSubPanel(":/svg/dark/delete.svg", this);
    auto *clearHistoryLabel = new QLabel(tr("Clear Search History"), this);
    clearHistoryLabel->setObjectName("SettingsCardDesc");
    auto *clearHistoryBtn = new QPushButton(tr("Clear All"), this);
    clearHistoryBtn->setObjectName("SettingsCardButton");
    clearHistoryBtn->setCursor(Qt::PointingHandCursor);
    clearHistoryBtn->setFixedHeight(30);
    clearHistoryPanel->contentLayout()->addWidget(clearHistoryLabel, 1);
    clearHistoryPanel->contentLayout()->addWidget(clearHistoryBtn, 0, Qt::AlignVCenter);
    m_mainLayout->addWidget(clearHistoryPanel);

    if (ConfigStore::instance()->get<bool>(ConfigKeys::SearchHistoryEnabled, false))
    {
        searchAutocompletePanel->initExpanded();
        clearHistoryPanel->initExpanded();
    }

    connect(searchHistorySwitch, &ModernSwitch::toggled, searchAutocompletePanel, &SettingsSubPanel::setExpanded);
    connect(searchHistorySwitch, &ModernSwitch::toggled, clearHistoryPanel, &SettingsSubPanel::setExpanded);

    connect(searchAutocompleteSwitch, &ModernSwitch::toggled, this,
            [](bool checked) { ConfigStore::instance()->set(ConfigKeys::SearchAutocompleteEnabled, checked); });

    connect(
        clearHistoryBtn, &QPushButton::clicked, this,
        [this]()
        {
            const bool confirmed = ModernMessageBox::question(
                this, tr("Clear Search History"),
                tr("Are you sure you want to clear search history for all servers?\n\nThis action cannot be undone."),
                tr("Clear"), tr("Cancel"), ModernMessageBox::Danger, ModernMessageBox::Warning);
            if (!confirmed)
            {
                return;
            }

            SearchHistoryManager::instance()->clearAllHistory();
            ModernToast::showMessage(tr("Search history cleared"), 1500);
        });

    
    m_mainLayout->addStretch();
}
