# -*- coding: utf-8 -*-

import markupsafe
from modularodm import fields

from framework.auth.decorators import Auth
from framework.exceptions import HTTPError

from website.addons.base import exceptions
from website.addons.base import AddonOAuthUserSettingsBase, AddonOAuthNodeSettingsBase
from website.addons.base import StorageAddonBase
from website.oauth.models import ExternalProvider

from addons.figshare import messages
from addons.figshare.client import FigshareClient
from addons.figshare import settings
from addons.figshare.serializer import FigshareSerializer

class Figshare(ExternalProvider):
    name = 'figshare'
    short_name = 'figshare'

    client_id = settings.CLIENT_ID
    client_secret = settings.CLIENT_SECRET

    auth_url_base = settings.FIGSHARE_OAUTH_AUTH_ENDPOINT
    callback_url = settings.FIGSHARE_OAUTH_TOKEN_ENDPOINT
    auto_refresh_url = callback_url
    # refresh_time = settings.REFRESH_TIME  # TODO: maybe
    # expiry_time = settings.EXPIRY_TIME
    default_scopes = ['all']

    def handle_callback(self, response):
        """View called when the Oauth flow is completed. Adds a new BoxUserSettings
        record to the user and saves the user's access token and account info.
        """
        client = FigshareClient(response['access_token'])
        about = client.userinfo()

        return {
            'provider_id': about['id'],
            'display_name': '{} {}'.format(about['first_name'], about.get('last_name')),
        }


class FigshareUserSettings(AddonOAuthUserSettingsBase):
    oauth_provider = Figshare
    serializer = FigshareSerializer


class FigshareNodeSettings(StorageAddonBase, AddonOAuthNodeSettingsBase):
    oauth_provider = Figshare
    serializer = FigshareSerializer