import json
import requests

# TODO: Complete methods.


class AttributeType():
    """Attribute Type Client."""

    # Service Setup
    config = {
        'schema': 'http',
        'host': 'localhost',
        'port': '9903',
        'endpoint': 'api/v1/attribute_types'
    }

    @classmethod
    def base_url(cls):
        """Form the base url for the service."""
        return "{schema}://{host}:{port}/{endpoint}".format(**cls.config)

    @classmethod
    def configure(cls, options={}):
        cls.config.update(options)

    @classmethod
    def get_all(cls):
        """Return all attribute types."""
        r = requests.get(cls.base_url())
        if r.status_code == 200:
            return r.json()
        else:
            return None

    @classmethod
    def get(cls, code):
        """Return an attribute type."""
        r = requests.get(cls.base_url() + '/' + code)
        if r.status_code == 200:
            return r.json()
        else:
            return None

    @classmethod
    def create(cls, attrs):
        """Create an attribute type with the attributes
        passed in attrs dict."""
        r = requests.post(cls.base_url(), data=json.dumps(attrs))
        if r.status_code == 200:
            return r.json()
        else:
            return None

    @classmethod
    def update(cls, code, attrs):
        """Update the attribute type identified by code with attrs dict."""
        r = requests.put(cls.base_url() + '/' + code, data=json.dumps(attrs))
        if r.status_code == 200:
            return r.json()
        else:
            return None

    @classmethod
    def delete(cls, code):
        """Delete the attribute type identified by code."""
        r = requests.delete(cls.base_url() + '/' + code)
        return r.status_code == 204

    @classmethod
    def delete_all(cls):
        """Delete all attribute types (for all kind of resources)."""
        r = requests.delete(cls.base_url())
        return r.status_code == 204

    @classmethod
    def bulk_load(cls, json_string):
        """Bulk loads an array of attribute types."""
        h = {
            'Content-Type': 'application/json'
        }
        return requests.post(cls.base_url(), data=json_string, headers=h)


class Attribute():
    """Attribute Client."""

    # Service Setup
    config = {
        'schema': 'http',
        'host': 'localhost',
        'port': '9903',
        'endpoint': 'api/v1/attributes'
    }

    @classmethod
    def base_url(cls):
        """Form the base url for the service."""
        return "{schema}://{host}:{port}/{endpoint}".format(**cls.config)

    @classmethod
    def configure(cls, options={}):
        cls.config.update(options)

    @classmethod
    def get_all(cls):
        """Return all attributes defined."""
        r = requests.get(cls.base_url())
        if r.status_code == 200:
            return r.json()
        else:
            return None

    @classmethod
    def get_all_for(cls, resource_type):
        """Return all attribute for the resource type."""
        r = requests.get(cls.base_url() + '/for/' + resource_type)
        if r.status_code == 200:
            return r.json()
        else:
            return None

    # @classmethod
    # def create(cls, attrs):
    #   """Create an classroom with the attributes passed in attrs dict."""
    #   r = requests.post(cls.base_url(), data=json.dumps(attrs))
    #   if r.status_code == 200:
    #     return r.json()
    #   else:
    #     return None

    # @classmethod
    # def delete(cls, code):
    #   """Delete the classroom identified by code."""
    #   r = requests.delete(cls.base_url() + '/' + code)
    #   return r.status_code == 204

    @classmethod
    def delete_all(cls):
        """Delete all instructors."""
        r = requests.delete(cls.base_url())
        return r.status_code == 204

    @classmethod
    def bulk_load(cls, json_string):
        """Bulk loads an array of attributes."""
        h = {
            'Content-Type': 'application/json'
        }
        return requests.post(cls.base_url(), data=json_string, headers=h)
