#!D:/IDE/anaconda3/envs/Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName：RequestInterface.py
@Description：
@Author：XSH
@Time：2025/12/24 14:55
@Department：研发部
@Copyright：©2025 乐辰生物
"""
from AppInterfaces.AppInterface import AppInterface
from AppUtils.utils import *
class RequestInterface(AppInterface):
    __slots__ = ()
    __types__ = ()

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

    @property
    def has_slots(self) -> bool:
        return hasattr(self, '__slots__')

    @property
    def has_types(self) -> bool:
        return hasattr(self, '__types__')

    def initParameter(self, **args):
        if self.has_types and self.has_slots and len(self.__slots__) == len(self.__types__):
            keys: list[str] = list(args.keys())
            src: object
            dst: object
            for index, _property in enumerate(self.__slots__):
                if _property in keys:
                    src = args.get(_property)
                    setattr(self, _property, src)



        pass

    def toJson(self) -> str:
        buffer: dict[str, object] = {}
        for key, value in vars(self).items():
            if key.startswith('_') or key.startswith('__'):
                continue
            if isinstance(value, AppDataInterface):
                buffer[f'{key}'] = AppConvert.convert_to_serializable(value.serialize())
            elif isinstance(value, AppInterface):
                continue
            else:
                buffer[f'{key}'] = AppConvert.convert_to_serializable(value)
        return ConvertUtils.toJson(buffer)

    def asJson(self, buffer: str):
        self.deserialize(ConvertUtils.asJson(buffer))


    def toYaml(self) -> str:
        buffer: dict[str, object] = {}
        for key, value in vars(self).items():
            if key.startswith('_') or key.startswith('__'):
                continue
            if isinstance(value, AppDataInterface):
                buffer[f'{key}'] = AppConvert.convert_to_serializable(value.serialize())
            elif isinstance(value, AppInterface):
                continue
            else:
                buffer[f'{key}'] = AppConvert.convert_to_serializable(value)
        return ConvertUtils.toJson(buffer)

    def asYaml(self, buffer: str):
        self.deserialize(ConvertUtils.asYaml(buffer))

    def serialize(self) -> dict[str, object]:
        buffer: dict[str, object] = {}
        for key, value in vars(self).items():
            if isinstance(value, AppDataInterface):
                if id(value) != id(self):
                    buffer[f'{key}'] = value.serialize()
            else:
                buffer[f'{key}'] = value
        return buffer

    def deserialize(self, buffer: dict[str, object]):
        _buffer: dict[str, object]
        if isinstance(buffer, dict):
            _buffer = buffer
        elif isinstance(buffer, AppDataInterface):
            if id(self) == id(buffer):
                return
            _buffer = buffer.serialize()

        else:
            return
        keys: list[str] = _buffer.keys()
        for key, value in vars(self).items():
            if key in keys:
                if isinstance(value, AppDataInterface):
                    if id(value) != id(self):
                        value.deserialize(_buffer.get(key, None))
                else:
                    self.__dict__[f'{key}'] = _buffer.get(key, None)
        return

    def asObject(self, other):
        if isinstance(other, AppDataInterface):
            self.deserialize(other.serialize())
        elif isinstance(other, dict):
            self.deserialize(other.serialize())
        elif isinstance(other, str):
            self.deserialize(other)

    def toObject(self, other):
        if isinstance(other, AppDataInterface):
            other.deserialize(self.serialize())
        elif isinstance(other, dict):
            other.update(self.serialize())

    def isEqual(self, other) -> bool:
        return True if other is not None and id(self) == id(other) else False

    @staticmethod
    def has__dict__(buffer) -> bool:
        return hasattr(buffer, '__dict__')



    def save(self) -> bool:
        return AppUtils.save_string_to_file(self.save_filename(), self.toYaml())

    def load(self) -> bool:
        buffer: str = AppUtils.read_string_from_file(self.save_filename())
        if AppUtils.isEmpty(buffer):
            return False
        self.asYaml(buffer)
        return True
        pass

    def reload(self) -> bool:
        isLoad: bool = self.load()
        if not isLoad:
            isLoad = self.save()
        return isLoad

    def save_filename(self) -> str:
        return AppUtils.toSourceFile(f"{self.Name}.yaml")