#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：WebServerInterface.py
@Description：
@Author：XSH
@Time：2025/11/11 12:53
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from abc import abstractmethod
from AppInterfaces.ImplInterface import ImplInterface
from AppContexts.Contexts import WebSocketContext
from AppInterfaces.AppImplInterfaces.WebClientInterface import WebClientInterface
from AppSettings.Settings import RECODE_SUCCEED, RECODE_FAILURE
class WebServerInterface(ImplInterface):
    IOLocker    : Qt.QReadWriteLock = Qt.QReadWriteLock()
    Manager     : Qt.QWebSocketServer
    Parameter   : WebSocketContext
    Handles     : list[WebClientInterface]

    @property
    def manager(self) -> Qt.QWebSocket:
        return self.Manager if self.hasManager else None

    @property
    def parameter(self) -> WebSocketContext:
        return self.Parameter if self.hasParameter else None

    def setParameter(self, value):
        self.Parameter = value if isinstance(value, WebSocketContext) else None

    @property
    def handles (self) -> list:
        return self.Handles if self.hasHandles else None

    @property
    def hasHandles(self) -> bool:
        return hasattr(self, 'Handles') and isinstance(self.Handles, list               )

    @property
    def hasManager(self) -> bool:
        return hasattr(self, 'Manager') and isinstance(self.Manager, Qt.QWebSocketServer      )

    @property
    def hasParameter(self) -> bool:
        return hasattr(self, 'Parameter') and isinstance(self.Parameter, WebSocketContext)

    @property
    def isValid(self) -> bool:
        return self.hasManager and self.hasParameter

    @property
    def IsOpen(self) -> bool:
        return self.isValid and self.Manager.isListening()

    @property
    def isEnabled(self) -> bool:
        return self.isValid and self.Manager.isListening()

    @property
    def isDisable(self) -> bool:
        return not self.IsOpen

    @property
    def isAvailable(self) -> bool:
        return self.isValid and self.Parameter.IsOpen

    def ReOpen(self, **args) -> int:
        self.Close(**args)
        return self.Open(**args)

    def Start(self) -> bool:
        return self.Open() == RECODE_SUCCEED

    def Stop(self):
        self.Close()

    def indexOf(self, item) -> int:
        Lock: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        index: int = -1
        number: int = 0
        for client in self.Handles:
            if client == item:
                index = number
                break
            number += 1
        return index

    def __getitem__(self, key):
        if not self.hasHandles:
            self.initHandles()
        # Locker: Qt.QReadLocker = Qt.QReadLocker(self.IOLocker)
        if isinstance(key, int) and 0 <= key < len(self.Handles):
            return self.Handles[key]
        return None

    def __setitem__(self, key, value):
        if not self.hasHandles:
            self.initHandles()
        # Locker: Qt.QWriteLocker = Qt.QWriteLocker(self.IOLocker)
        if isinstance(key, int) and 0 <= key < len(self.Handles):
            old = self.Handles[key]
            if old == value:
                return
            self.Handles[key] = value
            if old is not None:
                del old
        else:
            self.Handles.append(value)

    @abstractmethod
    def Open(self, timeOut: int = 1000, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def Close(self, timeOut: int = 1000, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def initManager(self, **args):
        raise Exception('Interface Instance error')

    @abstractmethod
    def deleManager(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def initHandles(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def deleHandles(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def initContext(self):
        raise Exception('Interface Instance error')

    @abstractmethod
    def TransmitString(self, buffer, **args) -> int:
        raise Exception('Interface Instance error')

    @abstractmethod
    def TransmitBinary(self, buffer, **args) -> int:
        raise Exception('Interface Instance error')