from social.strategies.django_strategy import DjangoStrategy


class DSAStrategy(DjangoStrategy):
    settings_map = {
        'AMAZON_SECRET': 'AMAZON_API_SECRET',
        'AMAZON_KEY': 'AMAZON_APP_ID',
        'AMAZON_SCOPE': 'AMAZON_EXTENDED_PERMISSIONS',
        'ANGEL_KEY': 'ANGEL_CLIENT_ID',
        'ANGEL_SECRET': 'ANGEL_CLIENT_SECRET',
        'APPSFUEL_KEY': 'APPSFUEL_CLIENT_ID',
        'APPSFUEL_SECRET': 'APPSFUEL_CLIENT_SECRET',
        'BEHANCE_KEY': 'BEHANCE_CLIENT_ID',
        'BEHANCE_SECRET': 'BEHANCE_CLIENT_SECRET',
        'BEHANCE_SCOPE': 'BEHANCE_EXTENDED_PERMISSIONS',
        'BITBUCKET_KEY': 'BITBUCKET_CONSUMER_KEY',
        'BITBUCKET_SECRET': 'BITBUCKET_CONSUMER_SECRET',
        'DAILYMOTION_KEY': 'DAILYMOTION_OAUTH2_KEY',
        'DAILYMOTION_SECRET': 'DAILYMOTION_OAUTH2_SECRET',
        'DISQUS_KEY': 'DISQUS_CLIENT_ID',
        'DISQUS_SECRET': 'DISQUS_CLIENT_SECRET',
        'DOUBAN_OAUTH2_KEY': 'DOUBAN2_CONSUMER_KEY',
        'DOUBAN_OAUTH2_SECRET': 'DOUBAN2_CONSUMER_SECRET',
        'DOUBAN_KEY': 'DOUBAN_CONSUMER_KEY',
        'DOUBAN_SECRET': 'DOUBAN_CONSUMER_SECRET',
        'DROPBOX_KEY': 'DROPBOX_API_SECRET',
        'DROPBOX_SECRET': 'DROPBOX_APP_ID',
        'EVERNOTE_KEY': 'EVERNOTE_CONSUMER_KEY',
        'EVERNOTE_SECRET': 'EVERNOTE_CONSUMER_SECRET',
        'EXACTTARGET_SECRET': 'EXACTTARGET_APP_SIGNATURE',
        'EXACTTARGET_KEY': 'EXACTTARGET_UNUSED',
        'FACEBOOK_KEY': 'FACEBOOK_APP_ID',
        'FACEBOOK_SECRET': 'FACEBOOK_API_SECRET',
        'FACEBOOK_SCOPE': 'FACEBOOK_EXTENDED_PERMISSIONS',
        'FACEBOOK_APP_KEY': 'FACEBOOK_APP_ID',
        'FACEBOOK_APP_LOCAL_HTML': 'FACEBOOK_LOCAL_HTML',
        'FITBIT_KEY': 'FITBIT_CONSUMER_KEY',
        'FITBIT_SECRET': 'FITBIT_CONSUMER_SECRET',
        'FLICKR_SECRET': 'FLICKR_API_SECRET',
        'FLICKR_KEY': 'FLICKR_APP_ID',
        'FOURSQUARE_KEY': 'FOURSQUARE_CONSUMER_KEY',
        'FOURSQUARE_SECRET': 'FOURSQUARE_CONSUMER_SECRET',
        'GITHUB_SECRET': 'GITHUB_API_SECRET',
        'GITHUB_KEY': 'GITHUB_APP_ID',
        'GITHUB_SCOPE': 'GITHUB_EXTENDED_PERMISSIONS',
        'GOOGLE_OAUTH_KEY': 'GOOGLE_CONSUMER_KEY',
        'GOOGLE_OAUTH_SECRET': 'GOOGLE_CONSUMER_SECRET',
        'GOOGLE_OAUTH_SCOPE': 'GOOGLE_OAUTH_EXTRA_SCOPE',
        'GOOGLE_OAUTH2_KEY': 'GOOGLE_OAUTH2_CLIENT_KEY',
        'GOOGLE_OAUTH2_SECRET': 'GOOGLE_OAUTH2_CLIENT_SECRET',
        'GOOGLE_OAUTH2_SCOPE': 'GOOGLE_OAUTH_EXTRA_SCOPE',
        'INSTAGRAM_KEY': 'INSTAGRAM_CLIENT_ID',
        'INSTAGRAM_SECRET': 'INSTAGRAM_CLIENT_SECRET',
        'JAWBONE_KEY': 'JAWBONE_CONSUMER_KEY',
        'JAWBONE_SECRET': 'JAWBONE_CONSUMER_SECRET',
        'JAWBONE_SCOPE': 'JAWBONE_EXTENDED_PERMISSIONS',
        'LINKEDIN_KEY': 'LINKEDIN_CONSUMER_KEY',
        'LINKEDIN_SECRET': 'LINKEDIN_CONSUMER_SECRET',
        'LINKEDIN_FIELDS_SELECTORS': 'LINKEDIN_EXTRA_FIELD_SELECTORS',
        'LINKEDIN_OAUTH2_KEY': 'LINKEDIN_CONSUMER_KEY',
        'LINKEDIN_OAUTH2_SECRET': 'LINKEDIN_CONSUMER_SECRET',
        'LINKEDIN_OAUTH2_FIELDS_SELECTORS': 'LINKEDIN_EXTRA_FIELD_SELECTORS',
        'LINKEDIN_OAUTH2_SCOPE': 'LINKEDIN_SCOPE',
        'LIVE_KEY': 'LIVE_CLIENT_ID',
        'LIVE_SECRET': 'LIVE_CLIENT_SECRET',
        'LIVE_SCOPE': 'LIVE_EXTENDED_PERMISSIONS',
        'MAILRU_OAUTH2_KEY': 'MAILRU_OAUTH2_CLIENT_KEY',
        'MAILRU_OAUTH2_SECRET': 'MAILRU_OAUTH2_CLIENT_SECRET',
        'MAILRU_OAUTH2_SCOPE': 'MAILRU_OAUTH2_EXTRA_SCOPE',
        'MENDELEY_KEY': 'MENDELEY_CONSUMER_KEY',
        'MENDELEY_SECRET': 'MENDELEY_CONSUMER_SECRET',
        'MIXCLOUD_KEY': 'MIXCLOUD_CLIENT_ID',
        'MIXCLOUD_SECRET': 'MIXCLOUD_CLIENT_SECRET',
        'ODNOKLASSNIKI_OAUTH2_KEY': 'ODNOKLASSNIKI_OAUTH2_CLIENT_KEY',
        'ODNOKLASSNIKI_OAUTH2_SECRET': 'ODNOKLASSNIKI_OAUTH2_CLIENT_SECRET',
        'ODNOKLASSNIKI_OAUTH2_SCOPE': 'ODNOKLASSNIKI_OAUTH2_EXTRA_SCOPE',
        'ORKUT_KEY': 'ORKUT_CONSUMER_KEY',
        'ORKUT_SECRET': 'ORKUT_CONSUMER_SECRET',
        'RDIO_OAUTH2_SCOPE': 'RDIO2_PERMISSIONS',
        'READABILITY_KEY': 'READABILITY_CONSUMER_KEY',
        'READABILITY_SECRET': 'READABILITY_CONSUMER_SECRET',
        'REDDIT_SECRET': 'REDDIT_API_SECRET',
        'REDDIT_KEY': 'REDDIT_APP_ID',
        'REDDIT_SCOPE': 'REDDIT_EXTENDED_PERMISSIONS',
        'SHOPIFY_KEY': 'SHOPIFY_APP_API_KEY',
        'SHOPIFY_SECRET': 'SHOPIFY_SHARED_SECRET',
        'SKYROCK_KEY': 'SKYROCK_CONSUMER_KEY',
        'SKYROCK_SECRET': 'SKYROCK_CONSUMER_SECRET',
        'SOUNDCLOUD_KEY': 'SOUNDCLOUD_CLIENT_ID',
        'SOUNDCLOUD_SECRET': 'SOUNDCLOUD_CLIENT_SECRET',
        'SOUNDCLOUD_SCOPE': 'SOUNDCLOUD_EXTENDED_PERMISSIONS',
        'STACKOVERFLOW_KEY': 'STACKOVERFLOW_CLIENT_ID',
        'STACKOVERFLOW_SECRET': 'STACKOVERFLOW_CLIENT_SECRET',
        'STACKOVERFLOW_SCOPE': 'STACKOVERFLOW_EXTENDED_PERMISSIONS',
        'STOCKTWITS_KEY': 'STOCKTWITS_CONSUMER_KEY',
        'STOCKTWITS_SECRET': 'STOCKTWITS_CONSUMER_SECRET',
        'STRIPE_KEY': 'STRIPE_APP_ID',
        'STRIPE_SECRET': 'STRIPE_APP_SECRET',
        'TRELLO_KEY': 'TRELLO_CONSUMER_KEY',
        'TRELLO_SECRET': 'TRELLO_CONSUMER_SECRET',
        'TRIPIT_KEY': 'TRIPIT_API_KEY',
        'TRIPIT_SECRET': 'TRIPIT_API_SECRET',
        'TUMBLR_KEY': 'TUMBLR_CONSUMER_KEY',
        'TUMBLR_SECRET': 'TUMBLR_CONSUMER_SECRET',
        'TWILIO_SECRET': 'TWILIO_AUTH_TOKEN',
        'TWILIO_KEY': 'TWILIO_CONNECT_KEY',
        'TWITTER_KEY': 'TWITTER_CONSUMER_KEY',
        'TWITTER_SECRET': 'TWITTER_CONSUMER_SECRET',
        'VK_APP_SECRET': 'VKAPP_API_SECRET',
        'VK_APP_KEY': 'VKAPP_APP_ID',
        'VK_APP_USERMODE': 'VKAPP_USER_MODE',
        'VK_OAUTH2_EXTRA_DATA': 'VK_EXTRA_DATA',
        'VK_OAUTH2_SCOPE': 'VK_EXTRA_SCOPE',
        'VK_OAUTH2_SECRET': 'VK_API_SECRET',
        'VK_OPENAPI_LOCAL_HTML': 'VK_LOCAL_HTML',
        'VK_OPENAPI_APP_ID': 'VK_APP_ID',
        'WEIBO_KEY': 'WEIBO_CLIENT_KEY',
        'WEIBO_SECRET': 'WEIBO_CLIENT_SECRET',
        'XING_KEY': 'XING_CONSUMER_KEY',
        'XING_SECRET': 'XING_CONSUMER_SECRET',
        'YAHOO_KEY': 'YAHOO_CONSUMER_KEY',
        'YAHOO_SECRET': 'YAHOO_CONSUMER_SECRET',
        'YAMMER_KEY': 'YAMMER_CONSUMER_KEY',
        'YAMMER_SECRET': 'YAMMER_CONSUMER_SECRET',
        'YAMMER_STAGING_KEY': 'YAMMER_STAGING_CONSUMER_KEY',
        'YAMMER_STAGING_SECRET': 'YAMMER_STAGING_CONSUMER_SECRET',
        'YANDEX_SECRET': 'YANDEX_API_SECRET',
        'YANDEX_KEY': 'YANDEX_APP_ID',
        'ON_HTTPS': 'SOCIAL_AUTH_REDIRECT_IS_HTTPS',
    }

    def get_setting(self, name):
        if name in self.settings_map:
            # Try DSA setting name from map defined above
            try:
                return super(DSAStrategy, self).get_setting(
                    self.settings_map[name]
                )
            except (AttributeError, KeyError):
                pass
        # Fallback to PSA setting name
        return super(DSAStrategy, self).get_setting(name)

    def get_pipeline(self):
        pipeline = super(DSAStrategy, self).get_pipeline()
        pipeline_renamed = []
        for entry in pipeline:
            if entry.startswith('social_auth.backends.pipeline.social'):
                entry = entry.replace(
                    'social_auth.backends.pipeline.social',
                    'social_auth.backends.pipeline.sauth'
                )
            pipeline_renamed.append(entry)
        return pipeline_renamed
