#!/usr/bin/env python3

class PCloudException(Exception):
    """Exception raised by the API.

    :ivar msg: message of the exception, auto populated from ERROR_CODES
    :ivar result_code: result int returned by the API, can be None
    """

    ERROR_CODES = {
        # see https://github.com/pcloudfs/pclouddoc/blob/master/errors.txt
        1000: "Log in required.",
        1001: "No full path or name/folderid provided.",
        1002: "No full path or folderid provided.",
        1003: "Neither csr or publickey is provided. Please create Certificate Signing Request and pass it as 'csr' parameter or send your 'publickey'.",
        1004: "No fileid or path provided.",
        1005: "Unknown content-type requested.",
        1006: "Please provide flags.",
        1007: "Invalid or closed file descriptor.",
        1008: "Please provide lock 'type'.",
        1009: "Please provide 'offset'.",
        1010: "Please provide 'length'.",
        1011: "Please provide 'count'.",
        1012: "Invalid lock type. Please provide type (supported values: 0, 1, 2).",
        1013: "Date/time format not understood.",
        1014: "Thumb can not be created from this file type.",
        1015: "Please provide valid thumb size. Width and height must be divisible either by 4 or 5 and must be between 16 and 2048 (1024 for height).",
        1016: "No full topath or toname/tofolderid provided.",
        1017: "Invalid 'folderid' provided.",
        1018: "Invalid 'fileid' provided.",
        1019: "Please provide 'sha1' or 'md5' checksum",
        1020: "Please provide language.",
        1021: "Language not supported.",
        1022: "Please provide 'code'.",
        1023: "Please provide 'mail' to share folder with.",
        1024: "Please provide 'permissions' for the share.",
        1025: "Please provide 'sharerequestid' or 'code' to accept a share.",
        1026: "Please provide 'sharerequestid'.",
        1027: "Please provide 'shareid'.",
        1028: "Please provide link 'code'.",
        1029: "Please provide 'fileid'.",
        1030: "Please provide 'linkid'.",
        1031: "Please provide 'oldpassword'.",
        1032: "Please provide 'newpassword'.",
        1033: "Please provide 'mail'.",
        1034: "Please provide 'password'.",
        1035: "Please provide 'comment'.",
        1036: "Please provide 'uploadlinkid'.",
        1037: "Please provide at least one of 'topath', 'tofolderid' or 'toname'.",
        1038: "Please provide 'fileids'.",
        1039: "Please provide 'name'.",
        1040: "Please provide 'url'.",
        1041: "Please provide 'message'.",
        1042: "Please provide 'reason'.",
        1900: "Upload not found",
        2000: "Log in failed.",
        2001: "Invalid file/folder name.",
        2002: "A component of parent directory does not exist.",
        2003: "Access denied. You do not have permissions to preform this operation.",
        2004: "File or folder already exists.",
        2005: "Directory does not exist.",
        2006: "Folder is not empty.",
        2007: "Cannot delete the root folder.",
        2008: "User is over quota.",
        2009: "File not found.",
        2010: "Invalid path.",
        2011: "Requested speed limit too low, see minspeed for minimum.",
        2012: "Invalid 'code' provided.",
        2013: "Email is already verified.",
        2014: "Please verify your email address to preform this action.",
        2015: "Can not share root folder.",
        2016: "You can only share your own folders.",
        2017: "User does not accept shares.",
        2018: "Invalid 'mail' provided.",
        2019: "Share request already exists.",
        2020: "You can't share a folder with yourself.",
        2021: "Non existing share request. It might be already accepted or cancelled by the sending user.",
        2022: "Wrong user to accept the share.",
        2023: "You are trying to place shared folder into another shared folder.",
        2024: "User already has access to this folder or subfolder of this folder.",
        2025: "Invalid shareid.",
        2026: "You can only share your own files or folders. Copy the file to a folder you own if you need to share it.",
        2027: "Invalid or already deleted link.",
        2028: "There are active shares or sharerequests for this folder.",
        2029: "Revision with provided 'revisionid' not found.",
        2030: "New password is the same as the old one.",
        2031: "Wrong 'oldpassword' provided.",
        2032: "Password too short. Minimum length is 6 characters.",
        2033: "Password can not start or end with space.",
        2034: "Password does not contain enough different characters. The minimum is 4.",
        2035: "Password can not contain only consecutive characters.",
        2036: "Verification 'code' expired. Please request password reset again.",
        2037: "You need to accept Terms of Service and all other agreements to register.",
        2038: "User with this email is already registered.",
        2039: "You have to own the folder for upload.",
        2040: "Given 'uploadlinkid' not found.",
        2041: "Connection broken.",
        2042: "Cannot rename the root folder.",
        2043: "Cannot move a folder to a subfolder of itself.",
        2044: "Video links can only be generated for videos.",
        3000: "SSL error occurred. Check sslerror for more information.",
        3001: "Could not create thumb from the given file.",
        3002: "Connection to iTunes failed.",
        3003: "iTunes error.",
        4000: "Too many login tries from this IP address.",
        5000: "Internal error. Try again later.",
        5001: "Internal upload error.",
        5002: "Internal error, no servers available. Try again later.",
        5003: "Write error. Try reopening the file.",
        5004: "Read error. Try reopening the file.",
        6000: "Not modified",
        7001: "Invalid link 'code'.",
        7002: "This link is deleted by the owner.",
        7003: "This link is deleted because of copyright complaint.",
        7004: "This link has expired.",
        7005: "This link has reached its traffic limit.",
        7006: "This link has reached maximum downloads.",
        7007: "This link has reached its space limit.",
        7008: "This link has reached its file limit.",
        1000: "Log in required.",
        2000: "Log in failed.",
        2002: "A component of parent directory does not exist.",
        2004: "File or folder already exists.",
        2009: "File not found.",
    }

    def __init__(self, msg=None, result_code=None, **kwargs):
        """
        :param msg: message, initialize by .ERROR_CODES[result_code]
        :param result_code: int, pcloud api result, can be None
        """
        if msg is None:
            msg = "API Returned result: %s (%s)" % (
                    result_code,
                    PCloudException.ERROR_CODES.get(result_code,
                            "see https://docs.pcloud.com/errors/index.html")
                )
        super().__init__(msg, **kwargs)
        self.msg = msg
        self.result_code = result_code

