#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
# vim: se ts=4 et syn=python:

# created by: matteo.guadrini
# fp -- fontpreview
#
#     Copyright (C) 2020 Matteo Guadrini <matteo.guadrini@hotmail.it>
#
#     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 <http://www.gnu.org/licenses/>.

from argparse import ArgumentParser
from fontpreview import FontPreview
from fontpreview import __version__


def parse_arguments():
    """
    Function that captures the parameters and the arguments in the command line

    :return: parser object
    """
    # Create a common parser
    common_parser = ArgumentParser(add_help=False)
    common_parser.add_argument('--verbose', '-v', help='enable verbosity, for debug',
                               dest='verbose', action='store_true')

    # Create a principal parser
    parser_object = ArgumentParser(prog='fp', description='FontPreview cli', parents=[common_parser])
    parser_object.add_argument('--version', '-V', action='version', version='%(prog)s ' + __version__)
    parser_object.add_argument('font', help='font file path')
    parser_object.add_argument('-t', '--text', help='text include to preview image (default: a b c d e f)', dest='text')
    parser_object.add_argument('-b', '--background', help='background color (default: white)', dest='bg_color')
    parser_object.add_argument('-f', '--foreground', help='foreground color (default: black)', dest='fg_color')
    parser_object.add_argument('-i', '--background-image', help='background image path', dest='image')
    parser_object.add_argument('-d', '--dimension', help='dimension x and y (default: 700x327)', nargs=2,
                               dest='dimension', type=int)
    parser_object.add_argument('-s', '--save', help='save file path (default: current directory)', dest='save_path')
    parser_object.add_argument('-p', '--text-position', help='save file path (default: center)',
                               dest='text_position')
    parser_object.add_argument('-z', '--size', help='size of font (default: 64)', dest='size', type=int)

    # Return parser object
    return parser_object


def fp_arguments(arguments):
    """
    Check arguments and build dictionary arguments for FontPreview class

    :param arguments: argparse arguments
    :return: dictionary
    """
    # Initialize dictionary arguments
    fp_args = {}
    # Check all args passed in command line
    if arguments.font:
        fp_args['font'] = arguments.font
    if arguments.text:
        fp_args['font_text'] = arguments.text
    if arguments.bg_color:
        fp_args['bg_color'] = arguments.bg_color
    if arguments.fg_color:
        fp_args['fg_color'] = arguments.fg_color
    if arguments.image:
        fp_args['bg_image'] = arguments.image
    if arguments.dimension:
        fp_args['dimension'] = tuple(arguments.dimension)
    if arguments.size:
        fp_args['font_size'] = arguments.size
    # Return dictionary
    return fp_args


def v_print(verbose, *message):
    """
    Format verbosity message

    :param verbose: verbosity boolean
    :param message: list of message to print in verbosity mode
    :return: verbosity message
    """
    if verbose:
        print('DEBUG:', *message)


if __name__ == '__main__':
    # Parse arguments
    option = parse_arguments()
    args = option.parse_args()
    fpargs = fp_arguments(args)

    # Initialize object
    fp = FontPreview(**fpargs)

    # Check other args
    if args.image:
        v_print(args.verbose, 'set background with image {0}'.format(args.image))
        fp.bg_image = args.image
        fp.draw()
    if args.text_position:
        v_print(args.verbose, 'set text position: "{0}"'.format(args.text_position))
        fp.set_text_position(args.text_position)

    # Save image
    if args.save_path:
        fp.save(args.save_path)
    else:
        fp.save()

    v_print(args.verbose, 'font object => {0}'.format(fp))
