# Copyright 2016 Allen Institute for Brain Science
# This file is part of Allen SDK.
#
# Allen SDK 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, version 3 of the License.
#
# Allen SDK 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 Allen SDK.  If not, see <http://www.gnu.org/licenses/>.

from allensdk.api.queries.rma_template import RmaTemplate

class BiophysicalModuleApi(RmaTemplate):
    ''''''
    
    rma_templates = \
        {"biophysical_lims_queries": [
            {'name': 'neuronal_model_runs_by_ids',
             'description': 'see name',
             'model': 'NeuronalModelRun',
             'criteria': '[id$in{{ neuronal_model_run_ids }}]',
             'include': 'well_known_files(well_known_file_type),'
                        'neuronal_model(well_known_files(well_known_file_type),'
                            'specimen(project,specimen_tags,'
                                'ephys_roi_result'
                                '(ephys_qc_criteria,'
                                'well_known_files(well_known_file_type)),'
                                'neuron_reconstructions'
                                '(well_known_files(well_known_file_type)),'
                                'ephys_sweeps'
                                '(ephys_sweep_tags,'
                                'ephys_stimulus(ephys_stimulus_type))),'
                                'neuronal_model_template'
                                '(neuronal_model_template_type,'
                                'well_known_files(well_known_file_type)))',
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['neuronal_model_run_ids']
            },
            {'name': 'neuronal_models_by_ids',
             'description': 'see name',
             'model': 'NeuronalModel',
             'criteria': '[id$in{{ neuronal_model_ids }}]',
             'include': 'well_known_files(well_known_file_type),'
                        'specimen(project,specimen_tags,'
                            'ephys_roi_result'
                            '(ephys_qc_criteria,'
                            'well_known_files(well_known_file_type)),'
                            'neuron_reconstructions'
                            '(well_known_files(well_known_file_type)),'
                            'ephys_sweeps'
                            '(ephys_sweep_tags,'
                            'ephys_stimulus(ephys_stimulus_type))),'
                            'neuronal_model_template'
                            '(neuronal_model_template_type,'
                            'well_known_files(well_known_file_type))',
             'num_rows': 'all',
             'count': False,
             'criteria_params': ['neuronal_model_ids']
            }                                      
        ]}
    
    
    def __init__(self, base_uri=None):
        super(BiophysicalModuleApi, self).__init__(base_uri,
                                                   query_manifest=BiophysicalModuleApi.rma_templates)
    
    
    def get_neuronal_model_runs(self, neuronal_model_run_ids=None):
        '''List Neuronal Model Rusn available through LIMS
        with associated info needed to run in NEURON.
        
        Parameters
        ----------
        neuronal_model_run_ids : integer or list of integers, optional
            only select specific neuronal_model_runs.
        
        Returns
        -------
        dict : neuronal model run metadata        
        '''
        data = self.template_query('biophysical_lims_queries',
                                   'neuronal_model_runs_by_ids',
                                   neuronal_model_run_ids=neuronal_model_run_ids)
        
        return data
    
    
    def get_neuronal_models(self, neuronal_model_ids=None):
        '''List Neuronal Models available through LIMS
        with associated info needed to run in NEURON.
        
        Parameters
        ----------
        neuronal_model_ids : integer or list of integers, optional
            only select specific neuronal_models.
        
        Returns
        -------
        dict : neuronal model metadata        
        '''
        data = self.template_query('biophysical_lims_queries',
                                   'neuronal_models_by_ids',
                                   neuronal_model_ids=neuronal_model_ids)
        
        return data    