from copy import deepcopy
from tkinter import ttk, Tk, S, RAISED, Frame, END, FALSE, IntVar, Checkbutton, StringVar, Label, Button


def draw_new_window(total_list):
    window_curr = Tk()
    window_curr.title('猜颜色--可能情况')
    window_curr.geometry('350x350')

    columns = ['第一项', '第二项', '第三项', '第四项']
    table = ttk.Treeview(
        master=window_curr,  # 父容器
        height=40,  # 表格显示的行数,height行
        columns=columns,  # 显示的列
        show='headings',  # 隐藏首列
    )
    table.heading('第一项', text='第一项')  # 定义表头
    table.heading('第二项', text='第二项', )  # 定义表头
    table.heading('第三项', text='第三项', )  # 定义表头
    table.heading('第四项', text='第四项', )  # 定义表头
    table.column('第一项', width=55, minwidth=45, anchor=S, )  # 定义列
    table.column('第二项', width=55, minwidth=45, anchor=S)  # 定义列
    table.column('第三项', width=55, minwidth=45, anchor=S)  # 定义列
    table.column('第四项', width=55, minwidth=45, anchor=S)  # 定义列
    table.pack(pady=20)

    total_list_words = []
    col_list = {0: '蓝', 1: '红', 2: '绿', 3: '黄', 4: '紫', 5: '白'}
    for item1 in total_list:
        curr = []
        for item in item1:
            curr.append(col_list[item])
        total_list_words.append(curr)

    for index, data in enumerate(total_list_words):
        table.insert('', END, values=data)  # 添加数据到末尾

    window_curr.mainloop()


def del_impossible_data(input_data, total_set):
    error_set = []

    for item in total_set:
        all_correct = 0
        half_correct = 0
        curr_item = deepcopy(item)
        for i in range(4):
            if input_data[i] == item[i]:
                all_correct += 1
            if input_data[i] in curr_item:
                half_correct += 1
                curr_item.remove(input_data[i])
        half_correct = half_correct - all_correct
        if not (all_correct == input_data[4] and half_correct == input_data[5]):
            error_set.append(item)

    for item in error_set:
        total_set.remove(item)


def result_page(user_input):
    total_list = []
    col_list = {'蓝': 0, '红': 1, '绿': 2, '黄': 3, '紫': 4, '白': 5}

    if user_input[0]:
        error_set = []
        for i1 in range(6):
            for i2 in range(6):
                for i3 in range(6):
                    for i4 in range(6):
                        total_list.append([i1, i2, i3, i4])

        for listx in total_list:
            curr_bool = FALSE
            dictx = {}
            for key in listx:
                dictx[key] = dictx.get(key, 0) + 1
            for value in dictx.values():
                if value > 2:
                    error_set.append(listx)
                elif value == 2:
                    if curr_bool:
                        error_set.append(listx)
                    else:
                        curr_bool = True

        for item in error_set:
            total_list.remove(item)

    else:
        for i1 in range(6):
            for i2 in range(6):
                if i2 not in [i1]:
                    for i3 in range(6):
                        if i3 not in [i1, i2]:
                            for i4 in range(6):
                                if i4 not in [i1, i2, i3]:
                                    total_list.append([i1, i2, i3, i4])

    temp_data = deepcopy(user_input[1:])
    for item in temp_data:
        item[0] = col_list[item[0]]
        item[1] = col_list[item[1]]
        item[2] = col_list[item[2]]
        item[3] = col_list[item[3]]
        item[4] = int(item[4])
        item[5] = int(item[5])
        del_impossible_data(item, total_list)

    draw_new_window(total_list)


