#!/usr/bin/env python3

import argparse
import json
import os

try:
    import protool
except:
    import sys
    sys.path.insert(0, os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..")))
    import protool


def _handle_diff(args):
    """Handle the diff sub command."""

    if len(args.profiles) != 2:
        print("Expected 2 profiles for diff command")
        sys.exit(1)

    print(protool.diff(args.profiles[0], args.profiles[1], ignore_keys=args.ignore, tool_override=args.tool))


def _handle_read(args):
    """Handle the read sub command."""

    value = protool.value_for_key(args.profile, args.key)
    regular_types = [str, int, float]
    found_supported_type = False

    for regular_type in regular_types:
        if isinstance(value, regular_type):
            found_supported_type = True
            print(value)
            break

    if not found_supported_type:
        try:
            result = json.dumps(value)
        except:
            print("Unable to serialize values. Please use the XML format instead.")
            sys.exit(1)

        print(result)


def _handle_decode(args):
    """Handle the decode sub command."""
    print(protool.decode(args.profile))


def _handle_arguments():
    """Handle command line arguments and call the correct method."""

    parser = argparse.ArgumentParser()

    subparsers = parser.add_subparsers()

    diff_parser = subparsers.add_parser('diff', help="Perform a diff between two profiles")
    diff_parser.add_argument("-i", "--ignore", dest="ignore", action="store", nargs='+', default=None, help='A list of keys to ignore. e.g. --ignore TimeToLive UUID')
    diff_parser.add_argument("-t", "--tool", dest="tool", action="store", default=None, help='Specify a diff command to use. It should take two file paths as the final two arguments. Defaults to opendiff')
    diff_parser.add_argument("-p", "--profiles", dest="profiles", action="store", nargs=2, required=True, help='The two profiles to diff')
    diff_parser.set_defaults(subcommand="diff")

    read_parser = subparsers.add_parser('read', help="Read the value from a profile using the key specified command")
    read_parser.add_argument("-p", "--profile", dest="profile", action="store", required=True, help='The profile to read the value from')
    read_parser.add_argument("-k", "--key", dest="key", action="store", required=True, help='The key to read the value for')
    read_parser.set_defaults(subcommand="read")

    decode_parser = subparsers.add_parser('decode', help="Decode a provisioning profile and display in a readable format")
    decode_parser.add_argument("-p", "--profile", dest="profile", action="store", required=True, help='The profile to read the value from')
    decode_parser.set_defaults(subcommand="decode")

    args = parser.parse_args()

    try:
        _ = args.subcommand
    except:
        parser.print_help()
        sys.exit(1)

    if args.subcommand == "diff":
        _handle_diff(args)
    elif args.subcommand == "read":
        _handle_read(args)
    elif args.subcommand == "decode":
        _handle_decode(args)


if __name__ == "__main__":
    _handle_arguments()
