import random
import tkinter
import uuid
from tkinter import *
from tkinter.ttk import *

from common.hardware import Hardware
from config import conf


class WinGUI(Tk):
    def __init__(self):
        super().__init__()
        self.__win()
        # self.tk_label_lab_api_base = self.__tk_label_lab_api_base(self)
        # self.tk_input_api_base = self.__tk_input_api_base(self)
        self.tk_label_lab_api_key = self.__tk_label_lab_api_key(self)
        self.tk_input_api_key = self.__tk_input_api_key(self)
        self.tk_input_token = self.__tk_input_token(self)
        self.tk_label_lab_token = self.__tk_label_lab_token(self)
        # self.tk_button_save = self.__tk_button_save(self)
        self.tk_button_start = self.__tk_button_start(self)
        self.tk_button_pause = self.__tk_button_pause(self)
        self.tk_button_version = self.__tk_button_version(self)

    def __win(self):
        self.title("AI员工")
        # 设置窗口大小、居中
        width = 670
        height = 180
        screenwidth = self.winfo_screenwidth()
        screenheight = self.winfo_screenheight()
        geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
        self.geometry(geometry)

        self.resizable(width=False, height=False)

    def scrollbar_autohide(self, vbar, hbar, widget):
        """自动隐藏滚动条"""

        def show():
            if vbar: vbar.lift(widget)
            if hbar: hbar.lift(widget)

        def hide():
            if vbar: vbar.lower(widget)
            if hbar: hbar.lower(widget)

        hide()
        widget.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Enter>", lambda e: show())
        if vbar: vbar.bind("<Leave>", lambda e: hide())
        if hbar: hbar.bind("<Enter>", lambda e: show())
        if hbar: hbar.bind("<Leave>", lambda e: hide())
        widget.bind("<Leave>", lambda e: hide())

    def v_scrollbar(self, vbar, widget, x, y, w, h, pw, ph):
        widget.configure(yscrollcommand=vbar.set)
        vbar.config(command=widget.yview)
        vbar.place(relx=(w + x) / pw, rely=y / ph, relheight=h / ph, anchor='ne')

    def h_scrollbar(self, hbar, widget, x, y, w, h, pw, ph):
        widget.configure(xscrollcommand=hbar.set)
        hbar.config(command=widget.xview)
        hbar.place(relx=x / pw, rely=(y + h) / ph, relwidth=w / pw, anchor='sw')

    def create_bar(self, master, widget, is_vbar, is_hbar, x, y, w, h, pw, ph):
        vbar, hbar = None, None
        if is_vbar:
            vbar = Scrollbar(master)
            self.v_scrollbar(vbar, widget, x, y, w, h, pw, ph)
        if is_hbar:
            hbar = Scrollbar(master, orient="horizontal")
            self.h_scrollbar(hbar, widget, x, y, w, h, pw, ph)
        self.scrollbar_autohide(vbar, hbar, widget)

    def __tk_label_lab_api_base(self, parent):
        label = Label(parent, text="API_BASE", anchor="center", )
        label.place(x=30, y=30, width=60, height=30)
        return label

    def __tk_input_api_base(self, parent):
        ipt = Entry(parent, )
        ipt.insert(0, conf().get("api_base"))
        ipt.place(x=100, y=30, width=520, height=30)
        return ipt

    def __tk_label_lab_api_key(self, parent):
        label = Label(parent, text="设备号", anchor="center", )
        label.place(x=30, y=30, width=60, height=30)
        return label

    def __tk_input_api_key(self, parent):
        ipt = Entry(parent, )
        merchine_code = Hardware.get_machine_code()
        ipt.insert(0, 'PC-'+str(merchine_code))
        ipt.place(x=100, y=30, width=520, height=30)
        ipt.config(state='readonly')
        return ipt

    def __tk_input_token(self, parent):
        ipt = Entry(parent, )
        ipt.place(x=100, y=80, width=520, height=30)
        return ipt

    def __tk_label_lab_token(self, parent):
        label = Label(parent, text="TOKEN", anchor="center", )
        label.place(x=30, y=80, width=60, height=30)
        return label

    def __tk_button_save(self, parent):
        btn = Button(parent, text="保存", takefocus=False, )
        btn.place(x=30, y=190, width=80, height=30)
        return btn

    def __tk_button_start(self, parent):
        btn = Button(parent, text="启动", takefocus=False, )
        btn.place(x=30, y=130, width=80, height=30)
        return btn

    def __tk_button_pause(self, parent):
        btn = Button(parent, text="暂停", takefocus=False, state=tkinter.DISABLED )
        btn.place(x=130, y=130, width=80, height=30)
        return btn

    def __tk_button_version(self, parent):
        btn = Button(parent, text="版本", takefocus=False )
        btn.place(x=530,y=130, width=50, height=30)
        return btn


