# -*- coding: utf-8 -*-
"""
Dialog styling for the plugin.

The colour set is chosen per theme (dark / light) instead of being derived from
the QGIS palette, because the raw palette produces a flat, washed out dialog.
Dark mode is still detected from the palette so the plugin follows whichever
QGIS theme the user picked.
"""

import os

from qgis.PyQt.QtGui import QPalette
from qgis.PyQt.QtWidgets import QApplication

# Qt6 requires scoped enum access; the scoped form also works on Qt5.
_ROLE = QPalette.ColorRole

_UI_DIR = os.path.dirname(__file__)

DARK_COLORS = {
    'BG': '#1f2125',
    'SURFACE': '#282b31',
    'SURFACE_ALT': '#2f333a',
    'BORDER': '#3c414a',
    'BORDER_STRONG': '#4d5460',
    'TEXT': '#e7eaee',
    'MUTED': '#9aa2ac',
    'ACCENT': '#3ddc97',
    'ACCENT_HOVER': '#57e8a9',
    'ACCENT_TEXT': '#0f2419',
    'ACCENT_SOFT': '#22493a',
    'CHECK_ICON': 'check_ink.png',
    'BLUE': '#4aa3ff',
    'AMBER': '#f0b429',
    'VIOLET': '#a78bfa',
    'C_RGB': '#4aa3ff',
    'C_NDVI': '#3ddc97',
    'C_NBR': '#f0883e',
}

LIGHT_COLORS = {
    'BG': '#f4f6f9',
    'SURFACE': '#ffffff',
    'SURFACE_ALT': '#eef2f6',
    'BORDER': '#d6dce4',
    'BORDER_STRONG': '#b7c0cb',
    'TEXT': '#1f2328',
    'MUTED': '#5d6874',
    'ACCENT': '#0f8a5f',
    'ACCENT_HOVER': '#0b7350',
    'ACCENT_TEXT': '#ffffff',
    'ACCENT_SOFT': '#dcf3e8',
    'CHECK_ICON': 'check_white.png',
    'BLUE': '#1a6fd4',
    'AMBER': '#b7791f',
    'VIOLET': '#6d4bd6',
    'C_RGB': '#1a6fd4',
    'C_NDVI': '#0f8a5f',
    'C_NBR': '#c2600f',
}

