#!/usr/bin/python3
# -*- coding: utf-8 -*-

import argparse

from lxcmgr import lxcmgr
from lxcmgr.pkgmgr import App, PkgMgr

parser = argparse.ArgumentParser(description='LXC container and package manager')
subparsers = parser.add_subparsers()

parser_list = subparsers.add_parser('list')
subparsers_list = parser_list.add_subparsers()
parser_list_installed = subparsers_list.add_parser('installed')
parser_list_installed.set_defaults(action='list-installed')
parser_list_online = subparsers_list.add_parser('online')
parser_list_online.set_defaults(action='list-online')
parser_list_updates = subparsers_list.add_parser('updates')
parser_list_updates.set_defaults(action='list-updates')

parser_install = subparsers.add_parser('install')
parser_install.set_defaults(action='install')
parser_install.add_argument('app', help='Application to install')

parser_update = subparsers.add_parser('update')
parser_update.set_defaults(action='update')
parser_update.add_argument('app', help='Application to update')

parser_uninstall = subparsers.add_parser('uninstall')
parser_uninstall.set_defaults(action='uninstall')
parser_uninstall.add_argument('app', help='Application to uninstall')

parser_container = subparsers.add_parser('container')
subparsers_container = parser_container.add_subparsers()

parser_container_prepare = subparsers_container.add_parser('prepare')
parser_container_prepare.set_defaults(action='container-prepare')
parser_container_prepare.add_argument('layers', help='OverlayFS LXC rootfs layers')
parser_container_prepare.add_argument('container', help='Container name')
parser_container_prepare.add_argument('lxc', nargs=argparse.REMAINDER)

parser_container_cleanup = subparsers_container.add_parser('cleanup')
parser_container_cleanup.set_defaults(action='container-cleanup')
parser_container_cleanup.add_argument('container', help='Container name')
parser_container_cleanup.add_argument('lxc', nargs=argparse.REMAINDER)

parser_container_create = subparsers_container.add_parser('create')
parser_container_create.set_defaults(action='container-create')
parser_container_create.add_argument('container', help='Container name')
parser_container_create.add_argument('lxc', nargs=argparse.REMAINDER)

parser_container_destroy = subparsers_container.add_parser('destroy')
parser_container_destroy.set_defaults(action='container-destroy')
parser_container_destroy.add_argument('container', help='Container name')
parser_container_destroy.add_argument('lxc', nargs=argparse.REMAINDER)

def print_apps(packages):
    for app, meta in packages.items():
        print('{} {}'.format(app, meta['version']))
        for key, value in meta['meta'].items():
            print('  {}: {}'.format(key, value))

def list_online():
    pm = PkgMgr()
    pm.fetch_online_packages()
    print_apps(pm.online_packages['apps'])

def list_installed():
    pm = PkgMgr()
    pm.load_installed_packages()
    print_apps(pm.installed_packages['apps'])

def list_updates():
    pm = PkgMgr()
    pm.load_installed_packages()
    apps = [app for app in pm.installed_packages if pm.has_update(app)]
    updates = {name: meta for (name, meta) in pm.online_packages['apps'].items() if name in apps}
    print_apps(updates)

def install_app(app):
    pm = PkgMgr()
    app = App(app)
    pm.install_app(app)
    # TODO: periodicky vypisovat output

def update_app(app):
    pm = PkgMgr()
    pm.update_app(app)

def uninstall_app(app):
    pm = PkgMgr()
    pm.uninstall_app(app)

args = parser.parse_args()
if args.action == 'list-installed':
    list_installed()
elif args.action == 'list-online':
    list_online()
elif args.action == 'list-updates':
    list_updates()
elif args.action == 'install':
    install_app(args.app)
elif args.action == 'update':
    update_app(args.app)
elif args.action == 'uninstall':
    uninstall_app(args.app)
elif args.action == 'container-prepare':
    # Used with LXC hooks on container startup
    lxcmgr.prepare_container(args.container, args.layers)
elif args.action == 'container-cleanup':
    # Used with LXC hooks on container stop
    lxcmgr.cleanup_container(args.container)
elif args.action == 'container-create':
    # Used by package installer and builder
    lxcmgr.register_container(args.container)
elif args.action == 'container-destroy':
    # Used by package installer and builder
    lxcmgr.unregister_container(args.container)
