from __future__ import annotations

from pathlib import Path
import sys

from PySide6.QtCore import Qt
from PySide6.QtGui import QColor, QFont, QPalette, QPixmap
from PySide6.QtWidgets import QApplication, QFrame, QHBoxLayout, QLabel, QWidget


GOLD = "#e2b85d"
GOLD_BRIGHT = "#ffd979"
RED = "#781214"
RED_BRIGHT = "#b61c22"
DARK = "#090909"
PANEL = "#141211"
PANEL_2 = "#211b18"
TEXT = "#e9dfcf"
MUTED = "#b7a896"
GREEN = "#8cff5a"
WARNING = "#ff9d62"


def resource_dir() -> Path:
    return Path(getattr(sys, "_MEIPASS", Path(__file__).resolve().parent))


def asset_path(name: str) -> Path:
    return resource_dir() / "assets" / "awakening" / name


def url_path(name: str) -> str:
    return asset_path(name).as_posix()


def apply_theme(app: QApplication) -> None:
    palette = QPalette()
    palette.setColor(QPalette.Window, QColor("#080404"))
    palette.setColor(QPalette.WindowText, QColor(TEXT))
    palette.setColor(QPalette.Base, QColor("#0d0b0a"))
    palette.setColor(QPalette.AlternateBase, QColor("#1a1412"))
    palette.setColor(QPalette.Text, QColor(TEXT))
    palette.setColor(QPalette.Button, QColor("#45110f"))
    palette.setColor(QPalette.ButtonText, QColor(GOLD_BRIGHT))
    palette.setColor(QPalette.Highlight, QColor("#7d211b"))
    palette.setColor(QPalette.HighlightedText, QColor("#fff1c7"))
    palette.setColor(QPalette.ToolTipBase, QColor("#100c0b"))
    palette.setColor(QPalette.ToolTipText, QColor(TEXT))
    app.setPalette(palette)
    app.setFont(QFont("Segoe UI", 9))

    qss = f"""
    QWidget {{
        color: {TEXT};
        selection-background-color: #7d211b;
        selection-color: #fff0c8;
    }}
    QMainWindow, QWidget#centralRoot {{
        background-color: #080303;
    }}
    QFrame#topBar {{
        background: rgba(7, 5, 5, 235);
        border-bottom: 1px solid #71341f;
    }}
    QFrame#awakeningPanel, QGroupBox {{
        background-color: rgba(20, 17, 15, 242);
        border: 1px solid #753b28;
        border-radius: 4px;
    }}
    QGroupBox {{
        margin-top: 12px;
        padding-top: 12px;
        font-weight: 700;
        color: {GOLD_BRIGHT};
    }}
    QGroupBox::title {{
        subcontrol-origin: margin;
        left: 12px;
        padding: 0 6px;
        background: #15110f;
    }}
    QLabel#sectionTitle {{
        color: {GOLD_BRIGHT};
        font-family: Georgia;
        font-weight: 700;
        font-size: 13px;
        padding: 5px 10px;
        background: qlineargradient(x1:0,y1:0,x2:1,y2:0,stop:0 #26110d,stop:.5 #721912,stop:1 #26110d);
        border: 1px solid #9b542f;
        border-radius: 3px;
    }}
    QLabel#statusLabel {{ color: #d9c7a7; padding: 2px 8px; }}
    QLabel#itemTitle {{
        color: {GOLD_BRIGHT};
        font-family: Georgia;
        font-size: 14px;
        font-weight: 700;
    }}
    QLabel#legendaryTitle {{
        color: #ffd65c;
        font-family: Georgia;
        font-size: 14px;
        font-weight: 700;
        text-shadow: 1px 1px #000;
    }}
    QPushButton {{
        color: #ffe6a5;
        background: qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #81201b,stop:.48 #5a0e0d,stop:1 #2b0808);
        border: 1px solid #c77937;
        border-radius: 5px;
        padding: 6px 13px;
        font-weight: 700;
    }}
    QPushButton:hover {{
        background: qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #a72a21,stop:.48 #76120e,stop:1 #3d0908);
        border-color: #ffd476;
    }}
    QPushButton:pressed {{ background: #350707; padding-top: 7px; }}
    QPushButton:disabled {{ color: #74685c; background: #211b18; border-color: #4b4037; }}
    QPushButton#goldButton {{
        background: qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #9e6f1f,stop:.5 #63320b,stop:1 #2c1706);
        color: #fff0aa;
        border-color: #e1ae4f;
    }}
    QLineEdit, QComboBox, QSpinBox {{
        background: #0e0c0b;
        color: #f1e8d8;
        border: 1px solid #655244;
        border-radius: 3px;
        padding: 5px 7px;
        min-height: 20px;
    }}
    QComboBox {{ padding-right: 29px; }}
    QLineEdit:focus, QComboBox:focus, QSpinBox:focus {{ border-color: #c9803e; }}
    QComboBox::drop-down {{
        border: 0;
        border-left: 1px solid #4c382c;
        width: 25px;
    }}
    QComboBox::down-arrow {{
        image: url("{url_path('combo_down_arrow.png')}");
        width: 12px;
        height: 10px;
    }}
    QComboBox::down-arrow:on {{ top: 1px; }}
    QComboBox QAbstractItemView {{
        background: #15110f;
        color: #eadcc9;
        selection-background-color: #7a2119;
        border: 1px solid #8b4e32;
    }}
    QCheckBox {{ spacing: 7px; }}
    QTabWidget::pane {{
        border: 1px solid #6e3826;
        background: rgba(8, 7, 6, 220);
        top: -1px;
    }}
    QTabBar::tab {{
        color: #d6c7b0;
        background: #24201d;
        border: 1px solid #5d4a3c;
        border-bottom: none;
        min-width: 112px;
        padding: 9px 12px;
        margin-right: 2px;
        font-weight: 700;
    }}
    QTabBar::tab:selected {{
        color: #ffe28b;
        background: qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #8e2b19,stop:1 #3a0b09);
        border-color: #c2783c;
    }}
    QTabBar::tab:hover:!selected {{ background: #3b2921; color: #f1d598; }}
    QTableWidget, QTableView, QTreeWidget {{
        background: rgba(9, 8, 7, 238);
        alternate-background-color: rgba(29, 24, 21, 238);
        gridline-color: #48362c;
        border: 1px solid #63402e;
        color: #eadfce;
    }}
    QHeaderView::section {{
        color: #f0cf7b;
        background: qlineargradient(x1:0,y1:0,x2:0,y2:1,stop:0 #4a2420,stop:1 #211815);
        border: 0;
        border-right: 1px solid #684632;
        border-bottom: 1px solid #8c5532;
        padding: 6px;
        font-weight: 700;
    }}
    QTableWidget::item:selected, QTreeWidget::item:selected {{ background: #70211a; color: #fff2c7; }}
    QScrollBar:vertical {{ background: #100d0c; width: 14px; margin: 0; }}
    QScrollBar::handle:vertical {{ background: #67402f; min-height: 30px; border: 1px solid #a86a3d; border-radius: 4px; }}
    QScrollBar::handle:vertical:hover {{ background: #8b4c34; }}
    QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {{ height: 0; }}
    QScrollBar:horizontal {{ background: #100d0c; height: 14px; }}
    QScrollBar::handle:horizontal {{ background: #67402f; min-width: 30px; border: 1px solid #a86a3d; border-radius: 4px; }}
    QTextEdit, QPlainTextEdit, QTextBrowser {{
        background: #070706;
        color: #ddd1bf;
        border: 1px solid #62402d;
        selection-background-color: #7b2119;
    }}
    QProgressBar {{
        border: 1px solid #74513b;
        border-radius: 4px;
        text-align: center;
        color: #fff0c7;
        background: #0b0908;
        min-height: 17px;
    }}
    QProgressBar::chunk {{
        background: qlineargradient(x1:0,y1:0,x2:1,y2:0,stop:0 #5d080d,stop:.5 #d42b25,stop:1 #741016);
        border-radius: 3px;
    }}
    QSplitter::handle {{ background: #5e3022; }}
    QToolTip {{ background-color: #110d0b; color: #f0e1ca; border: 1px solid #b56d38; padding: 5px; }}
    """
    app.setStyleSheet(qss)


class SectionTitle(QWidget):
    def __init__(self, text: str, parent=None):
        super().__init__(parent)
        layout = QHBoxLayout(self)
        layout.setContentsMargins(0, 0, 0, 0)
        left = QLabel()
        right = QLabel()
        for label, name in ((left, "arrow_title_left.png"), (right, "arrow_title_right.png")):
            pix = QPixmap(str(asset_path(name)))
            if not pix.isNull():
                label.setPixmap(pix.scaledToHeight(16, Qt.SmoothTransformation))
        title = QLabel(text)
        title.setObjectName("sectionTitle")
        title.setAlignment(Qt.AlignCenter)
        layout.addWidget(left)
        layout.addWidget(title, 1)
        layout.addWidget(right)


class Panel(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setObjectName("awakeningPanel")
