import argparse

from .version import VERSION


def generate_tileset_cli(parser):
    # Positional
    parser.add_argument(
        "image", type=argparse.FileType("rb"), nargs="+", help="input image file"
    )
    # Options
    parser.add_argument(
        "-c",
        "--output-c-file",
        type=argparse.FileType("w"),
        metavar="FILE",
        help="output C file",
    )
    parser.add_argument(
        "-H",
        "--output-header-file",
        type=argparse.FileType("w"),
        metavar="FILE",
        help="output C header file",
    )
    parser.add_argument(
        "-i",
        "--output-image",
        type=argparse.FileType("wb"),
        metavar="FILE",
        help="output image file representing the tileset (required to generate a tilemap)",
    )
    parser.add_argument(
        "-b",
        "--output-binary",
        type=argparse.FileType("wb"),
        metavar="FILE",
        help="output binary file",
    )
    parser.add_argument(
        "-d",
        "--deduplicate",
        action="store_true",
        default=False,
        help="remove duplicated tiles from the tileset",
    )
    parser.add_argument(
        "-a",
        "--alternative-palette",
        action="store_true",
        default=False,
        help="invert the colors",
    )
    parser.add_argument(
        "-s",
        "--sprite8x16",
        action="store_true",
        default=False,
        help="Rearrange the tiles to be used in 8x16 sprites",
    )
    parser.add_argument(
        "-n",
        "--name",
        type=str,
        default="TILESET",
        help="name of the tileset (used for variable names in generated code, default=TILESET)",
    )


def generate_tilemap_cli(parser):
    # Positional
    parser.add_argument(
        "tileset",
        type=argparse.FileType("rb"),
        help="the tileset (generated by img2gb tileset -i)",
    )
    parser.add_argument(
        "tilemap",
        type=argparse.FileType("rb"),
        help="an image representing the tilemap",
    )
    # Options
    parser.add_argument(
        "-c",
        "--output-c-file",
        type=argparse.FileType("w"),
        metavar="FILE",
        help="output C file",
    )
    parser.add_argument(
        "-H",
        "--output-header-file",
        type=argparse.FileType("w"),
        metavar="FILE",
        help="output C header file",
    )
    parser.add_argument(
        "-b",
        "--output-binary",
        type=argparse.FileType("wb"),
        metavar="FILE",
        help="output binary file",
    )
    parser.add_argument(
        "-o",
        "--offset",
        type=int,
        default=0,
        help="offset of the tileset in the video memory (default = 0)",
    )
    parser.add_argument(
        "-m",
        "--missing",
        choices=["error", "replace"],
        default="error",
        help="action to do when a tile of the tilemap is missing from the tileset (default = error)",
    )
    parser.add_argument(
        "-r",
        "--replace",
        type=int,
        metavar="TILE_ID",
        default=0,
        help="replace missing tiles by the given one when --missing=replace",
    )
    parser.add_argument(
        "-n",
        "--name",
        type=str,
        default="TILEMAP",
        help="name of the tileset (used for variable names in generated code, default=TILEMAP)",
    )


def generate_cli():
    parser = argparse.ArgumentParser(
        prog="img2gb", description="Converts images to GameBoy tilesets and tilemaps"
    )

    parser.add_argument("-V", "--version", action="version", version=VERSION)

    subparsers = parser.add_subparsers(dest="subcommand")

    tileset_parser = subparsers.add_parser("tileset", help="Generates GameBoy tilesets")
    generate_tileset_cli(tileset_parser)

    tilemap_parser = subparsers.add_parser("tilemap", help="Generates GameBoy tilemaps")
    generate_tilemap_cli(tilemap_parser)

    return parser


def parse_cli(args):
    parser = generate_cli()
    return parser.parse_args(args)
