from typing import List, Optional

from qgis.PyQt.QtWidgets import (
    QComboBox,
    QDialog,
    QDialogButtonBox,
    QLabel,
    QLineEdit,
    QMessageBox,
    QTextEdit,
    QVBoxLayout,
)

from .. import i18n
from ..kumoy.api.team import TeamDetail
from ..kumoy.constants import (
    MAX_CHARACTERS_PROJECT_NAME,
    MAX_CHARACTERS_PROJECT_DESCRIPTION,
)
from ..pyqt_version import QT_DIALOG_BUTTON_CANCEL, QT_DIALOG_BUTTON_OK


class ProjectEditDialog(QDialog):
    """Dialog for creating or editing a project with name and description"""

    def __init__(
        self,
        org_name: str,
        teams: List[TeamDetail],
        parent=None,
        initial_name: str = "",
        initial_description: str = "",
    ):
        super().__init__(parent)
        self.org_name = org_name
        self.teams = teams
        self.project_name = ""
        self.project_description = ""
        self.selected_team: Optional[TeamDetail] = None
        self.initial_name = initial_name
        self.initial_description = initial_description
        self.setup_ui()

    def setup_ui(self):
        self.setWindowTitle(i18n.tr("New Project"))

        layout = QVBoxLayout()

        # Team field (only shown when teams are provided)
        self.team_combo = QComboBox()
        if self.teams:
            team_label = QLabel(i18n.tr("Team") + ' <span style="color: red;">*</span>')
            layout.addWidget(team_label)
            for team in self.teams:
                self.team_combo.addItem(team.name, team)
            layout.addWidget(self.team_combo)

        # Name field
        name_label = QLabel(i18n.tr("Name") + ' <span style="color: red;">*</span>')
        layout.addWidget(name_label)

        self.name_input = QLineEdit()
        self.name_input.setPlaceholderText(i18n.tr("Enter project name"))
        self.name_input.setMaxLength(MAX_CHARACTERS_PROJECT_NAME)
        self.name_input.setText(self.initial_name)
        layout.addWidget(self.name_input)

        # Description field
        description_label = QLabel(i18n.tr("Description"))
        layout.addWidget(description_label)

        self.description_input = QTextEdit()
        self.description_input.setPlaceholderText(i18n.tr("Enter project description"))
        self.description_input.setMaximumHeight(100)
        self.description_input.textChanged.connect(self._limit_description)
        self.description_input.setPlainText(self.initial_description or "")
        layout.addWidget(self.description_input)

        # Buttons
        button_box = QDialogButtonBox(QT_DIALOG_BUTTON_OK | QT_DIALOG_BUTTON_CANCEL)
        button_box.accepted.connect(self.accept)
        button_box.rejected.connect(self.reject)
        layout.addWidget(button_box)

        self.setLayout(layout)

    def _limit_description(self):
        """Limit description to maximum characters"""
        if (
            len(self.description_input.toPlainText())
            > MAX_CHARACTERS_PROJECT_DESCRIPTION
        ):
            cursor = self.description_input.textCursor()
            self.description_input.setPlainText(
                self.description_input.toPlainText()[
                    :MAX_CHARACTERS_PROJECT_DESCRIPTION
                ]
            )
            cursor.setPosition(MAX_CHARACTERS_PROJECT_DESCRIPTION)
            self.description_input.setTextCursor(cursor)

    def accept(self):
        """Validate and accept the dialog"""
        self.project_name = self.name_input.text().strip()
        self.project_description = self.description_input.toPlainText().strip()
        self.selected_team = self.team_combo.currentData()

        if not self.project_name:
            QMessageBox.warning(
                self,
                i18n.tr("Invalid Input"),
                i18n.tr("Project name cannot be empty."),
            )
            return

        super().accept()
