# -*- coding: utf-8 -*-


"""Parameters controller"""


import collections

from openfisca_core import legislations, periods

from .. import contexts, conv, environment, model, wsgihelpers


@wsgihelpers.wsgify
def api1_parameters(req):
    ctx = contexts.Ctx(req)
    headers = wsgihelpers.handle_cross_origin_resource_sharing(ctx)

    assert req.method == 'GET', req.method
    params = req.GET
    inputs = dict(
        instant = params.get('instant'),
        names = params.getall('name'),
        )

    parameters_name = [
        parameter_json['name']
        for parameter_json in model.parameters_json_cache
        ]

    data, errors = conv.pipe(
        conv.struct(
            dict(
                instant = conv.pipe(
                    conv.empty_to_none,
                    conv.test_isinstance(basestring),
                    conv.function(lambda str: periods.instant(str)),
                    ),
                names = conv.pipe(
                    conv.uniform_sequence(
                        conv.pipe(
                            conv.empty_to_none,
                            conv.test_in(parameters_name, error = u'Parameter does not exist'),
                            ),
                        drop_none_items = True,
                        ),
                    conv.empty_to_none,
                    ),
                ),
            default = 'drop',
            ),
        )(inputs, state = ctx)

    if errors is not None:
        return wsgihelpers.respond_json(ctx,
            collections.OrderedDict(sorted(dict(
                apiVersion = 1,
                error = collections.OrderedDict(sorted(dict(
                    code = 400,  # Bad Request
                    errors = [conv.jsonify_value(errors)],
                    message = ctx._(u'Bad parameters in request'),
                    ).iteritems())),
                method = req.script_name,
                params = inputs,
                url = req.url.decode('utf-8'),
                ).iteritems())),
            headers = headers,
            )

    tax_benefit_system = model.tax_benefit_system

    if data['instant'] is None:
        if data['names'] is None:
            parameters_json = model.parameters_json_cache
        else:
            parameters_json = [
                parameter_json
                for parameter_json in model.parameters_json_cache
                if parameter_json['name'] in data['names']
                ]
    else:
        instant = data['instant']
        parameters_json = []
        dated_legislation_json = legislations.generate_dated_legislation_json(
            tax_benefit_system.legislation_json,
            instant,
            )
        for name in data['names']:
            name_fragments = name.split('.')
            parameter_json = dated_legislation_json
            for name_fragment in name_fragments:
                parameter_json = parameter_json['children'][name_fragment]
            parameter_json['name'] = name
            parameter_json_in_cache = [
                parameter_json1
                for parameter_json1 in model.parameters_json_cache
                if parameter_json1['name'] == name
                ][0]
            parameter_json['description'] = parameter_json_in_cache['description']
            parameters_json.append(parameter_json)

    return wsgihelpers.respond_json(ctx,
        collections.OrderedDict(sorted(dict(
            apiVersion = 1,
            country_package_git_head_sha = environment.country_package_git_head_sha,
            currency = tax_benefit_system.CURRENCY,
            method = req.script_name,
            parameters = parameters_json,
            # parameters_file_path = model.parameters_file_path,
            url = req.url.decode('utf-8'),
            ).iteritems())),
        headers = headers,
        )

@@ -3349,92 +3349,28 @@
   re
-turn wsgihelpers.respond_json(ctx,%0A        collections.OrderedDict(sorted(
+sponse_dict = 
 dict(%0A
-    
     
@@ -3381,36 +3381,32 @@
 apiVersion = 1,%0A
-    
         country_
@@ -3474,64 +3474,8 @@
 ha,%0A
-            currency = tax_benefit_system.CURRENCY,%0A    
     
@@ -3496,36 +3496,32 @@
 eq.script_name,%0A
-    
         paramete
@@ -3554,121 +3554,254 @@
     
-    # parameters_file_path = model.parameters_file_path,%0A            url = req.url.decode('utf-8'),%0A            )
+url = req.url.decode('utf-8'),%0A        )%0A    if hasattr(tax_benefit_system, 'CURRENCY'):%0A        response_dict%5B'currency'%5D = tax_benefit_system.CURRENCY%0A    return wsgihelpers.respond_json(ctx,%0A        collections.OrderedDict(sorted(response_dict
 .ite

Return currency only if tax benefit system supports it