#! /usr/bin/env python3
#
# YacGuide Android Application
#
# Copyright (C) 2020, 2023 Christian Sommer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

"""Development script for YacGuide Android Application"""

import argparse

import yacguide
import yacguide.utils
from yacguide.utils import error
import yacguide.app


def get_cli_parser():
    """Return argument parser object.

    Returns:
         argparse.ArgumentParser: Argument parser object
    """
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--debug", "-d", action="store_true",
                        help="Print debug information.")
    sub_parsers = parser.add_subparsers(help="Sub-command help")

    clean_parser = sub_parsers.add_parser(
        "clean", help="Run clean-up")
    clean_parser.set_defaults(func=clean_callback)
    help_clean_distclean = "clean everything"
    clean_parser.add_argument("--distclean", action="store_true",
                              default=False,
                              help=help_clean_distclean)

    release_parser = sub_parsers.add_parser(
        "release", help="Create release using the container")
    release_parser.set_defaults(func=release_callback)
    help_release_type = "release type"
    release_parser.add_argument("--type", required=True,
                                choices=yacguide.app.RELEASE_TYPES,
                                help=help_release_type)
    help_release_version = """release version string. For release
type 'dev' it is optional and defaults to the date code in the format
'YYYYMMDD'. For release type 'stable' the format is expected to be
'X.Y.Y'."""
    release_parser.add_argument("--version", help=help_release_version)
    help_release_skip_checks = """skip checks. Only use, if you know
what you do."""
    release_parser.add_argument("--skip-checks", action="store_true",
                                default=False,
                                help=help_release_skip_checks)
    return parser


def clean_callback(args):
    """Callback function for `clean` sub-parser.

    Args:
        args (argparse.Namespace): Parsed arguments
    """
    yacguide.utils.clean(distclean=args.distclean)


def release_callback(args):
    """Callback function for `release` sub-parser.

    Args:
        args (argparse.Namespace): Parsed arguments
    """
    release = yacguide.app.Release(skip_checks=args.skip_checks)
    release.pre_checks()
    if args.type == "dev":
        release.create_dev_release(args.version)
    elif args.type == "stable":
        if not args.version:
            error("Option --version must be set for"
                  " release type 'stable'.")
        release.create_stable_release(args.version)


if __name__ == "__main__":
    PARSER = get_cli_parser()
    ARGS = PARSER.parse_args()
    yacguide.init_logging(ARGS.debug)
    ARGS.func(ARGS)
