"""
Simple API client for the Slideform API
"""
import requests

url = 'https://rest.slideform.co'

def create_auth_header(api_key):
    """
    Create a simple authorization header.
    """
    return {
        'x-api-key': api_key
    }

def get_projects(api_key):
    """
    Return a list of projects.
    Each project consists of a name and ID.

    Here is an example of a return value:
    {
      'projects': [
        {
            'id': 'a3eec859c45947e5bc8e1cf2b9012039',
            'name': 'Four Pragmas (Test)'
        },
        {
            'id': '23bca5a6f95e46d3ba32d28a05da192d',
            'name': 'Chart Examples'
        }
      ]
    }
    """

    res = requests.get(
        url + '/projects',
        headers=create_auth_header(api_key)
    )

    return res.json()

def submit_job(api_key, form_values):
    """
    Submit a form to create a new presentation.

    The return value will consist of a JSON response containing
    the file name used to later retrieve the file.
    """
    res = requests.post(
        url + '/job',
        headers=create_auth_header(api_key),
        json=form_values
    )

    json_reply = res.json()
    if json_reply['status'] != 'failed':
        return json_reply['id']
    else:
        return None

def bulk_submit(api_key, form_values):
    """
    Bulk submit the forms.
    """
    res = requests.post(
        url + '/bulk',
        headers=create_auth_header(api_key),
        json=form_values
    )

    json_reply = res.json()
    if json_reply['status'] != 'failed':
        return json_reply['file_name']
    else:
        return None

def get_job_status(
        api_key,
        job_id
):
    """
    Retrieve the output results and save it to a destination.
    """
    have_results = False
    while(True):
        res = requests.get(
            url + '/status/' + job_id,
            headers=create_auth_header(api_key)
        )
        try:
            job = res.json()
            if job['status'] == 'submitted':
                time.sleep(3.00)
            elif job['status'] == 'failed':
                return False
            else:
                have_results = True
                break
        except Exception as e:
            print(e)

    if have_results:
        return job

    return None

def upload_image(
        api_key,
        file_path
):
    """
    Upload a new image so that we can embed it in a presentation later.
    """
    files = {
        'file': open(file_path,'rb')
    }

    res = requests.post(
        url + '/files',
        headers=create_auth_header(api_key),
        files=files
    )

    return res.json()

def add_pragma_to_job(
        form,
        pragma,
        value,
        type
):
    """
    Help construct valid form input.
    """
    if 'pragmas' not in form:
        form['pragmas'] = {}

    if 'options' not in form:
        form['options'] = {}

    form['pragmas'][pragma] = value
    form['options'][pragma] = {}
    if type == 'text':
        form['options'][pragma] = {
            'type': 'text'
        }
    elif type == 'image':
        form['options'][pragma] = {
            'type': 'image',
            'crop': False,
            'transparent': False,
        }

    return form
