#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：WebReaderInterface.py
@Description：
@Author：XSH
@Time：2025/11/11 15:03
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from PyQt5 import Qt
from abc import abstractmethod
from AppInterfaces.AppImplInterfaces.WebServerInterface import WebServerInterface, WebClientInterface, WebSocketContext
from AppInterfaces.ReaderInterface import ReaderInterface


class WebReaderInterface(ReaderInterface):
    Manager     : WebServerInterface    #
    Parameter   : WebSocketContext      #


    @property
    def hasManager(self) -> bool:
        return hasattr(self, 'Manager') and self.Manager is not None and isinstance(self.Manager, WebServerInterface)

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

    @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 hasParameter(self) -> bool:
        return hasattr(self, 'Parameter') and self.Parameter is not None and isinstance(self.Parameter, WebSocketContext)

    def Start(self) -> bool:
        if not self.hasManager:
            self.initManager()
        if self.hasManager:
            return self.Manager.Start()
        return False

    def Stop(self):
        self.Close()

    @abstractmethod
    def SendRequest(self, request, **args):
        pass

    @property
    @abstractmethod
    def isAvailable(self) -> bool:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def isValid(self) -> bool:
        raise Exception('Interface Instance error')

    @property
    @abstractmethod
    def IsOpen(self) -> bool:
        raise Exception('Interface Instance error')

    @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, **args):
        raise Exception('Interface Instance error')

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

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

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

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

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

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

    @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')