#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
 @Author: Administrator
 @FileName: SerialRequest.py
 @DateTime: 2025/4/8 20:11
 @SoftWare: PyCharm
 @Author  ：xsh
"""
import sys
from PyQt5                              import Qt
from AppInterfaces.ModelInterface       import RequestModelInterface
from AppCore.Levels                     import *
from AppUtils.utils                     import *
from AppCore.Requests                   import *


class SerialRequest(RequestModelInterface):
    __slots__ = ('ReceivedBinary','TransmitBinary','ReceivedTails' ,'ReceivedWaits' ,'Handler'       )
    ResultNumber   : int
    ReceivedBinary : bytes
    TransmitBinary : bytes
    ReceivedTails  : bytes
    ReceivedWaits  : int
    Handler        : object

    def __init__(self, **args):
        super(SerialRequest, self).__init__()
        self.initialization("串口请求")
        self.initParameter(**args)

    def initParameter(self, **args):
        super(SerialRequest, self).initParameter(**args)
        self.ResultNumber   = args.get('ResultNumber'   , self.ResultNumber   )
        self.ReceivedBinary = args.get('ReceivedBinary' , self.ReceivedBinary )
        self.TransmitBinary = args.get('TransmitBinary' , self.TransmitBinary )
        self.ReceivedTails  = args.get('ReceivedTails'  , self.ReceivedTails  )
        self.ReceivedWaits  = args.get('ReceivedWaits'  , self.ReceivedWaits  )
        self.Handler        = args.get('Handler'        , self.Handler        )

    def initValue(self):
        super(SerialRequest, self).initValue()
        self.ResultNumber   = 0
        self.ReceivedBinary = b''
        self.TransmitBinary = b''
        self.ReceivedTails  = b"\r\n"
        self.ReceivedWaits  = 3000
        self.Handler        = None


    def initMicroscopeRequest(self):
        self.initValue()
        self.ResultNumber   = 0
        self.ReceivedBinary = b''
        self.TransmitBinary = b''
        self.ReceivedTails  = b'\n'
        self.ReceivedWaits  = 3000
        self.Handler        = None

    @classmethod
    def MicroscopeRequest(cls, buffer: str, handler: object = None) -> "SerialRequest":

        result: cls = cls.__new__(cls)
        args: dict[str, object] = {}
        args['ResultNumber'    ] = 0
        args['ReceivedBinary'  ] = b''
        args['TransmitBinary'  ] = cls.str_to_ascii(buffer)
        args['ReceivedTails'   ] = b'\r'
        args['ReceivedWaits'   ] = 300
        args['Handler'         ] = handler
        result.__init__(**args)
        return result

    @staticmethod
    def str_to_ascii(buffer: str) -> bytes:
        if not isinstance(buffer, str) or len(buffer) <= 0:
            return b'\r'
        result: bytes = buffer.encode('utf-8')
        if '\r' != buffer[-1]:
            result = f'{buffer}\r'.encode('utf-8')
        return result

    def copy(self):
        result = self.__class__.__new__(self.__class__)
        result.__init__(**self.serialize())
        return result








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

    @property
    def hasReceivedBinary (self) -> bool:
        return hasattr(self, 'ReceivedBinary') and isinstance(self.ReceivedBinary , (bytes, bytearray, Qt.QByteArray) )

    @property
    def hasTransmitBinary (self) -> bool:
        return hasattr(self, 'TransmitBinary') and isinstance(self.TransmitBinary , (bytes, bytearray, Qt.QByteArray) )

    @property
    def hasReceivedTails  (self) -> bool:
        return hasattr(self, 'ReceivedTails' ) and isinstance(self.ReceivedTails  , (bytes, bytearray, Qt.QByteArray) )

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

    @property
    def hasHandler        (self) -> bool:
        return hasattr(self, 'Handler'       ) and self.Handler is not None
