#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：WebBusinessInterface.py
@Description：
@Author：XSH
@Time：2025/12/2 17:25
@Department：研发部
@Copyright：©2025 乐辰生物
"""
import asyncio
from PyQt5 import Qt
from abc import abstractmethod, abstractclassmethod, abstractstaticmethod
from AppInterfaces.utils                    import *
from AppSettings.utils                      import *
from AppCore.Requests                       import *
from AppInterfaces.Interfaces               import *
from AppCore.Levels                         import *
from AppUtils.utils                         import *
class WebBusinessInterface(AdapterInterface):
    Manager : WebReaderInterface             # 服务端接口
    Handler : WebClientInterface             # 客户端接口
    Level   : int                            # 接口类型
    def deleManager(self):
        self.Manager = None

    def initManager(self):
        if self.Manager is None:
            from AppCore.AppReaders.WebReader import WebReader
            self.Manager = WebReader.GetInstance()



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

    @property
    def hasHandler(self) -> bool:
        return hasattr(self, 'Handler') and isinstance(self.Handler, WebClientInterface)

    @property
    def hasLevel(self) -> bool:
        return hasattr(self, 'Level') and isinstance(self.Level, int)


    @property
    def isValid(self) -> bool:
        return self.hasManager or self.hasHandler

    @property
    def isAvailable(self) -> bool:
        return self.hasHandler and self.Handler.isAvailable

    def initValue(self):
        super(WebBusinessInterface, self).initValue()
        self.initManager    ()
        self.initWEBAPILevel()

    def deleValue(self):
        super(WebBusinessInterface, self).deleValue()
        self.deleManager()


    @abstractmethod
    def initWEBAPILevel(self):
        pass

    @abstractmethod
    def Done(self, handler: WebClientInterface, **args):
        pass

    @abstractmethod
    def WaitDone(self, handler: WebClientInterface, **args) -> int:
        pass


    async def async_WaitDone(self, handler: WebClientInterface, **args):
        self.Done(handler, **args)
        return


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

    @property
    def handler(self) -> WebClientInterface:
        return self.Handler if self.hasHandler else None

    @property
    def level(self) -> int:
        return self.Level if self.hasLevel else WebAPILevel.NoneWebAPI


    def WebSendFrame(self, buffer: (str, bytes, bytearray, Qt.QByteArray, Request, Response)) -> int:
        isWrong: int
        if self.isAvailable:
            self.manager.Transmit(buffer)
            isWrong = RECODE_SUCCEED
        else:
            self.initManager()
            self.manager.Transmit(buffer)
            isWrong = RECODE_SUCCEED

        return isWrong


    def WebSendRequest(self, buffer: (object, dict, list, tuple)) -> int:
        isWrong: int = RECODE_FAILURE
        try:
            if not self.hasLevel:
                self.initWEBAPILevel()
            request: Request = Request.APIInstance(self.Level)
            request.data = buffer
            if self.isAvailable:
                self.manager.Transmit(request.toJson())
                isWrong = RECODE_SUCCEED
            else:
                self.initManager()
                self.manager.Transmit(request.toJson())
                isWrong = RECODE_SUCCEED
        except Exception as e:
            AppUtils.print_Exception(e)

        return isWrong

        pass