import safenet.api
import requests
import json
from StringIO import StringIO
import base64

__author__ = "William Clyde"
__copyright__ = "Copyright 2016, William Clyde"
__license__ = "MIT"


class Directory:
    """Directory management"""
    def __init__(self):
        """__init__"""

        self.headers = {'content-type': 'application/json',
                        'authorization':
                        'Bearer {0}'.format(safenet.api.getToken())}

    def create(self, path, meta_data, is_private=True):
        """Create a new directory

        Parameters
        ----------

        :param path: string
            path of new directory
        :param meta_data: string
            optional directory information
        :param is_private: bool
            marks the file as private

        Returns
        -------

        bool
            True if successful, False otherwise
        """
        privacy = "true" if is_private else "false"
        data = """{{  "isPrivate": {privacy}, "metadata": "{meta_data}" }}"""
        response = requests.post(safenet.api.DRIVE_DIR_URL + path,
                                 headers=self.headers,
                                 data=data.format(privacy, meta_data=base64.b64encode(meta_data)))
        if response.status_code == 200:
            return True
        return False

    def get(self, path):
        """Get directory at path

        Parameters
        ----------

        :param path: string
            path to directory
        """
        response = requests.get(safenet.api.DRIVE_DIR_URL + path, headers=self.headers)
        if response.status_code == 200:
            return json.load(StringIO(response.text))
        else:
            return response.reason

    def update(self, path, new_name, meta_data):
        """Update the name of the directory

        Parameters
        ----------

        :param path: string
            path to directory
        :param new_name: string
            updated directory name
        :param meta_data: string
            optional directory information

        Returns
        -------

        bool
        True if successful, otherwise False
        """
        data = """{{ "name":"{new_name}", "metadata":"{meta_data}" }}"""
        response = requests.put(safenet.api.DRIVE_DIR_URL + path,
                                data=data.format(new_name=new_name,
                                                 meta_data=base64.b64encode(meta_data)),
                                headers=self.headers)
        if response.status_code == 200:
            return True
        return False

    def move(self, src_path, dest_path, copy=False):
        """Move directory to new location with optional copy

        Parameters
        ----------

        :param src_path: string
            current path to directory
        :param dest_path: string
            new path to directory
        :param copy: bool
            copy file instead of moving

        Returns
        -------

        bool
        True if successful, otherwise False
        """
        action = "copy" if copy else "move"
        data = """{{ "srcRootPath":"drive",
                     "srcPath":"{src_path}",
                     "destRootPath":"drive",
                     "destPath":"{dest_path}",
                     "action":"{action}" }} """.format(src_path=src_path,
                                                       dest_path=dest_path,
                                                       action=action)
        response = requests.post(safenet.api.DIR_URL + "/movedir",
                                 data=data,
                                 headers=self.headers)
        if response.status_code == 200:
            return True
        return False

    def delete(self, path):
        """delete

        Parameters
        ----------

        :param path: string
            path of the directory to delete

        Returns
        -------

        bool
            True if successful, otherwise False
        """
        response = requests.delete(safenet.api.DRIVE_DIR_URL + path, headers=self.headers)
        if response.status_code == 200:
            return True
        return False
