from modules.ui import *

AUTOINSTALL_TXT = 'Custom/PluginData/sfishere/AutoInstall.txt'
FAVORITES_TXT = 'Custom/PluginData/sfishere/Favorites.txt'

VERSION = 'v1.12'
"""
Changelog:
-Fixed Uninstall all vars, broken after 1.08
-Fixed dummyvars restoration moved to bad position it did not need to

Changelog 1.11:
-Fixed couple of bugs
-Added cool flat themes for main window. Not added for dialogs because are not centered and require handle of 'cancel'.
-Fixed scan scope for (undocumented yet) unpacked packages detector, now restricted to non-Scenes.

Changelog 1.10:
-Fixed bug (again, now properly) in vam_functions.py, exception with favorites.txt is not present Thanks to cdt_dreks!
-Fixed bug in packages.py, introduced in function redesign. Thanks to cdt_dreks!

Changelog 1.09:
-Fixed bug in db_functions.py, name+'.' to avoid ambiguity in names with similar substrings) 
-Added UTF-8 encoding specification for loading config file. Thanks to yuezy3!
-Fixed bug in vam_functions.py, exception when favorites.txt is not present, now returns empty list. Thanks to cdt_dreks!
-Added option to install package plus available dependencies, like Varbrowser does, and with recursivity)

Changelog  1.08:
-Fixed bug when copying vars that matched a name with longer name, because name was not including version delimiter'.'.
-Fixed bug when copying vars after another process. It required to press Refresh button before copyiing. Now it is not needed.
-Some code files reorganized
-Added Dependency usage filter from packages section also (menu and hotkey)
-Added Varbrowser's Favorites (view only, not scene-specific, just package)
-Changed Dependencies info view: it was confusing because it needed the packages view to be unfiltered or it would not show info. 
 Now it clears all filters. (To work as I intended, part of the code would have to be rewritten. I can not do it now)
-Added install symlink option on external packages, by pressing key L or by using menu. 
 Symlinks are copied (with their relative paths) to AddonPackages/symlinks. Other than that, they are the same as installed vars.
 Symlinked packages can be uninstalled but no files are removed. They move to AllPackages/symlinks and turn to 'available'.
 If you want to destroy the symlinks you can do it manually by deleting the symlinks subdirectories
"""

# This allows to build executable with the resources (icon, etc) included in the binary file
def resource_path(relative_path):
    import os
    import sys

    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)


def main():
    print(f"\nVambropy {VERSION} - Vam + VarBrowser helper tool")

    # Set the current working directory to the script's directory - code warning: disable when compiling with pyinstaller. Find out how to make it wokd both ways
    # script_dir = os.path.dirname(os.path.abspath(__file__))
    # os.chdir(script_dir)

    # Instance of tkinter UI
    gui = tk.Tk()

    # Window icon
    gui.iconbitmap(resource_path('hand_v.ico'))

    # GUI geometry, constraints and weights
    gui.rowconfigure(0, minsize=516, weight=3)
    gui.columnconfigure(0, minsize=960, weight=3)
    gui.columnconfigure(2, minsize=516, weight=3)

    # Create app instance, pass database name
    app = PackagesApp(gui, 'packages.db')

    # Thumbnail defaults
    app.thumbnail_size= 512
    # VarBrowser's autoinstall.txt file location
    app.autoinstall_path = AUTOINSTALL_TXT
    app.favorites_path = FAVORITES_TXT
    # Load defaults from config.ini
    app.load_config('config.ini')
    app.set_status(f'{VERSION}. Loading packages info. It may take some time...')

    # Update files and database
    app.refresh(all=True)
    # Update Treeview
    app.load_filtered_data()

    # First filter takes cursor focus
    app.filter_entries_p['var_name'].focus()

    gui.mainloop()


if __name__ == "__main__":
    main()
