# Path: lcopt/io.py
# def exchange_factory(input, type, amount, uncertainty, comment, **kwargs):
#     data_structure = {
#         'input': input,
#         'type': type,
#         'amount': amount,
#         'uncertainty type': uncertainty,
#         'comment': comment,
#     }
# 
#     for kw in kwargs:
#         data_structure[kw] = kwargs[kw]
#         
#     return data_structure
# 
# Path: lcopt/parameters.py
# class LcoptParameterSet(ParameterSet):
#     """
#     Subclass of `bw2parameters.parameter_set.ParameterSet` that takes a `lcopt.LcoptModel` and delegates parameter ordering and evaluation to `bw2parameters`
#     
#     TODO: Add more documentation and write tests
#     """
#     
#     def __init__(self, modelInstance):
#         
#         self.modelInstance = modelInstance
#         
#         self.norm_params = self.normalise_parameters()
# 
#         self.check_production_parameters_exist()
#         
#         self.all_params = {**self.modelInstance.params, **self.modelInstance.production_params, **self.norm_params, **self.modelInstance.allocation_params}
#         
#         self.bw2_params, self.bw2_global_params, self.bw2_export_params = self.lcopt_to_bw2_params(0)
#         
#         
#         super().__init__(self.bw2_params, self.bw2_global_params)
#         
#         self.evaluated_parameter_sets = self.preevaluate_exchange_params()
#         
#     def lcopt_to_bw2_params(self, ps_key):
#         
#         k0 = list(self.modelInstance.parameter_sets.keys())[ps_key]
#         ps1 = self.modelInstance.parameter_sets[k0]
#         
#         bw2_params = {k:{(x if x != 'function' else 'formula'):y for x, y in v.items()} for k,v in self.all_params.items()}
#         
#         for k in bw2_params.keys():
#             bw2_params[k]['amount'] = ps1.get(k,0)
#             
#         bw2_global_params = {x['name']: ps1.get(x['name'],x['default']) for x in self.modelInstance.ext_params}
# 
#         bw2_export_params = []
# 
#         for k, v in bw2_params.items():
#             to_append = {'name': k}
#             if v.get('formula'):
#                 to_append['formula'] = v['formula']
#             else:
#                 to_append['amount'] = v['amount']
#                 
#             bw2_export_params.append(to_append)
#             
#         for k, v in bw2_global_params.items():
#             bw2_export_params.append({'name':k, 'amount':v})
#         
#         return bw2_params, bw2_global_params, bw2_export_params
#     
#     def normalise_parameters(self):
#         
#         param_copy = deepcopy(self.modelInstance.params)
#         #production_params = deepcopy(self.modelInstance.production_params)
#         #allocation_params = deepcopy(self.modelInstance.allocation_params)
#         norm_params = OrderedDict()
#         for k, v in param_copy.items():
#             norm_params['n_{}'.format(k)] = {}
#             for key, item in v.items():
#                 if key == 'function':
#                     if not item:
#                         norm_function = '{} / {}'.format(k, v['normalisation_parameter'])
#                     else:
#                         norm_function = '({}) / {}'.format(item, v['normalisation_parameter'])
#                     
#                     norm_params['n_{}'.format(k)][key] = norm_function
#                 else:
#                     norm_params['n_{}'.format(k)][key] = item
#         return norm_params
#     
#     def preevaluate_exchange_params(self):
#         
#         evaluated_params = OrderedDict()
#         
#         for n, k in enumerate(self.modelInstance.parameter_sets.keys()):
#             
#             self.params, self.global_params, _ = self.lcopt_to_bw2_params(n)
#             self.evaluate_and_set_amount_field()
#             
#             this_set = {}
#             
#             for j, v in self.params.items():
#                 this_set[j] = v['amount']
#                 
#             evaluated_params[k] = this_set
#                 
#         self.params, self.global_params , _ = self.lcopt_to_bw2_params(0)
#         self.evaluate_and_set_amount_field()
#         
#             
#             
#         return evaluated_params
# 
#     def check_production_parameters_exist(self):
#         """ old versions of models won't have produciton parameters, leading to ZeroDivision errors and breaking things"""
#         for k, v in self.modelInstance.parameter_sets.items():
#             for p_id in self.modelInstance.production_params.keys():
#                 if v.get(p_id):
#                     #print('{} already exists'.format(p_id))
#                     pass
#                 else:
#                     #print('No production parameter called {} - setting it to 1'.format(p_id))
#                     v[p_id] = 1.0
# 
#             for p_id in self.modelInstance.allocation_params.keys():
#                 if v.get(p_id):
#                     #print('{} already exists'.format(p_id))
#                     pass
#                 else:
#                     #print('No production parameter called {} - setting it to 1'.format(p_id))
#                     v[p_id] = 1.0


from lcopt.io import exchange_factory
from copy import deepcopy
from .parameters import LcoptParameterSet
#from lcopt.parameter_interpreter import ParameterInterpreter


class Bw2Exporter():
    
    def __init__(self, modelInstance):
        self.modelInstance = modelInstance

        # set up the parameter hook dictionary

