# -*- coding: utf-8 -*-
from setuphelpers import *
import platform

uninstallkey = []

# Defining variables
app_dir = makepath(programfiles32, "Driver Store Explorer")
app_bin_path = makepath(app_dir, "Rapr.exe")


def install():
    # Initializing variables
    package_version = control.version.split("-")[0]
    impacted_process_list = control.impacted_process.split(",")
    bin_name = "DriverStoreExplorer.v%s.zip" % package_version
    app_name = control.name

    # Getting installed version
    if isfile(app_bin_path):
        installed_version = get_version_from_binary(app_name)
    else:
        installed_version = ""

    # Installing the package
    print("Installing: %s" % app_name)
    if installed_version < package_version:
        killalltasks(impacted_process_list)
        if isdir(app_dir):
            remove_tree(app_dir)
        mkdirs(app_dir)
        print("Extracting: %s to: %s " % (bin_name, app_dir))
        unzip(bin_name, app_dir)
        create_programs_menu_shortcut(app_name, app_bin_path)

        # Adding the package to the "list-registry"
        register_windows_uninstall(control)  # control is a PackageEntry object corresponding to the package
    else:
        print("%s portable is already installed in the correct version" % app_name)


def uninstall():
    # Initializing variables
    impacted_process_list = control.version.split(",")
    app_name = control.name

    # Uninstalling the package
    print("Uninstalling: %s" % app_name)
    killalltasks(impacted_process_list)
    if isdir(app_dir):
        remove_tree(app_dir)
    unregister_uninstall("Driver Store Explorer")
    remove_programs_menu_shortcut(app_name)


def audit():
    # Initializing variables
    package_version = control.version.split("-")[0]
    app_version = get_version_from_binary(app_bin_path)

    if isdir(app_dir):
        if package_version == app_version:
            print("OK: Folder exists: %s" % app_dir)
            return "OK"
    else:
        print("ERROR: Folder not exists: %s" % app_dir)
        return "ERROR"





def get_proxies():
    import platform

    if platform.python_version_tuple()[0] == "3":
        from urllib.request import getproxies
    else:
        from urllib import getproxies
    return getproxies()


def get_proxies_from_wapt_console():
    proxies = {}
    if platform.system() == "Windows":
        waptconsole_ini_path = makepath(user_local_appdata(), "waptconsole", "waptconsole.ini")
    else:
        waptconsole_ini_path = makepath(user_home_directory(), ".config", "waptconsole", "waptconsole.ini")
    if isfile(waptconsole_ini_path):
        proxy_wapt = inifile_readstring(waptconsole_ini_path, "global", "http_proxy")
        if proxy_wapt:
            proxies = {"http": proxy_wapt, "https": proxy_wapt}
    return proxies


def get_version_from_binary(filename, parameter="ProductVersion"):
    if filename.endswith(".msi"):
        return get_msi_properties(filename)[parameter]
    else:
        return get_file_properties(filename)[parameter]


def remove_outdated_binaries(version, list_extensions=["exe", "msi", "deb", "rpm", "dmg", "pkg", "zip"], list_filename_contain=None):
    if type(list_extensions) != list:
        list_extensions = [list_extensions]
    if list_filename_contain:
        if type(list_filename_contain) != list:
            list_filename_contain = [list_filename_contain]
    list_extensions = ["." + ext for ext in list_extensions if ext[0] != "."]
    for file_ext in list_extensions:
        for bin_in_dir in glob.glob("*%s" % file_ext):
            if not version in bin_in_dir:
                remove_file(bin_in_dir)
            if list_filename_contain:
                for filename_contain in list_filename_contain:
                    if not filename_contain in bin_in_dir:
                        remove_file(bin_in_dir)
