import copy
import os
import subprocess
import sys
from tkinter import *
import customtkinter
from src.util import Util


class Toolbar:
    def __init__(self, image_window, timer):
        self.timers = []
        self.current_image = None
        self.current_original_image = None
        self.image_window = image_window
        self.timer = timer
        self.timer_check = None
        self.always_on_top_check = None

        toolbar = Frame(self.image_window.window, bd=1, relief=RAISED)
        toolbar.pack(side=BOTTOM, fill=X)

        next_button = customtkinter.CTkButton(toolbar, compound=LEFT, text="next ➡️",
                                              command=self.image_window.next_image)
        next_button.pack(side=LEFT, padx=0, pady=0)

        self.bw_button = customtkinter.CTkButton(toolbar, compound=LEFT, text="black&white",
                                                 command=self.toggle_black_white)
        self.bw_button.pack(side=LEFT, padx=0, pady=0)

        self.mirror_button = customtkinter.CTkButton(toolbar, compound=LEFT, command=self.toggle_mirror,
                                                     text="mirror")
        self.mirror_button.pack(side=LEFT, padx=0, pady=0)

        open_folder_button = customtkinter.CTkButton(toolbar, compound=LEFT, command=self.open_folder,
                                                     text="open folder")
        open_folder_button.pack(side=LEFT, padx=0, pady=0)

        self.always_on_top_button = customtkinter.CTkButton(toolbar, compound=LEFT, command=self.toggle_always_on_top,
                                                            text="always on top")
        self.always_on_top_button.pack(side=LEFT, padx=0, pady=0)

        self.fullscreen_button = customtkinter.CTkButton(toolbar, compound=LEFT, command=self.toggle_fullscreen,
                                                         text="fullscreen")
        self.fullscreen_button.pack(side=LEFT, padx=0, pady=0)

        timer_button = customtkinter.CTkButton(toolbar, compound=LEFT, command=self.toggle_timer, text="timer")
        timer_button.pack(side=LEFT, padx=0, pady=0)

        self.timer_label = customtkinter.CTkLabel(toolbar, text=str(self.timer), text_color=Util.get_default_button_color())
        self.timer_label.pack(side=RIGHT, ipadx=20)

    def display_new_timer(self):
        self.timer = self.timers.pop(0)
        self.timer_label.configure(text=Util.format_seconds(self.timer))
        self.timer_check = self.image_window.window.after(1000, self.update_timer, self.timer)

    def update_timer(self, current):
        current -= 1
        if current == 10:
            self.image_window.play_countdown()
        if current > 0:
            self.timer_label.configure(text=Util.format_seconds(current))
            self.timer_check = self.image_window.window.after(1000, self.update_timer, current)
        else:
            self.image_window.window.after_cancel(self.timer_check)
            if len(self.image_window.image.images) == 0 or len(self.timers) == 0:
                self.image_window.window.destroy()
                self.image_window.window.update()
                return
            self.image_window.next_image()

    def toggle_black_white(self):
        self.image_window.option["bw"] = not self.image_window.option["bw"]
        self.bw_button.configure(fg_color=Util.get_default_active_button_color() if self.image_window.option["bw"] else Util.get_default_button_color())

        self.image_window.image.apply_options()

    def toggle_mirror(self):
        self.image_window.option["mirror"] = not self.image_window.option["mirror"]
        self.mirror_button.configure(fg_color=Util.get_default_active_button_color() if self.image_window.option["mirror"] else Util.get_default_button_color())

        self.image_window.image.apply_options()

    def toggle_timer(self):
        try:
            self.timer_label.pack_info()
            self.timer_label.pack_forget()
        except TclError:
            self.timer_label.pack(side=RIGHT, ipadx=20)

    def open_folder(self):
        if sys.platform == "win32":
            os.startfile(self.image_window.app.selected_folder)
        else:
            opener = "open" if sys.platform == "darwin" else "xdg-open"
            subprocess.call([opener, self.image_window.app.selected_folder])

    def toggle_always_on_top(self):
        self.image_window.option["always_on_top"] = not self.image_window.option["always_on_top"]
        if self.image_window.option["always_on_top"]:
            self.always_on_top_button.configure(fg_color=Util.get_default_active_button_color())
            self.stay_on_top()
        else:
            self.always_on_top_button.configure(fg_color=Util.get_default_active_button_color())
            self.image_window.window.after_cancel(self.always_on_top_check)

    def stay_on_top(self):
        # self.window.lift()
        self.image_window.window.attributes('-topmost', True)
        self.always_on_top_check = self.image_window.window.after(2000, self.stay_on_top)

    def toggle_fullscreen(self):
        self.image_window.option["fullscreen"] = not self.image_window.option["fullscreen"]
        self.fullscreen_button.configure(fg_color=Util.get_default_active_button_color() if self.image_window.option["fullscreen"] else Util.get_default_button_color())

        self.image_window.window.attributes("-fullscreen", self.image_window.option["fullscreen"])
