#!/usr/bin/python3

import argparse
import os

from keyman_config import add_standard_arguments, initialize_logging, initialize_sentry, verify_dbus_running
from keyman_config.get_kmp import get_keyman_dir, InstallLocation
from keyman_config.list_installed_kmp import get_installed_kmp


def main():
    parser = argparse.ArgumentParser(description='Show installed Keyman keyboard packages with name, version, id.')
    parser.add_argument('-l', "--long", help='additionally show description', action="store_true")
    parser.add_argument('-s', "--shared", help='show keyboard packages installed in shared area', action="store_true")
    parser.add_argument('-o', "--os", help='show keyboard packages installed by the OS', action="store_true")
    parser.add_argument('-u', "--user", help='show keyboard packages installed in user area', action="store_true")
    add_standard_arguments(parser)

    args = parser.parse_args()

    initialize_logging(args)
    initialize_sentry()
    verify_dbus_running()

    _all = not args.user and not args.shared and not args.os
    if args.user or _all:
        installed_kmp = get_installed_kmp(InstallLocation.User)
        if args.verbose or args.veryverbose:
            print("--- Installed user Keyman keyboard packages (%s) ---" % (get_keyman_dir(InstallLocation.User)))
        else:
            print("--- Installed user Keyman keyboard packages ---")
        print_keyboards(installed_kmp, args.long)
    if args.shared or _all:
        installed_kmp = get_installed_kmp(InstallLocation.Shared)
        if args.verbose or args.veryverbose:
            print("--- Installed shared Keyman keyboard packages (%s) ---" %
                  (get_keyman_dir(InstallLocation.Shared)))
        else:
            print("--- Installed shared Keyman keyboard packages ---")
        print_keyboards(installed_kmp, args.long)
    if args.os or _all:
        installed_kmp = get_installed_kmp(InstallLocation.OS)
        if args.verbose or args.veryverbose:
            print("--- Installed OS Keyman keyboard packages (%s) ---" % (get_keyman_dir(InstallLocation.OS)))
        else:
            print("--- Installed OS Keyman keyboard packages ---")
        print_keyboards(installed_kmp, args.long)


def print_keyboards(installed_kmp, detailed):
    for packageID in sorted(installed_kmp):
        print(f"{installed_kmp[packageID]['name']}, version: {installed_kmp[packageID]['kmpversion']}, id: {packageID}")
        if detailed:
            if installed_kmp[packageID]['version'] != installed_kmp[packageID]['kmpversion']:
                print("New version of keyboard available. Installed keyboard package is %s. Website has %s."
                      % (installed_kmp[packageID]['kmpversion'], installed_kmp[packageID]['version']))
            if installed_kmp[packageID]['name'] != installed_kmp[packageID]['kmpname']:
                print("Name mismatch. Installed keyboard package is %s. Website says it is %s."
                      % (installed_kmp[packageID]['name'], installed_kmp[packageID]['kmpname']))

            if installed_kmp[packageID]['description']:
                print(installed_kmp[packageID]['description'])
            else:
                print("No description")
            print(os.linesep)


if __name__ == "__main__":
    main()
