from urllib3.exceptions import LocationParseError
from urllib3.util import url

from nav_dataverk.connectors.bucket_storage_base import BucketStorageConnector
from nav_dataverk.connectors.google_storage import GoogleStorageConnector
from nav_dataverk.connectors.azure_blob_storage import AzureStorageConnector
from nav_dataverk.connectors.AWS_S3 import S3Connector
from nav_dataverk.connectors.s3 import AWSS3Connector
from collections.abc import Mapping
from enum import Enum


class BucketType(Enum):
    AWS_S3 = "aws_s3"
    DATAVERK_S3 = "dataverk_s3"
    GCS = "google_cloud"
    AZURE = "azure"
    GITHUB = "github"


def get_storage_connector(bucket_type: BucketType, bucket_name: str, settings: Mapping, encrypted: bool=True) -> BucketStorageConnector:
    if bucket_type == BucketType.AWS_S3:
        return AWSS3Connector(bucket_name=bucket_name, settings=settings, encrypted=encrypted)
    elif bucket_type == BucketType.DATAVERK_S3:
        return S3Connector(bucket_name=bucket_name, s3_endpoint=settings["bucket_storage_connections"]["dataverk_s3"]["host"], encrypted=encrypted)
    elif bucket_type == BucketType.GCS:
        project = _gcp_project_name(settings)
        credentials = _gcp_credentials(settings)
        return GoogleStorageConnector(bucket_name=bucket_name, gcp_project=project, gcp_credentials=credentials, encrypted=encrypted)
    elif bucket_type == BucketType.AZURE:
        return AzureStorageConnector(bucket_name=bucket_name, settings=settings, encrypted=encrypted)
    elif bucket_type == BucketType.GITHUB:
        return None
    else:
        raise ValueError(f'Bucket type {bucket_type} is not supported')


def _gcp_project_name(settings):
    try:
        return settings["bucket_storage_connections"]["google_cloud"]["client"]
    except KeyError:
        return None


def _gcp_credentials(settings):
    try:
        return settings["bucket_storage_connections"]["google_cloud"]["credentials"]
    except KeyError:
        return None
