import threading

import settings
from collections import defaultdict
import time
from .McuDebug import McuDebug


class OtherSet():
    def __init__(self, mcu):
        super().__init__()
        self.mcu = mcu
        self.get_value_flag = False
        self.last_value = defaultdict(float)
        is_debug = True if settings.IS_DEBUG == "true" else False
        self.mcu_debug = McuDebug(mcu, is_debug=is_debug, is_deviation=False)
        # self.ampy_run = None
        # 自动获取数据
        # QTimer.singleShot(500, self.get_data_from_mcu)

    def get_data_from_mcu(self, *args):
        self.get()

    def init(self):
        self.ui.is_auto_send_base_info.setMinimum(0)
        self.ui.is_auto_send_base_info.setMaximum(10000)
        self.ui.is_move_retry.setMinimum(0)
        self.ui.is_move_retry.setMaximum(10000)
        self.ui.is_data_response.setMinimum(0)
        self.ui.is_data_response.setMaximum(10000)
        self.ui.low_speed.setMinimum(0)
        self.ui.low_speed.setMaximum(10000)
        self.ui.is_test.setMinimum(0)
        self.ui.is_test.setMaximum(10000)
        self.ui.to_init_mode.setMinimum(0)
        self.ui.to_init_mode.setMaximum(10000)
        self.ui.turntable_move_to_init_mode.setMinimum(0)
        self.ui.turntable_move_to_init_mode.setMaximum(10000)
        self.ui.led_count.setMinimum(0)
        self.ui.led_count.setMaximum(10000)
        self.ui.turntable_steering_angle_ratio.setMinimum(0)
        self.ui.turntable_steering_angle_ratio.setMaximum(10000)
        self.ui.is_manual_check.setMinimum(0)
        self.ui.is_manual_check.setMaximum(10000)
        self.ui.camera_steering_angle_ratio.setMinimum(0)
        self.ui.camera_steering_angle_ratio.setMaximum(100000)
        self.ui.is_auto_motor_to_disable.setMinimum(0)
        self.ui.is_auto_motor_to_disable.setMaximum(10000)
        self.ui.diff_dir.setMinimum(0)
        self.ui.diff_dir.setMaximum(10000)
        self.ui.is_auto_send_pos_info.setMinimum(0)
        self.ui.is_auto_send_pos_info.setMaximum(10000)
        self.ui.is_dog.setMinimum(0)
        self.ui.is_dog.setMaximum(10000)
        self.ui.has_been_set_motor_config.setMinimum(0)
        self.ui.has_been_set_motor_config.setMaximum(10000)
        # 相机舵机
        self.ui.camera_degree.setSingleStep(0.1)
        self.ui.camera_degree.setMinimum(-90)
        self.ui.camera_degree.setMaximum(90)
        self.ui.camera_degree.valueChanged.connect(
            lambda x: self.change_value(x, "相机舵机")
        )

        self.ui.set.clicked.connect(self.set)
        self.ui.get.clicked.connect(self.get)
        self.ui.init_mcu.clicked.connect(self.init_mcu)
        self.ui.pushButton.clicked.connect(self.check_and_upload)
        self.ui.pushButton_2.clicked.connect(self.stop_mcu)
        # 设定相机舵机偏移量
        self.ui.set_camera_degree.clicked.connect(
            lambda *args: self.set_deviation("相机舵机")
        )

    def check_and_upload(self, *args):
        try:
            ampy_class = settings.PLUGINS_DICT["ampy_scribe"]["AMPY插件"]
        except:
            return
        self.s = threading.Thread(
            target=ampy_class(top_windows=self.windows, windows=self).run, args=()
        )
        self.s.start()

    def stop_mcu(self, *args):
        self.mcu.stop_mcu()

    def send_log(self, text):
        if self.mcu:
            self.mcu.send_log(text)

    # 写入文件测试
    def init_mcu(self, *args):
        self.mcu.to_init_device_origin_point(device_name="mcu", is_force=True)

    def change_value(self, value, name):
        value = round(value, 2)
        if self.last_value[name] - 1 <= value <= self.last_value[name] + 1:
            if name == "相机电机":
                self.mcu_debug.camera_high_motor(value=value)
            if name == "相机舵机":
                self.mcu_debug.camera_steering(value=value)
            if name == "转盘舵机":
                self.mcu_debug.turntable_steering(value=value)
            if name == "转盘前后电机":
                self.mcu_debug.turntable_position_motor(value=value)
            if name == "翻板舵机中位":
                self.mcu_debug.overturn_steering(value=value)
            if name == "翻板舵机高位":
                self.mcu_debug.overturn_steering(value=value)
            if name == "翻板舵机上升速度":
                pass
            print(value, name)
        self.last_value[name] = value

    def set_deviation(self, name, camera_steering_deviation):
        if name == "相机舵机":
            # 设定相机舵机偏移量
            # camera_steering_deviation = self.ui.camera_degree.value()
            device_name = "camera_steering"
            self.mcu.set_deviation(
                device_name=device_name, _type=0, deviation=camera_steering_deviation
            )
            # self.ui.camera_degree.setValue(0.0)

    def setMcuInfo(self, data):
        self.mcu.msg_type = "set_other_mcu_info"
        if not self.get_value_flag:
            self.mcu.sendSocketMessage(
                code=1, msg="请先获取其他mcu配置信息", device_status=0, data=None
            )
            self.mcu.msg_type = "mcu"
            return

        # is_auto_send_base_info = data.get("is_auto_send_base_info")
        # is_move_retry = data.get("is_move_retry")
        # is_data_response = data.get("is_data_response")
        # low_speed = data.get("low_speed")
        # is_test = data.get("is_test")
        # to_init_mode = data.get("to_init_mode")
        # turntable_move_to_init_mode = data.get("turntable_move_to_init_mode")
        # led_count = data.get("led_count")
        # turntable_steering_angle_ratio = data.get("turntable_steering_angle_ratio")
        # is_manual_check = data.get("is_manual_check")
        # camera_steering_angle_ratio = data.get("camera_steering_angle_ratio")
        # is_auto_motor_to_disable = data.get("is_auto_motor_to_disable")
        # diff_dir = data.get("diff_dir")
        # is_auto_send_pos_info = data.get("is_auto_send_pos_info")
        # is_dog = data.get("is_dog")
        # has_been_set_motor_config = data.get("has_been_set_motor_config")

        # data = {
        #     "is_auto_send_base_info": is_auto_send_base_info,
        #     "is_move_retry": is_move_retry,
        #     "is_data_response": is_data_response,
        #     "low_speed": low_speed,
        #     "is_test": is_test,
        #     "to_init_mode": to_init_mode,
        #     "turntable_move_to_init_mode": turntable_move_to_init_mode,
        #     "led_count": led_count,
        #     "turntable_steering_angle_ratio": turntable_steering_angle_ratio,
        #     "is_manual_check": is_manual_check,
        #     "camera_steering_angle_ratio": camera_steering_angle_ratio,
        #     "is_auto_motor_to_disable": is_auto_motor_to_disable,
        #     "diff_dir": diff_dir,
        #     "is_auto_send_pos_info": is_auto_send_pos_info,
        #     "is_dog": is_dog,
        #     "has_been_set_motor_config": has_been_set_motor_config,
        # }
        self.mcu.set_to_mcu_other_info(data)
        self.mcu.sendSocketMessage(code=0, msg="已完成设置", device_status=2, data=None)
        print("已完成设置")
        self.mcu.msg_type = "mcu"

    def getMcuInfo(self, *args):
        self.mcu.get_other_info()
        _s = time.time()
        last_num = self.mcu.last_mcu_other_info_data["num"]
        print("_s", _s)
        while 1:
            time.sleep(0.1)
            if time.time() - _s > 2:
                return False
            print(
                "last_mcu_other_info_data  num",
                self.mcu.last_mcu_other_info_data["num"],
            )
            if last_num < self.mcu.last_mcu_other_info_data["num"]:
                data = self.mcu.last_mcu_other_info_data["data"]
                print("获取中断", data)
                break
            # if self.mcu.last_mcu_other_info_data["time"] - _s > 0:
            #     data = self.mcu.last_mcu_other_info_data["data"]
            #     break
        print("获取成功")
        is_auto_send_base_info = data["is_auto_send_base_info"]
        is_move_retry = data["is_move_retry"]
        is_data_response = data["is_data_response"]
        low_speed = data["low_speed"]
        is_test = data["is_test"]
        to_init_mode = data["to_init_mode"]
        turntable_move_to_init_mode = data["turntable_move_to_init_mode"]
        led_count = data["led_count"]
        turntable_steering_angle_ratio = data["turntable_steering_angle_ratio"]
        is_manual_check = data["is_manual_check"]
        camera_steering_angle_ratio = data["camera_steering_angle_ratio"]
        diff_dir = data["diff_dir"]
        is_auto_send_pos_info = data["is_auto_send_pos_info"]
        is_dog = data["is_dog"]
        has_been_set_motor_config = data["has_been_set_motor_config"]

        is_auto_motor_to_disable = data["is_auto_motor_to_disable"]
        if is_auto_motor_to_disable is None:
            is_auto_motor_to_disable = False
        dataSend = {
            "is_auto_send_base_info": is_auto_send_base_info,
            "is_move_retry": is_move_retry,
            "is_data_response": is_data_response,
            "low_speed": low_speed,
            "is_test": is_test,
            "to_init_mode": to_init_mode,
            "turntable_move_to_init_mode": turntable_move_to_init_mode,
            "led_count": led_count,
            "turntable_steering_angle_ratio": turntable_steering_angle_ratio,
            "is_manual_check": is_manual_check,
            "camera_steering_angle_ratio": camera_steering_angle_ratio,
            "is_auto_motor_to_disable": is_auto_motor_to_disable,
            "diff_dir": diff_dir,
            "is_auto_send_pos_info": is_auto_send_pos_info,
            "is_dog": is_dog,
            "has_been_set_motor_config": has_been_set_motor_config,
        }
        self.get_value_flag = True
        self.mcu.msg_type = "get_other_mcu_info"
        self.mcu.sendSocketMessage(
            code=0, msg="获取mcu其他配置信息", device_status=2, data=dataSend
        )
        self.mcu.msg_type = "mcu"

    def __del__(self):
        self.state = 2
        self.t = None
