#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#    py-ard
#    Copyright (c) 2023 Be The Match operated by National Marrow Donor Program. All Rights Reserved.
#
#    This library is free software; you can redistribute it and/or modify it
#    under the terms of the GNU Lesser General Public License as published
#    by the Free Software Foundation; either version 3 of the License, or (at
#    your option) any later version.
#
#    This library is distributed in the hope that it will be useful, but WITHOUT
#    ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
#    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
#    License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with this library;  if not, write to the Free Software Foundation,
#    Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
#
#    > http://www.fsf.org/licensing/licenses/lgpl.html
#    > http://www.opensource.org/licenses/lgpl-license.php
#
import argparse
import sys

from pyard.constants import VALID_REDUCTION_TYPES
import pyard.misc
from pyard.exceptions import InvalidAlleleError
from pyard.misc import get_data_dir, get_imgt_version

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="""
        py-ard tool to redux GL String
        """,
    )
    parser.add_argument(
        "-v",
        "--version",
        dest="version",
        action="store_true",
        help="IPD-IMGT/HLA DB Version number",
    )
    parser.add_argument(
        "-d",
        "--data-dir",
        dest="data_dir",
        help="Data directory to store imported data",
    )
    parser.add_argument(
        "-i",
        "--imgt-version",
        dest="imgt_version",
        help="IPD-IMGT/HLA db to use for redux",
    )
    parser.add_argument("-g", "--gl", dest="gl_string", help="GL String to reduce")
    parser.add_argument(
        "-r",
        "--redux-type",
        choices=VALID_REDUCTION_TYPES,
        dest="redux_type",
        help="Reduction Method",
    )
    parser.add_argument("--splits", dest="splits", help="Find Broad and Splits")

    args = parser.parse_args()

    imgt_version = get_imgt_version(args.imgt_version)
    data_dir = get_data_dir(args.data_dir)
    ard = pyard.init(imgt_version=imgt_version, data_dir=data_dir)

    if args.version:
        version = ard.get_db_version()
        print(f"IPD-IMGT/HLA version:", version)
        print(f"py-ard version:", pyard.__version__)
        sys.exit(0)

    if args.splits:
        mapping = pyard.find_broad_splits(args.splits)
        if mapping:
            print(f"{mapping[0]} = {'/'.join(mapping[1])}")
        sys.exit(0)

    try:
        if args.redux_type:
            print(ard.redux(args.gl_string, args.redux_type))
        else:
            for redux_type in VALID_REDUCTION_TYPES:
                redux_type_info = f"Reduction Method: {redux_type}"
                print(redux_type_info)
                print("-" * len(redux_type_info))
                print(ard.redux(args.gl_string, redux_type))
                print()
    except InvalidAlleleError as e:
        print("Error:", e)

    # Remove ard and close db connection
    del ard
