#!/bin/env python3

import sys
import os
import json
import click

default_pkglist_cmd = 'echo a dummy get pkglist command'

def save_data():
    global g_file
    global g_data
    with open(g_file, 'w+') as f:
        f.write(json.dumps(g_data, sort_keys=True, indent=4))

def add_items_to_list(thelist, items):
    for item in items:
        if item not in thelist:
            thelist.append(item)

def del_items_from_list(thelist, items):
    for item in items:
        if item in thelist:
            thelist.remove(item)

@click.group()
@click.argument('file')
def cli(file):
    global g_file
    global g_data
    global g_pending

    if os.path.exists(file):
        with open(file, 'r+') as f:
            g_data = json.loads(f.read())
    else:
        print(f"warning!!! no such file '{file}'", file=sys.stderr)
        g_data = dict()

    if 'sync' not in g_data:
        g_data['sync'] = list()
    if 'ignore' not in g_data:
        g_data['ignore'] = list()
    if 'pkglist_cmd' not in g_data:
        g_data['pkglist_cmd'] = default_pkglist_cmd

    g_file = file

    # get pending list
    os.system(g_data['pkglist_cmd'] + " > pkglist.tmp.txt")
    with open("pkglist.tmp.txt") as f:
        l = f.read().split()
        l = map(lambda x: x.strip(), l)
        l = filter(lambda x: x not in g_data['ignore'] and x not in g_data['sync'], l)
        g_pending = list(l)
    os.system("rm pkglist.tmp.txt")

@click.command(name='list-pending')
def list_pending():
    global g_pending
    if len(g_pending) != 0:
        click.echo('\n'.join(g_pending))

@click.command(name='list-sync')
def list_sync():
    global g_data
    if len(g_data['sync']) != 0:
        click.echo('\n'.join(g_data['sync']))

@click.command(name='list-ignore')
def list_ignore():
    global g_data
    if len(g_data['ignore']) != 0:
        click.echo('\n'.join(g_data['ignore']))

@click.command()
@click.argument('args', nargs=-1)
def add(args):
    global g_data
    add_items_to_list(g_data['sync'], args)
    del_items_from_list(g_data['ignore'], args)
    save_data()

@click.command(name='add-all')
def add_all():
    global g_data
    global g_pending
    add_items_to_list(g_data['sync'], g_pending)
    del_items_from_list(g_data['ignore'], g_pending)
    save_data()

@click.command()
@click.argument('args', nargs=-1)
def ignore(args):
    global g_data
    add_items_to_list(g_data['ignore'], args)
    del_items_from_list(g_data['sync'], args)
    save_data()

@click.command(name='ignore-all')
def ignore_all():
    global g_data
    global g_pending
    add_items_to_list(g_data['ignore'], g_pending)
    save_data()

@click.command(name='del')
@click.argument('args', nargs=-1)
def delete(args):
    global g_data
    del_items_from_list(g_data['sync'], args)
    del_items_from_list(g_data['ignore'], args)
    save_data()


@click.command(name='set-pkglist-cmd')
@click.argument('args', nargs=-1)
def set_pkglist_cmd(args):
    global g_data
    g_data['pkglist_cmd'] = ' '.join(args)
    save_data()

cli.add_command(list_pending)
cli.add_command(list_sync)
cli.add_command(list_ignore)
cli.add_command(add)
cli.add_command(add_all)
cli.add_command(ignore)
cli.add_command(delete)
cli.add_command(ignore_all)
cli.add_command(set_pkglist_cmd)

if __name__ == '__main__':
    cli()
