from pydactyl.api import base


class Account(base.PterodactylAPI):
    """Class for interacting with the Pterodactyl Client API.

    Methods in this class appear in the base **client.account** namespace
    when using PterodactylClient.
    """

    def get_account(self):
        """List details of the account belonging to the client API key."""
        endpoint = 'client/account'
        response = self._api_request(endpoint=endpoint)
        return response

    def get_2fa_setup_code(self):
        """Generates a TOTP QR code image to allow the setup of 2FA."""
        endpoint = 'client/account/two-factor'
        response = self._api_request(endpoint=endpoint)
        return response

    def enable_2fa(self, code: str):
        """Enables TOTP 2FA.

        Takes a TOTP code generated by a client configured using the code
        from get_2fa_setup_code().  This does not take the code directly and
        you must first setup an authenticator app such as Google
        Authenticator to generate the code provided to this method.

        Args:
            code(str): TOTP code generated by authenticator app
        """
        data = {'code': code}
        endpoint = 'client/account/two-factor'
        response = self._api_request(endpoint=endpoint, mode='POST', data=data)
        return response

    def disable_2fa(self, password: str):
        """Disables 2FA on the account.

        Args:
            password(str): User's account password
        """
        data = {'password': password}
        endpoint = 'client/account/two-factor'
        response = self._api_request(endpoint=endpoint, mode='DELETE',
                                     data=data)
        return response

    def update_email(self, email: str, password: str):
        """Updates the email address of the account.

        Args:
            email(str): New email address
            password(str): User's account password
        """
        data = {'email': email, 'password': password}
        endpoint = 'client/account/email'
        response = self._api_request(endpoint=endpoint, mode='PUT',
                                     data=data)
        return response

    def update_password(self, current: str, new: str, new_confirm: str):
        """Updates the password of the account.

        Args:
            current(str): User's current password
            new(str): New password for the account, must match new_confirm
            new_confirm(str): New password for the account, must match new
        """
        data = {'current_password': current, 'password': new,
                'password_confirmation': new_confirm}
        endpoint = 'client/account/password'
        response = self._api_request(endpoint=endpoint, mode='PUT',
                                     data=data)
        return response

    def api_key_list(self):
        """List client's API keys."""
        endpoint = 'client/account/api-keys'
        response = self._api_request(endpoint=endpoint)
        return base.parse_response(response, detail=False)

    def api_key_create(self, description: str, allowed_ips: list):
        """Create a client API key.

        Args:
            description(str): Note for the API key
            allowed_ips(iter): List of allowed IPs
        """
        endpoint = 'client/account/api-keys'
        data = {'description': description, 'allowed_ips': allowed_ips}
        response = self._api_request(endpoint=endpoint, mode='POST', data=data)
        return response

    def api_key_delete(self, identifier):
        """Delete a client API key.

        Args:
            identifier(str): API key identifier
        """
        endpoint = 'client/account/api-keys/{}'.format(identifier)
        response = self._api_request(endpoint=endpoint, mode='DELETE')
        return response
