import logging
import cdsapi


class ApiRequest:
    def __init__(self):
        self.client = cdsapi.Client()
        self.max_time_delay = 120

    def get_reply(self, current_param):
        try:
            param = {'format': 'zip', 'temporal_resolution': 'monthly', 'experiment': current_param['experiment'],
                     'variable': current_param['variable'], 'model': current_param['model'], 'year': [],
                     'month': ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']}
            if current_param['level'] == 'all':
                param['level'] = ['1', '5', '10', '20', '30', '50', '70', '100', '150', '200', '250', '300', '400',
                                  '500', '600', '700', '850', '925', '1000']
            if current_param['experiment'] == 'historical':
                param['year'] = [str(y) for y in range(1850, 2015)]
            else:
                param['year'] = [str(y) for y in range(2015, 2024)]
            if current_param['model'] == "miroc_es2h":
                param['year'] = ["1850"]
            result = self.client._api("%s/resources/%s" % (self.client.url, 'projections-cmip6'), param, "POST")
            return result.reply
        except Exception as e:
            print(e)
            return None
