# Run tkinter code in another thread
import threading
import tkinter as tk
from tkinter import ttk
from mine_overlay import DiggingOverlay


class PrimaryOverlay(threading.Thread):

    def __init__(self):
        threading.Thread.__init__(self)
        self.MiningOverlay = DiggingOverlay
        self.run_mode = 'init'

        self.root = tk.Tk
        self.rb_frame = tk.Frame
        self.ButtonFrame = tk.Frame
        self.parameter_frame = tk.Frame
        self.parameter_label_frame = tk.Frame
        self.timing_frame = tk.Frame

        self.rb_int = tk.IntVar
        self.energy_use = tk.StringVar
        self.spawn_use = tk.StringVar
        self.emitter_use = tk.StringVar

        self.EnergyLabel = tk.Label
        self.EnergyEntry = tk.Entry
        self.SpawnLabel = tk.Label
        self.SpawnEntry = tk.Entry
        self.EmitterLabel = tk.Label
        self.Emitter_Box = ttk.Combobox

        self.RadioButtons = dict
        self.RadioButtonNames = ["Equip", "Crops", "Farm", "Magic", "Craft", "Mine"]
        self.RadioButton1 = tk.Radiobutton
        self.RadioButton2 = tk.Radiobutton
        self.RadioButton3 = tk.Radiobutton
        self.RadioButton4 = tk.Radiobutton
        self.RadioButton5 = tk.Radiobutton
        self.RadioButton6 = tk.Radiobutton

        self.StartButton = tk.Button
        self.StopButton = tk.Button
        self.PauseButton = tk.Button
        self.QuitButton = tk.Button
        self.TkPosition = '133x305+60+600'
        self.setDaemon(True)
        self.StatusLabel = tk.Label

        self.global_timeout_label = tk.Label
        self.hourly_breaks_label = tk.Label
        self.break_duration_label = tk.Label
        self.global_timeout_entry = tk.Entry
        self.hourly_breaks_entry = tk.Entry
        self.break_duration_entry = tk.Entry
        self.global_timeout_use = tk.StringVar
        self.hourly_breaks_use = tk.StringVar
        self.break_duration_use = tk.StringVar



        # self.TkPosition = config.returnEnemyPlayerOverlayPos()
        #
        self.start()

    def run(self):
        self.MiningOverlay = DiggingOverlay()
        self.root = tk.Tk()

        self.rb_frame = tk.Frame(self.root)
        self.rb_int = tk.IntVar(self.root, value=1)
        self.RadioButtons = dict()
        # var = tk.IntVar(value=1)
        for i in range(1, 7):
            self.RadioButtons[i] = tk.Radiobutton(self.rb_frame, text=self.RadioButtonNames[i - 1],
                                                  variable=self.rb_int,
                                                  value=i, command=self.radio_button_callback)
            self.RadioButtons[i].grid(row=i, column=0, sticky='w')

        self.parameter_frame = tk.Frame(self.root)
        self.energy_use = tk.StringVar(self.root, value='2000')
        self.spawn_use = tk.StringVar(self.root, value='25')
        self.emitter_use = tk.StringVar(self.root, value='multi')

        self.EnergyLabel = tk.Label(self.parameter_frame, text="Runs:", font=("Helvetica", 10, "bold"),
                                    background="grey", width='6')
        self.EnergyEntry = tk.Entry(self.parameter_frame, textvariable=self.energy_use, font=("Helvetica", 10, "bold"),
                                    width='10')
        self.SpawnLabel = tk.Label(self.parameter_frame, text="Spawn:", font=("Helvetica", 10, "bold"),
                                   background="grey", width='6')
        self.SpawnEntry = tk.Entry(self.parameter_frame, textvariable=self.spawn_use, font=("Helvetica", 10, "bold"),
                                   width='10')
        self.EmitterLabel = tk.Label(self.parameter_frame, text="Emitter:", font=("Helvetica", 10, "bold"),
                                     background="grey", width='6')
        self.Emitter_Box = ttk.Combobox(self.parameter_frame, textvariable=self.emitter_use,
                                        font=("Helvetica", 8, "bold"),
                                        width='9')
        self.Emitter_Box['values'] = ('multi', 'main', 'mushroom', 'amulett', 'sword', 'staff', 'wand', 'ring')

        self.EnergyLabel.grid(row=0, column=0, sticky='w')
        self.EnergyEntry.grid(row=0, column=1)
        self.SpawnLabel.grid(row=1, column=0, sticky='w')
        self.SpawnEntry.grid(row=1, column=1)
        self.EmitterLabel.grid(row=2, column=0, sticky='w')
        self.Emitter_Box.grid(row=2, column=1)

        self.ButtonFrame = tk.Frame(self.root)
        self.StartButton = tk.Button(self.ButtonFrame, text="Start", command=self.start_button_callback, width='5')
        self.StopButton = tk.Button(self.ButtonFrame, text="Stop", command=self.stop_button_callback, width='5',
                                    state=tk.DISABLED)
        self.PauseButton = tk.Button(self.ButtonFrame, text="Pause", command=self.pause_button_callback, width='5',
                                     state=tk.DISABLED)
        self.QuitButton = tk.Button(self.ButtonFrame, text="Quit", command=self.quit_button_callback, width='5',
                                    state=tk.NORMAL)

        self.StartButton.grid(row=0, column=0)
        self.StopButton.grid(row=1, column=0)
        self.PauseButton.grid(row=2, column=0)
        self.QuitButton.grid(row=4, column=0)

        self.StatusLabel = tk.Label(self.root, text="Nothing to see here", font=("Helvetica", 10, "bold"),
                                    background="grey", anchor=tk.CENTER)

        self.timing_frame = tk.Frame(self.root)

        self.global_timeout_use = tk.StringVar(self.root, value='0')
        self.hourly_breaks_use = tk.StringVar(self.root, value='0')
        self.break_duration_use = tk.StringVar(self.root, value='3')

        self.global_timeout_label = tk.Label(self.timing_frame, text="Stop after (h):", font=("Helvetica", 10, "bold"),
                                    background="grey", width='13')
        self.hourly_breaks_label = tk.Label(self.timing_frame, text="Breaks per hour:", font=("Helvetica", 10, "bold"),
                                    background="grey", width='13')
        self.break_duration_label = tk.Label(self.timing_frame, text="Break time (m):", font=("Helvetica", 10, "bold"),
                                    background="grey", width='13')
        self.global_timeout_entry = tk.Entry(self.timing_frame, textvariable=self.global_timeout_use, font=("Helvetica", 10, "bold"),
                                    width='2')
        self.hourly_breaks_entry = tk.Entry(self.timing_frame, textvariable=self.hourly_breaks_use, font=("Helvetica", 10, "bold"),
                                    width='2')
        self.break_duration_entry = tk.Entry(self.timing_frame, textvariable=self.break_duration_use, font=("Helvetica", 10, "bold"),
                                    width='2')



        self.global_timeout_label.grid(row=0, column=0, sticky='w', columnspan=2 )
        self.global_timeout_entry.grid(row=0, column=3)
        self.hourly_breaks_label.grid(row=1, column=0, sticky='w', columnspan=2)
        self.hourly_breaks_entry.grid(row=1, column=3)
        self.break_duration_label.grid(row=2, column=0, sticky='w', columnspan=2)
        self.break_duration_entry.grid(row=2, column=3)



        self.rb_frame.grid(row=0, column=0, sticky='w')
        self.ButtonFrame.grid(row=0, column=1, sticky='w')
        self.parameter_frame.grid(row=1, column=0, columnspan=2, sticky='w')
        self.timing_frame.grid(row=2, column=0, columnspan=2, sticky='w')
        self.StatusLabel.grid(row=3, column=0, columnspan=2)

        # self.ClearButton.pack(side="top")
        self.root.geometry(self.TkPosition)
        self.root.overrideredirect(1)  # fenster ohne aussen rum :-)
        # self.root.attributes('-alpha', 0.7)  # fenster transparent
        self.root.attributes('-topmost', 1)  # fenster immer im vordergrund
        # self.root.wm_attributes("-disabled", True)
        # self.root.configure(background='black')
        self.hide_mining_overlay()
        self.root.mainloop()

    def callback(self):
        self.root.quit()

    def destroy(self):
        self.hide_mining_overlay()
        self.root.destroy()

    def start_button_callback(self):
        self.StartButton.configure(state=tk.DISABLED)
        self.StopButton.configure(state=tk.NORMAL)
        self.PauseButton.configure(state=tk.NORMAL)
        self.QuitButton.configure(state=tk.DISABLED)
        for i in range(1, 7):
            tt = self.rb_int.get()
            if self.rb_int.get() != i:
                (self.RadioButtons[i]).configure(state=tk.DISABLED)
        self.StatusLabel.configure(text='started')
        self.run_mode = 'started'

    def stop_button_callback(self):
        self.StartButton.configure(state=tk.NORMAL)
        self.StopButton.configure(state=tk.DISABLED)
        self.PauseButton.configure(state=tk.DISABLED)
        self.QuitButton.configure(state=tk.NORMAL)
        for i in range(1, 7):
            self.RadioButtons[i].configure(state=tk.NORMAL)
        self.run_mode = 'stopped'

    def pause_button_callback(self):
        self.StartButton.configure(state=tk.NORMAL)
        self.StopButton.configure(state=tk.NORMAL)
        self.PauseButton.configure(state=tk.DISABLED)
        self.QuitButton.configure(state=tk.DISABLED)
        self.run_mode = 'paused'

    def quit_button_callback(self):
        self.run_mode = 'quit'

    def radio_button_callback(self):
        if self.rb_int.get() == 1:
            self.EnergyEntry.configure(state=tk.NORMAL)
            self.energy_use.set('2000')
            self.SpawnEntry.configure(state=tk.NORMAL)
            self.spawn_use.set('25')
            self.Emitter_Box.configure(state=tk.NORMAL)
            self.Emitter_Box.configure(values=('multi', 'main', 'mushroom', 'amulett', 'sword', 'staff', 'wand', 'ring'))
            self.EmitterLabel.configure(text="Emitter:")
            self.SpawnLabel.configure(text="Spawn:")
            self.EnergyLabel.configure(text="Runs:")
            self.emitter_use.set('multi')
            self.hide_mining_overlay()
        elif self.rb_int.get() == 2 or self.rb_int.get() == 3:
            self.EnergyEntry.configure(state=tk.DISABLED)
            self.energy_use.set('')
            self.SpawnEntry.configure(state=tk.DISABLED)
            self.spawn_use.set('')
            self.Emitter_Box.configure(state=tk.NORMAL)
            self.Emitter_Box.configure(values=('rainbow', 'bigbomb', 'rocket', 'bomb'))
            self.emitter_use.set('rainbow')
            self.EmitterLabel.configure(text="Strat:")
            self.SpawnLabel.configure(text="")
            self.EnergyLabel.configure(text="")
            self.hide_mining_overlay()
        elif self.rb_int.get() == 3:
            self.EnergyEntry.configure(state=tk.DISABLED)
            self.energy_use.set('')
            self.SpawnEntry.configure(state=tk.DISABLED)
            self.spawn_use.set('')
            self.Emitter_Box.configure(state=tk.DISABLED)
            self.emitter_use.set('')
            self.EmitterLabel.configure(text="")
            self.SpawnLabel.configure(text="")
            self.EnergyLabel.configure(text="")
            self.hide_mining_overlay()
        elif self.rb_int.get() == 4:
            self.EnergyEntry.configure(state=tk.DISABLED)
            self.energy_use.set('')
            self.SpawnEntry.configure(state=tk.NORMAL)
            self.spawn_use.set('100')
            self.Emitter_Box.configure(state=tk.DISABLED)
            self.emitter_use.set('')
            self.EmitterLabel.configure(text="")
            self.SpawnLabel.configure(text="Spawn:")
            self.EnergyLabel.configure(text="")
            self.hide_mining_overlay()
        elif self.rb_int.get() == 5:
            self.EnergyEntry.configure(state=tk.NORMAL)
            self.energy_use.set('100')
            self.SpawnEntry.configure(state=tk.DISABLED)
            self.spawn_use.set('')
            self.Emitter_Box.configure(state=tk.NORMAL)
            self.Emitter_Box.configure(values=('m25x1', 'm10x2', 'm5x3', 'simple', 'm1x5_T', 'm1x5_X', 'house'))
            self.emitter_use.set('simple')
            self.EmitterLabel.configure(text="Mode:")
            self.SpawnLabel.configure(text="")
            self.EnergyLabel.configure(text="Runs:")
            self.hide_mining_overlay()
        elif self.rb_int.get() == 6:
            self.EnergyEntry.configure(state=tk.NORMAL)
            self.energy_use.set('2')
            self.SpawnEntry.configure(state=tk.DISABLED)
            self.spawn_use.set('')
            self.Emitter_Box.configure(state=tk.DISABLED)
            self.emitter_use.set('')
            self.EmitterLabel.configure(text="")
            self.SpawnLabel.configure(text="")
            self.EnergyLabel.configure(text="Runs:")
            self.show_mining_overlay()

    #
    def get_run_mode(self):
        return self.run_mode

    def update_status_label(self, msg):
        self.StatusLabel.configure(text=msg)

    def hide_mining_overlay(self):
        self.MiningOverlay.hide_window()

    def show_mining_overlay(self):
        self.MiningOverlay.show_window()

    def get_strategy_value_as_int():
        if self.emitter_use == "rainbow":
            return 11
        elif self.emitter_use == "bigbomb":
            return 9
        elif self.emitter_use == "rocket":
            return 7
        elif self.emitter_use == "bomb":
            return 5
        else:
            return 11
