import logging
import json
from bos_toolbox.common import BOS_TOOLBOX_CONFIG_FILE, BOS_TOOLBOX_LANGUAGES

LOG = logging.getLogger(__name__)


def main(parser, args):
    file = open(BOS_TOOLBOX_CONFIG_FILE, 'r')
    config = json.load(file)
    file.close()

    file = open(BOS_TOOLBOX_CONFIG_FILE, 'w')

    if args.language[0] in BOS_TOOLBOX_LANGUAGES:
        config['language'] = args.language[0]
    else:
        config['language'] = 'english'

    json.dump(config, file)
    file.close()

    print('BOS Toolbox language set up properly, please restart the application')


def build_arg_parser(parser, desc):
    label = desc['label']
    hlp = desc['help']

    parser.description = desc['description']
    parser.add_argument(
        'language',
        nargs='+',
        metavar=label['language'],
        help=hlp['language'],
        choices=BOS_TOOLBOX_LANGUAGES,
        # widget='FilterableDropdown',
        widget='Listbox',
        gooey_options={
            # height of the listbox in pixels
            'height': 130,
            'show_help': False,
        },
    )
