import time

import keyboard

from Project.gui import GUI
from Project.src.globals import Store
import ttkbootstrap as ttk


def background_click_group_module():
    global background_click_group
    # 后台点击框架组件
    background_click_group = Store.root.set_init_labelframe("后台点击", 660, 216, 248, 420)  # 设置后台点击框架组件大小和位置
    background_click_labelframe = GUI(background_click_group)  # 实例化对象
    Store.background_click_group = background_click_group
    # 配置标签
    background_click_labelframe.set_label(
        {
            "点击类型": {
                "x": 16,
                "y": 24,
            },
            "每次点击间隔时间": {
                "x": 16,
                "y": 64,
            },
            "秒": {
                "x": 208,
                "y": 64,
            },
            "启动和停止和热键": {
                "x": 16,
                "y": 112,
            },
            "请输入正确的值":{
                "name": "time_tip",
                "label_style": "danger",
            },
            "未获取到指定的窗口": {
                "name": "hwnd_tip",
                "label_style": "danger",
            }
        }
    )
    Store.time_tip = background_click_labelframe.get_label_name().get("time_tip")
    Store.hwnd_tip = background_click_labelframe.get_label_name().get("hwnd_tip")
    # 配置文本框
    background_click_labelframe.set_entry(
        [
            {  # 每次点击间隔时间
                'width': 80,
                'x': 124,
                'y': 60,
                'var': 'var_time',
                'state': None
            },
            {  # 启动和停止和热键
                'width': 80,
                'x': 124,
                'y': 108,
                'var': 'var_hotkey',
                # 'state': None
            }
        ]
    )
    var = ttk.StringVar()  # 被选择单选框的值赋值给 var
    var.set(1)  # 默认选中鼠标左键
    # 配置单选框
    background_click_labelframe.set_radiobutton(
        var,
        {
            "鼠标左键": {
                'x': 80,
                'y': 25,
                'value': 1,
                'callback': 'radiobutton_value',
            },
            "鼠标右键": {
                'x': 160,
                'y': 25,
                'value': 2,
                'callback': 'radiobutton_value'
            },
        },
    )
    # 全局文本框 var 的值
    global background_click_vars
    background_click_vars = background_click_labelframe.get_local_vars()
    # 初始化间隔时间为1
    background_click_vars.get('var_time').set(1)
    # 初始化热键
    background_click_vars.get('var_hotkey').set('X')

    def get_key_value(even):
        if Store.flag:
            print("脚本启动中……")
            time.sleep(0.1)
        else:
            key = keyboard.read_key()
            background_click_vars.get('var_hotkey').set(key.upper())
            Store.background_click_entry_var_hotkey = background_click_labelframe.entry_value.get("entry_var_hotkey")
            Store.init_window.focus_set()
            time.sleep(0.1)

    background_click_labelframe.entry_value.get("entry_var_hotkey").bind('<Key>', get_key_value)
    # 用户输入在点击间隔时间文本框的值保存到globals全局变量模块
    Store.background_click_entry_var_time = background_click_labelframe.entry_value.get("entry_var_time")
    # 用户设置热键的值保存到globals全局变量模块
    Store.background_click_entry_var_hotkey = background_click_labelframe.entry_value.get("entry_var_hotkey")
    # 配置按钮
    background_click_labelframe.set_button(
        {
            '启动脚本': {
                'name': 'start',
                'x': 16,
                'y': 160,
                'callback': 'cycle_click',
            },
            '停止脚本': {
                'name': 'stop',
                'x': 16,
                'y': 160,
                'btn_style': 'danger',
                'callback': 'clear_cycle_click',
            }
        }
    )
    background_click_labelframe.get_button_value().get("stop").place_forget()
    Store.background_click_labelframe_get_button_value = background_click_labelframe.get_button_value()

