#!/usr/bin/python3
import argparse
import sys
import textwrap

from argo_probe_json.analysis import (
    check_equality, get_range, value_in_range, check_target_in_value,
    check_list_equality, parse_target_value
)
from argo_probe_json.exceptions import CriticalException, JsonException, \
    WarningException
from argo_probe_json.json import Json
from argo_probe_json.status import Status

NOTE = """
notes:
  The format for the warning and critical range is as follows:
  10 - raises alert when value is outside of [0, 10] range
  10: - raises alert when value is outside of [10, Inf] range
  """ + "  10:20 - raises alert when value is outside of [10, 20] range\n\n" \
      "examples:\n" \
      "  Checking that JSON response is valid\n" \
      "  /usr/libexec/argo/probes/json/check_json -u https://test.argo.eu/ " \
      "-t 30\n\n" \
      "  Checking that JSON contains a key 'test' with a value 'test_value'\n" \
      "  /usr/libexec/argo/probes/json/check_json -u https://test.argo.eu/ " \
      "-t 30 -k test -v test_value\n\n" \
      "  Checking that JSON contains a key 'test' and testing if it fits in" \
      " given warning and critical range\n" \
      "  /usr/libexec/argo/probes/json/check_json -u https://test.argo.eu/ " \
      "-t 30 -k 30 -w 3:5 -c 0:2"

USAGE = """
  Probe that checks JSON response given the URL
  -u URL -t TIMEOUT [-k KEY] [[[-v TARGET_VALUE] | [--is-true] | [--is-false]] 
  | [-w WARNING] [-c CRITICAL]] 
  [-h]
"""


def main():
    parser = argparse.ArgumentParser(
        add_help=False,
        usage=USAGE,
        formatter_class=argparse.RawTextHelpFormatter,
        epilog=textwrap.dedent(NOTE)
    )
    required = parser.add_argument_group("required arguments")
    optional = parser.add_argument_group("optional arguments")
    values = optional.add_mutually_exclusive_group()

    required.add_argument(
        "-u", "--url", dest="url", type=str, required=True,
        help="the URL we wish to check"
    )
    required.add_argument(
        "-t", "--timeout", dest="timeout", type=float, required=True,
        default=30, help="seconds before the request times out (default: 30)"
    )
    optional.add_argument(
        "-k", "--key", dest="key", type=str,
        help="the key in JSON for which we wish to check value; use dots to "
             "access a nested key"
    )
    values.add_argument(
        "-v", "--target-value", dest="target_value",
        help="the target value to which the value of the key must be equal; "
             "if the key's value is a list, either a single value that must "
             "be among the key's values, or the full expected list of "
             "values in the format [value1, value2]"
    )
    values.add_argument(
        "--is-true", dest="is_true", action="store_true",
        help="check if key value is True"
    )
    values.add_argument(
        "--is-false", dest="is_false", action="store_true",
        help="check if key value is False"
    )
    optional.add_argument(
        "-w", "--warning", dest="warning", type=str,
        help="warning range in format <floor>:<ceil>; if the inspected value "
             "is in the given range, the probe will return WARNING status"
    )
    optional.add_argument(
        "-c", "--critical", dest="critical", type=str,
        help="critical range in format <floor>:<ceil>; if the inspected value "
             "is in the given range, the probe will return CRITICAL status"
    )
    optional.add_argument(
        "-h", "--help", action="help", default=argparse.SUPPRESS,
        help="Show this help message and exit"
    )

    args = parser.parse_args()

    status = Status()
    json = Json(url=args.url, timeout=args.timeout)

    if (args.target_value or args.is_true or args.is_false) and (
            args.warning or args.critical
    ):
        parser.error(
            "You cannot use single value comparison and the ranges at the same "
            "time"
        )

    try:
        if args.key:
            value = json.parse(key=args.key)

            if args.target_value:
                target = parse_target_value(args.target_value)

                if type(value) is list:
                    if type(target) is list:
                        if check_list_equality(
                                value=value, target=target
                        ):
                            status.ok(
                                msg=f"{args.key} value is "
                                    f"{args.target_value}"
                            )

                        else:
                            status.critical(
                                msg=f"{args.key} value is not "
                                    f"{args.target_value}"
                            )

                    elif check_target_in_value(
                            value=value, target=target
                    ):
                        status.ok(
                            msg=f"Value {args.target_value} is among "
                                f"{args.key} values"
                        )

                    else:
                        status.critical(
                            msg=f"Value {args.target_value} is not among "
                                f"{args.key} values"
                        )

                elif type(value) is dict:
                    status.critical(f"Unable to check equality for dict")

                else:
                    if check_equality(value=value, target=target):
                        status.ok(
                            msg=f"{args.key} value is {args.target_value}"
                        )

                    else:
                        status.critical(
                            msg=f"{args.key} value is not {args.target_value}"
                        )

            elif args.is_true:
                if value is True:
                    status.ok(f"Value {args.key} is True")

                else:
                    status.critical(f"Value {args.key} is not True")

            elif args.is_false:
                if value is False:
                    status.ok(f"Value {args.key} is False")

                else:
                    status.critical(f"Value {args.key} is not False")

            elif args.warning or args.critical:
                status.ok("JSON response OK")
                if args.warning:
                    warning = get_range(args.warning)
                    if value_in_range(value=value, range_tuple=warning):
                        status.warning(
                            msg=f"Value {value} in range "
                                f"{warning[0]}:{warning[1]}"
                        )

                if args.critical:
                    critical = get_range(args.critical)
                    if value_in_range(value=value, range_tuple=critical):
                        status.critical(
                            msg=f"Value {value} in range "
                                f"{critical[0]}:{critical[1]}"
                        )

            else:
                status.ok(f"Key {args.key} present in JSON response")

        else:
            status.ok("JSON response OK")

    except CriticalException as e:
        status.critical(msg=str(e))

    except WarningException as e:
        status.warning(str(e))

    except JsonException as e:
        status.unknown(msg=str(e))

    print(status.get_msg())
    sys.exit(status.get_code())


if __name__ == "__main__":
    main()
