__author__ = 'Vernon Wenberg III'

import tkinter as tk
import os
import tkinter.messagebox
import tkinter.filedialog
import tkinter.simpledialog
from tkinter import ttk
import subprocess
import sys

import viddb.dbfuncs
import viddb.dirfuncs


class Options(object):
    """
    Stores the directory sort option that has already been selected. Default sort method is by name.
    """
    sortby = None
    current_directory = ""

    @classmethod
    def set_sort_option(cls, sortby):
        Options.sortby = sortby

    @classmethod
    def set_current_directory(cls, directory):
        Options.current_directory = os.path.abspath(directory)


class DrawGui(Options):
    """
    Draws the main GUI
    """
    def __init__(self):
        self.root = tk.Tk()
        self.entry_string = tk.StringVar()
        self.dir_checkbutton_state = tk.IntVar()
        self.create_widgets(self.root)
        self.root.title('vidDB.py')
        self.root.geometry("1024x765")
        self.root.mainloop()

    def create_widgets(self, root):
        """
        Draws the main window and the majority of GUI widgets
        """
        self.dirfuncs = viddb.dirfuncs.DirFuncs()

        # Menu bar
        self.the_menu_bar = tk.Menu(self.root)
        self.root.config(menu=self.the_menu_bar)
        self.top = self.root.winfo_toplevel()
        self.top.rowconfigure(0, weight=1)
        self.top.columnconfigure(0, weight=1)
        self.root.rowconfigure(0, weight=1)
        self.root.columnconfigure(0, weight=1)

        # File menu
        self.the_file_menu = tk.Menu(self.the_menu_bar, tearoff=0)
        self.the_menu_bar.add_cascade(label="File", menu=self.the_file_menu)
        self.the_file_menu.add_command(label="sync", command=self.sync_database)
        self.the_file_menu.add_command(label="New Database", command=self.new_database)
        self.the_file_menu.add_command(label="Open Database", command=self.open_database)
        self.the_file_menu.add_command(label="Exit", command=root.quit)

        # Sort by sub-menu
        # Each sort option must pass a 'sortby' value which must then be created in self.build_dir_list
        # and class 'DirFuncs'.
        self.the_sort_menu = tk.Menu(root, tearoff=0)
        self.the_sort_menu.add_command(label="Name",
                                       command=lambda: self.build_dir_list(Options.current_directory, sortby="name"))
        self.the_sort_menu.add_command(label="Size",
                                       command=lambda: self.build_dir_list(Options.current_directory, sortby="size"))
        self.the_sort_menu.add_command(label="Date modified",
                                       command=lambda: self.build_dir_list(Options.current_directory,
                                                                           sortby="date_modified"))

        # View menu
        self.the_view_menu = tk.Menu(self.the_menu_bar, tearoff=0)
        self.the_menu_bar.add_cascade(label="View", menu=self.the_view_menu)