def ask_page(frame_main):
    user_input = [0]
    col_list = ['蓝', '红', '绿', '黄', '紫', '白']

    frame1 = Frame(frame_main, relief=RAISED, borderwidth=2)
    frame2 = Frame(frame_main, relief=RAISED, borderwidth=2)
    frame3 = Frame(frame_main, relief=RAISED, borderwidth=2)
    frame4 = Frame(frame_main, relief=RAISED)
    frame1.pack(side='top', expand='no', fill='x')
    frame2.pack(side='top', expand='no', fill='x')
    frame3.pack(side='top', expand='no')
    frame4.pack(side='top', expand='yes', fill='both')

    columns = ['第一项', '第二项', '第三项', '第四项', '全对', '半对']
    table = ttk.Treeview(
        master=frame4,  # 父容器
        height=20,  # 表格显示的行数,height行
        columns=columns,  # 显示的列
        show='headings',  # 隐藏首列
    )
    table.heading('第一项', text='第一项')  # 定义表头
    table.heading('第二项', text='第二项', )  # 定义表头
    table.heading('第三项', text='第三项', )  # 定义表头
    table.heading('第四项', text='第四项', )  # 定义表头
    table.heading('全对', text='全对', )  # 定义表头
    table.heading('半对', text='半对', )  # 定义表头
    table.column('第一项', width=55, minwidth=45, anchor=S, )  # 定义列
    table.column('第二项', width=55, minwidth=45, anchor=S)  # 定义列
    table.column('第三项', width=55, minwidth=45, anchor=S)  # 定义列
    table.column('第四项', width=55, minwidth=45, anchor=S)  # 定义列
    table.column('全对', width=55, minwidth=45, anchor=S)  # 定义列
    table.column('半对', width=55, minwidth=45, anchor=S)  # 定义列
    table.pack(pady=20)

    def table_add_data(info):
        for index, data in enumerate(info):
            table.insert('', END, values=data)  # 添加数据到末尾

    def table_redraw(info):
        x = table.get_children()
        for item in x:
            table.delete(item)
        table_add_data(info)

    def change_userinput_first_num():
        user_input[0] = single_or_not.get()

    single_or_not = IntVar()
    single_or_not.set(0)
    Checkbutton(frame1, text='允许重色', variable=single_or_not, onvalue=1, offvalue=0,
                command=change_userinput_first_num).pack(side='left')

    comvalue1 = StringVar()
    comboxlist1 = ttk.Combobox(frame3, textvariable=comvalue1, width=2)
    comboxlist1["values"] = col_list
    comvalue1.set('蓝')
    comboxlist1.pack(side='left')

    comvalue2 = StringVar()
    comboxlist2 = ttk.Combobox(frame3, textvariable=comvalue2, width=2)
    comboxlist2["values"] = col_list
    comvalue2.set('蓝')
    comboxlist2.pack(side='left')

    comvalue3 = StringVar()
    comboxlist3 = ttk.Combobox(frame3, textvariable=comvalue3, width=2)
    comboxlist3["values"] = col_list
    comvalue3.set('蓝')
    comboxlist3.pack(side='left')

    comvalue4 = StringVar()
    comboxlist4 = ttk.Combobox(frame3, textvariable=comvalue4, width=2)
    comboxlist4["values"] = col_list
    comvalue4.set('蓝')
    comboxlist4.pack(side='left')

    Label(frame3, text='    全对:').pack(side='left')
    comvalue5 = StringVar()
    comboxlist5 = ttk.Combobox(frame3, textvariable=comvalue5, width=2)
    comboxlist5["values"] = (0, 1, 2, 3, 4)
    comvalue5.set(0)
    comboxlist5.pack(side='left')

    Label(frame3, text='    半对:').pack(side='left')
    comvalue6 = StringVar()
    comboxlist6 = ttk.Combobox(frame3, textvariable=comvalue6, width=2)
    comboxlist6["values"] = (0, 1, 2, 3, 4)
    comvalue6.set(0)
    comboxlist6.pack(side='left')

    def go_last():
        if len(user_input) > 1:
            user_input.pop()
            table_redraw(user_input[1:])

    def go_next():
        temp = [comvalue1.get(), comvalue2.get(), comvalue3.get(), comvalue4.get(), comvalue5.get(),
                comvalue6.get()]
        user_input.append(temp)
        table_add_data([temp])

    Button(frame2, bg='snow', text='上一个', font=('Arial', 12), command=go_last) \
        .pack(side='left', expand='no', fill='y')
    Button(frame2, bg='snow', text='下一个', font=('Arial', 12), command=go_next) \
        .pack(side='right', expand='no', fill='y')

    def run_program():
        result_page(user_input)

    Button(frame1, bg='snow', text='运行', font=('Arial', 12), command=run_program) \
        .pack(side='right', expand='no', fill='y')


def main():
    window = Tk()
    window.title('猜颜色辅助--zikai')
    window.geometry('350x350')
    frame_main = Frame(window, relief=RAISED)
    frame_main.pack()
    ask_page(frame_main)
    window.mainloop()


if __name__ == '__main__':
    main()
