'''

client.py: simple clients for som api

Copyright (c) 2017 Vanessa Sochat

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''

from som.api.base.connect import (
    api_base,
    api_version,
    SomApiConnection
)

from som.api.base.standards import spec
from som.logman import bot

class ClientBase(object):


    def __init__(self, spec=None, token=None, base=None, version=None):
 
        # Base and version consistent across
        if base == None:
            base = api_base

        if version == None:
            version = api_version

        self.base = base
        self.version = version
        self.study = None

        if spec != None:
            self.spec = spec

        self.api = SomApiConnection(token)


    def get_base(self,study=None):
        '''get_base returns the base api for a particular study
        '''
        if study is None:
            study = self.study
        return "%s/%s/%s" %(self.base,self.version,study)


    def deidentifiy_update(self,identifiers,test=False):
        '''deidentify update calls deidentify, but setting save_records
        to True to ensure that data is updated/written.
        '''
        return self.deidentify(identifiers,test=test,save_records=True)


    def deidentify(self,ids,test=False,save_records=False):
        '''deidentify will take a list of identifiers, and return the deidentified.
        if save_records is True, will save records. If False (default) only
        returns identifiers.
        :param identifiers: a list of identifiers (e.g., people)
        '''    
        # Default is production endpoint particular to the study
        study = self.study

        # Saving records (uid) or not (mrn) changes the endpoint
        action = "mrn"
        if save_records == True:
            bot.logger.debug("save_records is %s, no new data will be saved.")
            action = "uid"
        else:
            bot.logger.debug("save_records is %s, data will be saved.")

        # Testing overrides all other specifications
        if test == True:
            study = 'test'

        url = "%s/%s/%s/%s" %(api_base,api_version,action,study)

        response = self.api.post(url,data=ids)
        if "results" in response:
            return response['results']
        return response
