#!D:\IDE\Anaconda3\envs\Microscope_Scan python
# -*- coding: UTF-8 -*-
"""
@FileName   : HitBotConfigModel.py
@Account    : Administrator
@Time       : 2025/9/14 11:44
@Author     : xsh
@Description: 
"""
from PyQt5 import Qt
from datetime import datetime, date
import copy
from AppCore.AppDBModels.CoreModel import (CoreModel, DBManager, dbController)
from sqlalchemy.orm import declarative_base, mapper, sessionmaker, Session
from sqlalchemy.orm import (declarative_base, Mapped, mapped_column, relationship, mapper, sessionmaker)
from sqlalchemy import Table, Engine, create_engine, Column, ForeignKey, and_, or_, insert, delete, update, select, func, ColumnElement,distinct
from sqlalchemy.types import (Text, VARCHAR, REAL, Float, Integer, String, SmallInteger, DateTime, Date, ARRAY,Boolean, JSON)
from sqlalchemy.orm.decl_api import DeclarativeBase, MetaData
from AppUtils.utils import *
from AppCore.Levels import *
class HitBotConfigModel(CoreModel):
    __tablename__ = 't_HitBotConfigModel'
    ID                              : Mapped[int    ] = mapped_column(primary_key=True      , nullable=False, doc='数据序号　　　')
    Number                          : Mapped[int    ] = mapped_column(default=180           , nullable=True , doc='仪器编号　　　')
    InPort                          : Mapped[int    ] = mapped_column(default=7             , nullable=True , doc='状态输出端口　')
    CheckWaitTime                   : Mapped[int    ] = mapped_column(default=200           , nullable=True , doc='检测间隔时间　')
    ActionWaitTime                  : Mapped[int    ] = mapped_column(default=200           , nullable=True , doc='动作延时时长　')
    IsClamp                         : Mapped[int    ] = mapped_column(default=0             , nullable=True , doc='当前夹抓状态　')
    OperationWaitTime               : Mapped[int    ] = mapped_column(default=200           , nullable=True , doc='夹抓开关延时　')
    ChamberPackedLeadOffset         : Mapped[float  ] = mapped_column(default=-5.0          , nullable=True , doc='片仓取片提前量')
    CheckSpeed                      : Mapped[float  ] = mapped_column(default=200.0         , nullable=True , doc='检测玻片速度　')
    SlideSplit                      : Mapped[float  ] = mapped_column(default=3.34          , nullable=True , doc='检测玻片间距　')
    SplitOffset                     : Mapped[float  ] = mapped_column(default=0.03          , nullable=True , doc='玻片徧移距离　')

    def __init__(self, **args):
        super(HitBotConfigModel, self).__init__()
        self.initialize()
        self.initParameter(**args)


    def initValue(self):
        super(HitBotConfigModel, self).initValue()
        self.ID            = 1               #
        self.Number        = 180             #
        self.InPort        = 7               #
        self.CheckWaitTime = 200             #
        self.ActionWaitTime= 200             #
        self.IsClamp       = 0               #
        self.CheckSpeed    = 200.0           #
        self.SlideSplit    = 3.34            #
        self.SplitOffset   = 0.03            #
        self.ChamberPackedLeadOffset = -5.0
        self.OperationWaitTime       = 200

    @classmethod
    def getCheckParameter(cls) -> (int, int, float, float, float):
        item: cls = cls.first()
        if item is None:
            item = cls.instance()
            item.initValue()
        return item.InPort, item.CheckWaitTime, item.CheckSpeed, item.SlideSplit, item.SplitOffset

    @classmethod
    def getNumber        (cls) -> int    :
        try:
            return cls.first().Number
        except Exception as e:
            return 180

    @classmethod
    def getInPort        (cls) -> int    :
        try:
            return cls.first().InPort
        except Exception as e:
            return 7

    @classmethod
    def getCheckWaitTime (cls) -> int    :
        try:
            return cls.first().CheckWaitTime
        except Exception as e:
            return 300
    @classmethod
    def getActionWaitTime(cls) -> int:
        try:
            return cls.first().ActionWaitTime
        except Exception as e:
            return 300

    @classmethod
    def getIsClamp       (cls) -> int    :
        try:
            return cls.first().IsClamp
        except Exception as e:
            return False

    @classmethod
    def getCheckSpeed    (cls) -> int    :
        try:
            return cls.first().CheckSpeed
        except Exception as e:
            return 100

    @classmethod
    def getSlideSplit    (cls) -> float  :
        try:
            return cls.first().SlideSplit
        except Exception as e:
            return 3.34

    @classmethod
    def getSplitOffset(cls) -> float:
        try:
            return cls.first().SplitOffset
        except Exception as e:
            return 0.0

    @classmethod
    def getChamberPackedLeadOffset(cls) -> float:
        try:
            return cls.first().ChamberPackedLeadOffset
        except Exception as e:
            return -5.0

    @classmethod
    def getOperationWaitTime(cls) -> int:
        try:
            return cls.first().OperationWaitTime
        except Exception as e:
            return 0

    @classmethod
    def setOperationWaitTime(cls, value: int):
        try:
            cls.model_update({cls.OperationWaitTime: 0 if not isinstance(value, (float, int)) else int(value) }, cls.ID == 1)
        except Exception as e:
            pass
    @classmethod
    def initModels(cls) -> list:
        return [
            cls.instance(ID            = 1    ,Number        = 180  ,InPort        = 7    ,CheckWaitTime = 200  , ActionWaitTime = 200, IsClamp       = 0    ,CheckSpeed    = 200.0,SlideSplit    = 3.34 ,SplitOffset   = 0.03, ChamberPackedLeadOffset=-5.0, OperationWaitTime = 200),
        ]

    @classmethod
    def getConfig(cls, level: int):
        Level: int = level if level in ObjectiveLevel.all() else ObjectiveLevel.ObjectiveNone
        item: cls = cls.first()
        if item is None:
            item = cls.instance()
            item.initValue()
        return item

    @classmethod
    def setNumber        (cls, value):
        cls.model_update({cls.Number        : value}, cls.ID == 1)
    @classmethod
    def setInPort        (cls, value):
        cls.model_update({cls.InPort        : value}, cls.ID == 1)
    @classmethod
    def setCheckWaitTime (cls, value):
        cls.model_update({cls.CheckWaitTime : value}, cls.ID == 1)
    @classmethod
    def setIsClamp       (cls, value):
        enable: int = 1 if isinstance(value, (int, float, bool)) and value else 0
        cls.model_update({cls.IsClamp       : enable}, cls.ID == 1)
    @classmethod
    def setCheckSpeed    (cls, value):
        cls.model_update({cls.CheckSpeed    : value}, cls.ID == 1)
    @classmethod
    def setSlideSplit    (cls, value):
        cls.model_update({cls.SlideSplit    : value}, cls.ID == 1)
    @classmethod
    def setSplitOffset(cls, value):
        cls.model_update({cls.SplitOffset: value}, cls.ID == 1)
    @classmethod
    def setActionWaitTime(cls, value):
        cls.model_update({cls.ActionWaitTime: value}, cls.ID == 1)

    @classmethod
    def setChamberPackedLeadOffset(cls, value):
        cls.model_update({cls.ChamberPackedLeadOffset: value}, cls.ID == 1)










    __field__ = (
        'ID'                       ,
        'Number'                   ,
        'InPort'                   ,
        'CheckWaitTime'            ,
        'ActionWaitTime'           ,
        'IsClamp'                  ,
        'OperationWaitTime'        ,
        'ChamberPackedLeadOffset'  ,
        'CheckSpeed'               ,
        'SlideSplit'               ,
        'SplitOffset'              ,
    )
    __types__ = (
        int    ,
        int    ,
        int    ,
        int    ,
        int    ,
        int    ,
        int    ,
        float  ,
        float  ,
        float  ,
        float  ,
    )

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

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

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

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

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

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

    @property
    def hasChamberPackedLeadOffset (self) -> bool:
        return hasattr(self, 'ChamberPackedLeadOffset'  ) and isinstance(self.ChamberPackedLeadOffset, (float, int)    )

    @property
    def hasCheckSpeed              (self) -> bool:
        return hasattr(self, 'CheckSpeed'               ) and isinstance(self.CheckSpeed             , (float, int)    )

    @property
    def hasSlideSplit              (self) -> bool:
        return hasattr(self, 'SlideSplit'               ) and isinstance(self.SlideSplit             , (float, int)    )

    @property
    def hasSplitOffset             (self) -> bool:
        return hasattr(self, 'SplitOffset'              ) and isinstance(self.SplitOffset            , (float, int)    )

    @property
    def hasOperationWaitTime(self) -> bool:
        return hasattr(self, 'OperationWaitTime') and isinstance(self.OperationWaitTime, (float, int))