_STYLESHEET = """
QDialog {
    background: @BG@;
}
QDialog, QWidget {
    color: @TEXT@;
    font-size: 12px;
}

/* ---------- header ---------- */
QFrame#accentBar {
    background: @ACCENT@;
    border: none;
    border-radius: 2px;
}
QLabel#headerTitle {
    font-size: 16px;
    font-weight: 700;
    color: @TEXT@;
}
QLabel#subtitleLabel {
    color: @MUTED@;
}
QLabel#footerLabel {
    color: @MUTED@;
    font-size: 11px;
}
QLabel#loadStatusLabel {
    color: @ACCENT@;
    font-size: 11px;
}
QLabel#validationLabel {
    color: @AMBER@;
    font-size: 11px;
    font-weight: 600;
}
QProgressDialog {
    background: @BG@;
}
QProgressDialog QLabel {
    color: @TEXT@;
    padding: 10px 6px 6px 6px;
}
QLabel#hintLabel, QLabel#label_cloud_hint, QLabel#label_export_hint,
QLabel#label_search_hint, QLabel#mutedLabel, QLabel#helpBody {
    color: @MUTED@;
    font-size: 11px;
}
QLabel#helpBody {
    font-size: 12px;
}

/* ---------- tabs ---------- */
QTabWidget::pane {
    border: 1px solid @BORDER@;
    border-radius: 10px;
    background: @SURFACE@;
    top: -1px;
}
QTabBar {
    qproperty-drawBase: 0;
}
QTabBar::tab {
    background: transparent;
    color: @MUTED@;
    border: 1px solid transparent;
    border-bottom: 2px solid transparent;
    padding: 8px 18px;
    margin-right: 4px;
    font-weight: 600;
    outline: 0;
}
QTabBar::tab:focus {
    outline: 0;
}
QTabBar::tab:hover {
    color: @TEXT@;
}
QTabBar::tab:selected {
    color: @ACCENT@;
    border-bottom: 2px solid @ACCENT@;
}

/* ---------- group boxes ---------- */
QGroupBox {
    border: 1px solid @BORDER@;
    border-radius: 10px;
    background: @SURFACE_ALT@;
    margin-top: 14px;
    padding: 16px 14px 12px 14px;
    font-weight: 600;
}
QGroupBox::title {
    subcontrol-origin: margin;
    subcontrol-position: top left;
    left: 14px;
    padding: 2px 8px;
    color: @ACCENT@;
    background: @BG@;
    border: 1px solid @BORDER@;
    border-radius: 6px;
    font-size: 11px;
}
/* Each section gets its own hue so the dialog reads at a glance. */
QGroupBox#groupBoundary::title, QGroupBox#groupExtent::title {
    color: @ACCENT@;
    border-color: @ACCENT@;
}
QGroupBox#groupDate::title {
    color: @BLUE@;
    border-color: @BLUE@;
}
QGroupBox#groupCloud::title {
    color: @AMBER@;
    border-color: @AMBER@;
}
QGroupBox#groupExport::title {
    color: @VIOLET@;
    border-color: @VIOLET@;
}

/* ---------- inputs ---------- */
QComboBox, QDateEdit, QSpinBox, QLineEdit {
    background: @SURFACE@;
    color: @TEXT@;
    border: 1px solid @BORDER_STRONG@;
    border-radius: 7px;
    min-height: 26px;
    padding: 2px 10px;
    selection-background-color: @ACCENT@;
    selection-color: @ACCENT_TEXT@;
}
QComboBox:hover, QDateEdit:hover, QSpinBox:hover, QLineEdit:hover {
    border-color: @ACCENT@;
}
QComboBox:focus, QDateEdit:focus, QSpinBox:focus, QLineEdit:focus {
    border-color: @ACCENT@;
}
QComboBox:disabled, QDateEdit:disabled, QSpinBox:disabled, QLineEdit:disabled {
    background: @SURFACE_ALT@;
    color: @MUTED@;
    border-color: @BORDER@;
}
/* Drop-down and spin buttons are intentionally left unstyled: overriding those
   sub-controls makes Qt drop the native arrows, leaving them invisible. */

/* ---------- calendar popup ---------- */
QCalendarWidget QWidget {
    alternate-background-color: @SURFACE_ALT@;
}
QCalendarWidget QAbstractItemView:enabled {
    background: @SURFACE@;
    color: @TEXT@;
    selection-background-color: @ACCENT@;
    selection-color: @ACCENT_TEXT@;
    outline: 0;
}
QCalendarWidget QAbstractItemView:disabled {
    color: @MUTED@;
}
QCalendarWidget QWidget#qt_calendar_navigationbar {
    background: @SURFACE_ALT@;
    border-bottom: 1px solid @BORDER@;
}
QCalendarWidget QToolButton {
    background: transparent;
    color: @TEXT@;
    padding: 4px 10px;
}
QCalendarWidget QToolButton:hover {
    background: @ACCENT_SOFT@;
    border-radius: 5px;
}
QCalendarWidget QMenu {
    background: @SURFACE@;
    color: @TEXT@;
    border: 1px solid @BORDER_STRONG@;
}
QComboBox QAbstractItemView {
    background: @SURFACE@;
    color: @TEXT@;
    border: 1px solid @BORDER_STRONG@;
    border-radius: 6px;
    padding: 4px;
    outline: 0;
    selection-background-color: @ACCENT@;
    selection-color: @ACCENT_TEXT@;
}

/* ---------- searchable combo ---------- */
/* The editable combo draws its own line edit, which must not look like a
   second input nested inside the first one. */
QComboBox QLineEdit {
    background: transparent;
    border: none;
    border-radius: 0px;
    min-height: 0px;
    padding: 0px;
    color: @TEXT@;
    selection-background-color: @ACCENT@;
    selection-color: @ACCENT_TEXT@;
}
QComboBox QLineEdit:disabled {
    background: transparent;
    color: @MUTED@;
}
/* The completer popup is a top level widget, so it is styled directly in
   make_searchable_combo with this same sheet. */
QListView#completerPopup {
    background: @SURFACE@;
    color: @TEXT@;
    border: 1px solid @BORDER_STRONG@;
    border-radius: 6px;
    padding: 4px;
    outline: 0;
}
QListView#completerPopup::item {
    padding: 5px 8px;
    border-radius: 4px;
}
QListView#completerPopup::item:selected,
QListView#completerPopup::item:hover {
    background: @ACCENT@;
    color: @ACCENT_TEXT@;
}

/* ---------- calendar popup ---------- */
QCalendarWidget QWidget {
    background: @SURFACE@;
    color: @TEXT@;
}
QCalendarWidget QAbstractItemView {
    background: @SURFACE@;
    color: @TEXT@;
    selection-background-color: @ACCENT@;
    selection-color: @ACCENT_TEXT@;
    outline: 0;
}
QCalendarWidget QToolButton {
    background: transparent;
    color: @TEXT@;
    border-radius: 5px;
    padding: 3px 8px;
}
QCalendarWidget QToolButton:hover {
    background: @ACCENT_SOFT@;
}

/* ---------- checkboxes ---------- */
QCheckBox {
    color: @TEXT@;
    spacing: 9px;
    padding: 3px 0;
}
QCheckBox::indicator {
    width: 17px;
    height: 17px;
    border: 1px solid @BORDER_STRONG@;
    border-radius: 5px;
    background: @SURFACE@;
}
QCheckBox::indicator:hover {
    border-color: @ACCENT@;
}
QCheckBox::indicator:checked {
    background: @ACCENT@;
    border-color: @ACCENT@;
    image: url("@CHECK_ICON@");
}
/* Output colours match how each product is rendered on the map. */
QCheckBox#check::indicator:hover {
    border-color: @C_RGB@;
}
QCheckBox#check::indicator:checked {
    background: @C_RGB@;
    border-color: @C_RGB@;
}
QCheckBox#check_ndvi::indicator:hover {
    border-color: @C_NDVI@;
}
QCheckBox#check_ndvi::indicator:checked {
    background: @C_NDVI@;
    border-color: @C_NDVI@;
}
QCheckBox#check_nbr::indicator:hover {
    border-color: @C_NBR@;
}
QCheckBox#check_nbr::indicator:checked {
    background: @C_NBR@;
    border-color: @C_NBR@;
}

/* ---------- progress ---------- */
QProgressBar {
    border: none;
    border-radius: 5px;
    background: @SURFACE@;
    min-height: 8px;
    max-height: 8px;
    text-align: center;
    color: transparent;
}
QProgressBar::chunk {
    background: @ACCENT@;
    border-radius: 5px;
}

/* ---------- buttons ---------- */
QPushButton {
    background: @SURFACE@;
    color: @TEXT@;
    border: 1px solid @BORDER_STRONG@;
    border-radius: 7px;
    min-width: 94px;
    min-height: 30px;
    padding: 4px 16px;
    font-weight: 600;
}
QPushButton:hover {
    border-color: @ACCENT@;
    color: @ACCENT@;
}
QPushButton:default {
    background: @ACCENT@;
    color: @ACCENT_TEXT@;
    border-color: @ACCENT@;
}
QPushButton:default:hover {
    background: @ACCENT_HOVER@;
    border-color: @ACCENT_HOVER@;
    color: @ACCENT_TEXT@;
}
QPushButton:disabled {
    background: @SURFACE_ALT@;
    color: @MUTED@;
    border-color: @BORDER@;
}

/* ---------- daily preview gallery ---------- */
QScrollArea#previewScroll {
    background: transparent;
    border: 1px solid @BORDER@;
    border-radius: 10px;
}
QWidget#previewCanvas {
    background: transparent;
}
QFrame#previewCard {
    background: @SURFACE@;
    border: 1px solid @BORDER@;
    border-radius: 10px;
}
QLabel#previewThumb {
    background: @SURFACE_ALT@;
    border: 1px solid @BORDER@;
    border-radius: 6px;
    color: @MUTED@;
    font-size: 11px;
}
QLabel#previewDay {
    font-size: 13px;
    font-weight: 700;
    color: @TEXT@;
}
QLabel#previewCloudLow {
    color: @C_NDVI@;
    font-size: 11px;
    font-weight: 600;
}
QLabel#previewCloudMid {
    color: @AMBER@;
    font-size: 11px;
    font-weight: 600;
}
QLabel#previewCloudHigh {
    color: @C_NBR@;
    font-size: 11px;
    font-weight: 600;
}
QPushButton#useDayButton {
    min-width: 0px;
    min-height: 24px;
    padding: 2px 10px;
    font-size: 11px;
    font-weight: 600;
    color: @ACCENT@;
    background: transparent;
    border: 1px solid @ACCENT@;
    border-radius: 6px;
}
QPushButton#useDayButton:hover {
    background: @ACCENT@;
    color: @ACCENT_TEXT@;
}

/* ---------- misc ---------- */
QFrame#divider {
    background: @BORDER@;
    max-height: 1px;
    border: none;
}
QSizeGrip {
    background: transparent;
    width: 14px;
    height: 14px;
}
QToolTip {
    background: @SURFACE@;
    color: @TEXT@;
    border: 1px solid @ACCENT@;
    padding: 4px 6px;
}
"""


def palette_color(role):
    return QApplication.palette().color(role)


def is_dark_mode():
    return palette_color(_ROLE.Window).lightness() < 128


def theme_colors():
    return DARK_COLORS if is_dark_mode() else LIGHT_COLORS


def build_stylesheet(colors=None):
    colors = dict(colors or theme_colors())
    # QSS url() needs forward slashes, even on Windows.
    icon_path = os.path.join(_UI_DIR, colors['CHECK_ICON']).replace('\\', '/')
    colors['CHECK_ICON'] = icon_path

    stylesheet = _STYLESHEET
    for token, value in colors.items():
        stylesheet = stylesheet.replace('@{}@'.format(token), value)
    return stylesheet


def apply_dialog_theme(widget):
    """Style a dialog so it looks consistent in both QGIS light and dark themes."""
    widget.setStyleSheet(build_stylesheet())
