from google.protobuf.json_format import MessageToDict

from dmmc.core import DarsmanMedia, ServiceClient


class MediaSDK:
    def __init__(self, url="127.0.0.1:50051"):
        self.darsman = DarsmanMedia(url=url)

    def retrieve_media(self, api_key, uuid_id):
        """
        :param uuid_id:
        :param api_key:
        :return:
        """
        service_factory = self.darsman.create_service_factory("media")
        service_client = ServiceClient(self.darsman, service_factory)

        response = service_client.retrieve_request(
            "media",
            api_key=api_key,
            uuid_id=uuid_id,
        )
        if str(response.code) != "0":
            print(response)

        return MessageToDict(response, preserving_proto_field_name=True)

    def create_media(
        self,
        api_key,
        file: bytes,
        file_name,
        file_size,
        bucket,
        store_place_type,
        acl="public-read",
        create_identifier=None,
        **kwargs,
    ):
        """
        :param create_identifier:
        :param acl:
        :param api_key:
        :param file:
        :param file_name:
        :param extension:
        :param file_size:
        :param bucket:
        :param store_place_type:
        :param kwargs:
                    extra_data: str,
        :return:
        """
        service_factory = self.darsman.create_service_factory("media")
        service_client = ServiceClient(self.darsman, service_factory)

        response = service_client.create_request(
            "media",
            api_key=api_key,
            file=file,
            file_name=file_name,
            file_size=file_size,
            bucket=bucket,
            store_place_type=store_place_type,
            acl=acl,
            create_identifier=create_identifier,
            **kwargs,
        )

        if str(response.code) != "0":
            print(response)

        return MessageToDict(response, preserving_proto_field_name=True)

    def validate_media(self, api_key, uuid_id):
        """
        :param api_key:
        :param uuid_id:
        :return: True / False
        """
        service_factory = self.darsman.create_service_factory("media")
        service_client = ServiceClient(self.darsman, service_factory)
        response = service_client.validate_request(
            "media",
            api_key=api_key,
            uuid_id=uuid_id,
        )
        if str(response.code) != "0":
            print(response)

        return MessageToDict(response, preserving_proto_field_name=True)

    def retrieve_bucket(self, api_key, uuid_id, **kwargs):
        """

        :param api_key:
        :param name:
        :param kwargs:
                is_public: object
        :return:
        """
        service_factory = self.darsman.create_service_factory("bucket")
        service_client = ServiceClient(self.darsman, service_factory)
        response = service_client.retrieve_request(
            "bucket", api_key=api_key, uuid_id=uuid_id
        )

        if str(response.code) != "0":
            print(response)

        return MessageToDict(response, preserving_proto_field_name=True)

    def create_bucket(self, api_key, name: str, **kwargs):
        """

        :param api_key:
        :param name:
        :param kwargs:
                    is_public: str,
        :return:
        """
        service_factory = self.darsman.create_service_factory("bucket")
        service_client = ServiceClient(self.darsman, service_factory)

        response = service_client.create_request(
            "bucket", api_key=api_key, name=name, **kwargs
        )

        if str(response.code) != "0":
            print(response)

        return MessageToDict(response, preserving_proto_field_name=True)

    def validate_bucket(self, api_key, uuid_id):
        """
        :param api_key:
        :param uuid_id:
        :return: True/ False
        """
        service_factory = self.darsman.create_service_factory("bucket")
        service_client = ServiceClient(self.darsman, service_factory)
        response = service_client.validate_request(
            "bucket",
            api_key=api_key,
            uuid_id=uuid_id,
        )
        if str(response.code) != "0":
            print(response)

        return MessageToDict(response, preserving_proto_field_name=True)
