######################################################################
#
# File: b2sdk/account_info/stub.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################

import collections
import threading

from .abstract import AbstractAccountInfo


class StubAccountInfo(AbstractAccountInfo):

    REALM_URLS = {'production': 'http://production.example.com'}

    def __init__(self):
        self._clear_stub_account_fields()

    def clear(self):
        self._clear_stub_account_fields()

    def _clear_stub_account_fields(self):
        self.application_key = None
        self.buckets = {}
        self.account_id = None
        self.allowed = None
        self.api_url = None
        self.auth_token = None
        self.download_url = None
        self.minimum_part_size = None
        self.realm = None
        self.application_key_id = None
        self._large_file_uploads = collections.defaultdict(list)
        self._large_file_uploads_lock = threading.Lock()

    def clear_bucket_upload_data(self, bucket_id):
        if bucket_id in self.buckets:
            del self.buckets[bucket_id]

    def _set_auth_data(
        self,
        account_id,
        auth_token,
        api_url,
        download_url,
        minimum_part_size,
        application_key,
        realm,
        allowed,
        application_key_id,
    ):
        self.account_id = account_id
        self.auth_token = auth_token
        self.api_url = api_url
        self.download_url = download_url
        self.minimum_part_size = minimum_part_size
        self.application_key = application_key
        self.realm = realm
        self.allowed = allowed
        self.application_key_id = application_key_id

    def refresh_entire_bucket_name_cache(self, name_id_iterable):
        self.buckets = {}

    def get_bucket_id_or_none_from_bucket_name(self, bucket_name):
        return None

    def save_bucket(self, bucket):
        self.buckets[bucket.bucket_id] = bucket

    def remove_bucket_name(self, bucket_name):
        pass

    def take_bucket_upload_url(self, bucket_id):
        return (None, None)

    def put_bucket_upload_url(self, bucket_id, upload_url, upload_auth_token):
        pass

    def get_account_id(self):
        return self.account_id

    def get_application_key_id(self):
        return self.application_key_id

    def get_account_auth_token(self):
        return self.auth_token

    def get_api_url(self):
        return self.api_url

    def get_application_key(self):
        return self.application_key

    def get_download_url(self):
        return self.download_url

    def get_minimum_part_size(self):
        return self.minimum_part_size

    def get_realm(self):
        return self.realm

    def get_allowed(self):
        return self.allowed

    def get_bucket_upload_data(self, bucket_id):
        return self.buckets.get(bucket_id, (None, None))

    def put_large_file_upload_url(self, file_id, upload_url, upload_auth_token):
        with self._large_file_uploads_lock:
            self._large_file_uploads[file_id].append((upload_url, upload_auth_token))

    def take_large_file_upload_url(self, file_id):
        with self._large_file_uploads_lock:
            upload_urls = self._large_file_uploads.get(file_id, [])
            if not upload_urls:
                return (None, None)
            else:
                return upload_urls.pop()

    def clear_large_file_upload_urls(self, file_id):
        with self._large_file_uploads_lock:
            if file_id in self._large_file_uploads:
                del self._large_file_uploads[file_id]
