# -*- coding: utf-8 -*-
"""
Tools for interacting with Ed API.

..codeauthor Grzegorz Pawełczuk <grzegorz.pawelczuk@nftlearning.com>
"""
import logging
from typing import Optional, Any, List, Dict

from nftl_ed_lms_tools.ed_api import EdApi, CallType


class Users(EdApi):
    """
    Ed API wrapper that is handling users category
    """
    API_VERSION = "v1"

    def __init__(self, token: str, api_url: str = None) -> None:
        super().__init__(token, api_url)

    def get_users(self, token=None):
        # type: (Optional[str]) -> Optional[List[Any]]
        """
        Get a list of users.
        http://developer.edapp.com/#users_get
        Args:
            token: optional auth token that will overwrite EdApi token
        Returns:
            None if data is invalid or users info as :py:class:`typing.List`
            when success
        """
        return super()._call(
            method='users',
            token=token,
            call_type=CallType.GET
        )

    def create_or_update_user(self, user_data, token=None):
        # type: (Dict[str,Any], Optional[str]) -> Optional[Dict[str,Any]]
        """
        Create or update a user.
        http://developer.edapp.com/#users_post

        Args:
            user_data: user data to create or update
            token: optional auth token that will overwrite EdApi token
        Returns:
            None if data is invalid or users info as :py:class:`typing.List`
            when success
        """
        if 'externalIdentifier' not in user_data:
            msg = '"username" or "email" not provided in user data!'
            logging.error(msg)
            raise AttributeError(msg)

        return super()._call(
            method='users',
            data=user_data,
            token=token,
            call_type=CallType.POST
        )

    def get_external_token(self, external_id, token=None):
        # type: (str, Optional[str]) -> Optional[Dict[str,Any]]
        """
        Get users authentication token by externalId.
        http://developer.edapp.com/#users_external__id__token_get

        Args:
            external_id: users external identifier
            token: optional auth token that will overwrite EdApi token
        Returns:
            None if data is invalid or users info as :py:class:`typing.List`
            when success
        """
        return super()._call(
            method=f'users/external/{external_id}/token',
            token=token,
            call_type=CallType.GET
        )

    def get_token(self, user_id, token=None):
        # type: (str, Optional[str]) -> Optional[Dict[str,Any]]
        """
        Get users authentication token by id.
        http://developer.edapp.com/#users__id__token_get

        Args:
            user_id: users external identifier
            token: optional auth token that will overwrite EdApi token
        Returns:
            None if data is invalid or users info as :py:class:`typing.List`
            when success
        """
        return super()._call(
            method=f'users/id/{user_id}/token',
            token=token,
            call_type=CallType.GET
        )