# Path: lcopt/io.py
# def exchange_factory(input, type, amount, uncertainty, comment, **kwargs):
#     data_structure = {
#         'input': input,
#         'type': type,
#         'amount': amount,
#         'uncertainty type': uncertainty,
#         'comment': comment,
#     }
# 
#     for kw in kwargs:
#         data_structure[kw] = kwargs[kw]
#         
#     return data_structure
# 
# Path: lcopt/parameters.py
# class LcoptParameterSet(ParameterSet):
#     """
#     Subclass of `bw2parameters.parameter_set.ParameterSet` that takes a `lcopt.LcoptModel` and delegates parameter ordering and evaluation to `bw2parameters`
#     
#     TODO: Add more documentation and write tests
#     """
#     
#     def __init__(self, modelInstance):
#         
#         self.modelInstance = modelInstance
#         
#         self.norm_params = self.normalise_parameters()
# 
#         self.check_production_parameters_exist()
#         
#         self.all_params = {**self.modelInstance.params, **self.modelInstance.production_params, **self.norm_params, **self.modelInstance.allocation_params}
#         
#         self.bw2_params, self.bw2_global_params, self.bw2_export_params = self.lcopt_to_bw2_params(0)
#         
#         
#         super().__init__(self.bw2_params, self.bw2_global_params)
#         
#         self.evaluated_parameter_sets = self.preevaluate_exchange_params()
#         
#     def lcopt_to_bw2_params(self, ps_key):
#         
#         k0 = list(self.modelInstance.parameter_sets.keys())[ps_key]
#         ps1 = self.modelInstance.parameter_sets[k0]
#         
#         bw2_params = {k:{(x if x != 'function' else 'formula'):y for x, y in v.items()} for k,v in self.all_params.items()}
#         
#         for k in bw2_params.keys():
#             bw2_params[k]['amount'] = ps1.get(k,0)
#             
#         bw2_global_params = {x['name']: ps1.get(x['name'],x['default']) for x in self.modelInstance.ext_params}
# 
#         bw2_export_params = []
# 
#         for k, v in bw2_params.items():
#             to_append = {'name': k}
#             if v.get('formula'):
#                 to_append['formula'] = v['formula']
#             else:
#                 to_append['amount'] = v['amount']
#                 
#             bw2_export_params.append(to_append)
#             
#         for k, v in bw2_global_params.items():
#             bw2_export_params.append({'name':k, 'amount':v})
#         
#         return bw2_params, bw2_global_params, bw2_export_params
#     
#     def normalise_parameters(self):
#         
#         param_copy = deepcopy(self.modelInstance.params)
#         #production_params = deepcopy(self.modelInstance.production_params)
#         #allocation_params = deepcopy(self.modelInstance.allocation_params)
#         norm_params = OrderedDict()
#         for k, v in param_copy.items():
#             norm_params['n_{}'.format(k)] = {}
#             for key, item in v.items():
#                 if key == 'function':
#                     if not item:
#                         norm_function = '{} / {}'.format(k, v['normalisation_parameter'])
#                     else:
#                         norm_function = '({}) / {}'.format(item, v['normalisation_parameter'])
#                     
#                     norm_params['n_{}'.format(k)][key] = norm_function
#                 else:
#                     norm_params['n_{}'.format(k)][key] = item
#         return norm_params
#     
#     def preevaluate_exchange_params(self):
#         
#         evaluated_params = OrderedDict()
#         
#         for n, k in enumerate(self.modelInstance.parameter_sets.keys()):
#             
#             self.params, self.global_params, _ = self.lcopt_to_bw2_params(n)
#             self.evaluate_and_set_amount_field()
#             
#             this_set = {}
#             
#             for j, v in self.params.items():
#                 this_set[j] = v['amount']
#                 
#             evaluated_params[k] = this_set
#                 
#         self.params, self.global_params , _ = self.lcopt_to_bw2_params(0)
#         self.evaluate_and_set_amount_field()
#         
#             
#             
#         return evaluated_params
# 
#     def check_production_parameters_exist(self):
#         """ old versions of models won't have produciton parameters, leading to ZeroDivision errors and breaking things"""
#         for k, v in self.modelInstance.parameter_sets.items():
#             for p_id in self.modelInstance.production_params.keys():
#                 if v.get(p_id):
#                     #print('{} already exists'.format(p_id))
#                     pass
#                 else:
#                     #print('No production parameter called {} - setting it to 1'.format(p_id))
#                     v[p_id] = 1.0
# 
#             for p_id in self.modelInstance.allocation_params.keys():
#                 if v.get(p_id):
#                     #print('{} already exists'.format(p_id))
#                     pass
#                 else:
#                     #print('No production parameter called {} - setting it to 1'.format(p_id))
#                     v[p_id] = 1.0


# Path: lcopt/bw2_export.py
from lcopt.io import exchange_factory
from copy import deepcopy
from .parameters import LcoptParameterSet

#from lcopt.parameter_interpreter import ParameterInterpreter


class Bw2Exporter():
    
    def __init__(self, modelInstance):
        self.modelInstance = modelInstance

        # set up the parameter hook dictionary

        self.evaluate_parameter_sets()