from allauth.socialaccount import providers
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
from django.conf import settings


class OSFAccount(ProviderAccount):

    def to_str(self):
        # default ... reserved word?
        dflt = super(OSFAccount, self).to_str()
        return next(
            value
            for value in (
                # try the name first, then the id, then the super value
                '{} {}'.format(
                    self.account.extra_data.get('first_name', None),
                    self.account.extra_data.get('last_name', None)
                ),
                self.account.extra_data.get('id', None),
                dflt
            )
            if value is not None
        )


class OSFProvider(OAuth2Provider):
    id = 'osf'
    name = 'Open Science Framework'
    account_class = OSFAccount

    def extract_common_fields(self, data):
        attributes = data.get('data').get('attributes')
        return dict(
            # we could put more fields here later the api has much
            # more available, just not sure how much we need right now
            username=self.extract_uid(data),
            first_name=attributes.get('given_name'),
            last_name=attributes.get('family_name'),
        )

    def extract_uid(self, data):
        return str(data.get('data').get('id'))

    def get_default_scope(self):
        return settings.DEFAULT_SCOPES

providers.registry.register(OSFProvider)
