import logging
from PySide6.QtCore import Property, QObject, Signal, Slot
from PySide6.QtGui import QGuiApplication

logger = logging.getLogger(__name__)


class Bridge(QObject):
    # Signals for UI communication
    translationReceived = Signal(dict)
    translationError = Signal(str)
    resetSignal = Signal(int, int)
    showWindowSignal = Signal(int, int)
    hideSignal = Signal()
    
    # Property change signals
    isPinnedChanged = Signal(bool)
    isDarkChanged = Signal(bool)
    defaultLangChanged = Signal(str)
    translationModeChanged = Signal(str)
    triggerKeyChanged = Signal(str)
    
    # Request for retranslation when settings change
    retranslateRequest = Signal(str)

    def __init__(self, config_manager):
        super().__init__()
        self.config = config_manager
        self._last_text = ""
        self._request_id = 0
        self._is_showing_from_tray = False

    @Property(bool, notify=isPinnedChanged)
    def isPinned(self):
        return self.config.get("isPinned")

    @isPinned.setter
    def isPinned(self, value):
        if self.isPinned != value:
            self.config.set("isPinned", value)
            self.isPinnedChanged.emit(value)

    @Property(bool, notify=isDarkChanged)
    def isDark(self):
        return self.config.get("isDark")

    @isDark.setter
    def isDark(self, value):
        if self.isDark != value:
            self.config.set("isDark", value)
            self.isDarkChanged.emit(value)

    @Property(str, notify=defaultLangChanged)
    def defaultLang(self):
        return self.config.get("defaultLang")

    @defaultLang.setter
    def defaultLang(self, value):
        if self.defaultLang != value:
            self.config.set("defaultLang", value)
            self.defaultLangChanged.emit(value)
            if self._last_text:
                self.retranslateRequest.emit(self._last_text)

    @Property(str, notify=translationModeChanged)
    def translationMode(self):
        return self.config.get("translationMode")

    @translationMode.setter
    def translationMode(self, value):
        if self.translationMode != value:
            self.config.set("translationMode", value)
            self.translationModeChanged.emit(value)
            if self._last_text:
                self.retranslateRequest.emit(self._last_text)

    @Property(str, notify=triggerKeyChanged)
    def triggerKey(self):
        return self.config.get("triggerKey")

    @triggerKey.setter
    def triggerKey(self, value):
        if self.triggerKey != value:
            self.config.set("triggerKey", value)
            self.triggerKeyChanged.emit(value)

    @Slot(str)
    def copy(self, text):
        if not text:
            return
        try:
            QGuiApplication.clipboard().setText(str(text))
        except Exception as e:
            logger.error(f"Copy error: {e}")

    @Slot(str)
    def showToast(self, text):
        self.translationReceived.emit({"toast": text})

    @Property(int)
    def requestId(self):
        return self._request_id

    def incrementRequestId(self):
        self._request_id += 1
        return self._request_id

    @Slot()
    def unpin(self):
        self.isPinned = False

    @property
    def last_text(self):
        return self._last_text

    @last_text.setter
    def last_text(self, text):
        self._last_text = text

    @Property(bool)
    def isShowingFromTray(self):
        return self._is_showing_from_tray

    @isShowingFromTray.setter
    def isShowingFromTray(self, value):
        self._is_showing_from_tray = value
