# --------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for license information.
#
# Code generated by aaz-dev-tools
# --------------------------------------------------------------------------------------------

# pylint: skip-file
# flake8: noqa

from azure.cli.core.aaz import *


@register_command(
    "adp account data-pool update",
    is_experimental=True,
)
class Update(AAZCommand):
    """Update a Data Pool
    """

    _aaz_info = {
        "version": "2022-09-01-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.autonomousdevelopmentplatform/accounts/{}/datapools/{}", "2022-09-01-preview"],
        ]
    }

    AZ_SUPPORT_NO_WAIT = True

    AZ_SUPPORT_GENERIC_UPDATE = True

    def _handler(self, command_args):
        super()._handler(command_args)
        return self.build_lro_poller(self._execute_operations, self._output)

    _args_schema = None

    @classmethod
    def _build_arguments_schema(cls, *args, **kwargs):
        if cls._args_schema is not None:
            return cls._args_schema
        cls._args_schema = super()._build_arguments_schema(*args, **kwargs)

        # define Arg Group ""

        _args_schema = cls._args_schema
        _args_schema.account_name = AAZStrArg(
            options=["--account-name"],
            help="The name of the ADP account",
            required=True,
            id_part="name",
            fmt=AAZStrArgFormat(
                pattern="^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*",
                max_length=50,
            ),
        )
        _args_schema.data_pool_name = AAZStrArg(
            options=["-n", "--name", "--data-pool-name"],
            help="The name of the Data Pool",
            required=True,
            id_part="child_name_1",
            fmt=AAZStrArgFormat(
                pattern="^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*",
                max_length=50,
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.locations = AAZListArg(
            options=["--locations"],
            arg_group="Properties",
            help="Gets or sets the collection of locations where Data Pool resources should be created",
        )
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="Properties",
            help="Resource tags",
            nullable=True,
        )

        locations = cls._args_schema.locations
        locations.Element = AAZObjectArg(
            nullable=True,
        )

        _element = cls._args_schema.locations.Element
        _element.encryption = AAZObjectArg(
            options=["encryption"],
            help="Encryption properties of a Data Pool location",
            nullable=True,
        )
        _element.name = AAZStrArg(
            options=["name"],
            help="The location name",
        )
        _element.storage_account_count = AAZIntArg(
            options=["storage-account-count"],
            help="The amount of storage accounts provisioned per Data Pool. Default: 5",
            nullable=True,
            fmt=AAZIntArgFormat(
                minimum=1,
            ),
        )
        _element.storage_sku = AAZObjectArg(
            options=["storage-sku"],
            help="The Storage SKU. Default: Standard_ZRS.",
            nullable=True,
        )

        encryption = cls._args_schema.locations.Element.encryption
        encryption.key_name = AAZStrArg(
            options=["key-name"],
            help="The name of Key Vault key",
        )
        encryption.key_vault_uri = AAZStrArg(
            options=["key-vault-uri"],
            help="The URI of a soft delete-enabled Key Vault that is in the same location as the Data Pool location",
        )
        encryption.key_version = AAZStrArg(
            options=["key-version"],
            help="The version of Key Vault key",
            nullable=True,
        )
        encryption.user_assigned_identity = AAZStrArg(
            options=["user-assigned-identity"],
            help="The resource ID of a user-assigned Managed Identity used to access the encryption key in the Key Vault. Requires access to the key operations get, wrap, unwrap, and recover",
        )

        storage_sku = cls._args_schema.locations.Element.storage_sku
        storage_sku.name = AAZStrArg(
            options=["name"],
            help="The SKU name.",
            enum={"Premium_LRS": "Premium_LRS", "Premium_ZRS": "Premium_ZRS", "Standard_GRS": "Standard_GRS", "Standard_GZRS": "Standard_GZRS", "Standard_LRS": "Standard_LRS", "Standard_RAGRS": "Standard_RAGRS", "Standard_RAGZRS": "Standard_RAGZRS", "Standard_ZRS": "Standard_ZRS"},
        )

        tags = cls._args_schema.tags
        tags.Element = AAZStrArg(
            nullable=True,
        )
        return cls._args_schema

    def _execute_operations(self):
        self.DataPoolsGet(ctx=self.ctx)()
        self.InstanceUpdateByJson(ctx=self.ctx)()
        self.InstanceUpdateByGeneric(ctx=self.ctx)()
        yield self.DataPoolsCreateOrUpdate(ctx=self.ctx)()

    def _output(self, *args, **kwargs):
        result = self.deserialize_output(self.ctx.vars.instance, client_flatten=True)
        return result

    class DataPoolsGet(AAZHttpOperation):
        CLIENT_TYPE = "MgmtClient"

        def __call__(self, *args, **kwargs):
            request = self.make_request()
            session = self.client.send_request(request=request, stream=False, **kwargs)
            if session.http_response.status_code in [200]:
                return self.on_200(session)

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AutonomousDevelopmentPlatform/accounts/{accountName}/dataPools/{dataPoolName}",
                **self.url_parameters
            )

        @property
        def method(self):
            return "GET"

        @property
        def error_format(self):
            return "MgmtErrorFormat"

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "accountName", self.ctx.args.account_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "dataPoolName", self.ctx.args.data_pool_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2022-09-01-preview",
                    required=True,
                ),
            }
            return parameters

        @property
        def header_parameters(self):
            parameters = {
                **self.serialize_header_param(
                    "Accept", "application/json",
                ),
            }
            return parameters

        def on_200(self, session):
            data = self.deserialize_http_content(session)
            self.ctx.set_var(
                "instance",
                data,
                schema_builder=self._build_schema_on_200
            )

        _schema_on_200 = None

        @classmethod
        def _build_schema_on_200(cls):
            if cls._schema_on_200 is not None:
                return cls._schema_on_200

            cls._schema_on_200 = AAZObjectType()
            _build_schema_data_pool_read(cls._schema_on_200)

            return cls._schema_on_200

    class DataPoolsCreateOrUpdate(AAZHttpOperation):
        CLIENT_TYPE = "MgmtClient"

        def __call__(self, *args, **kwargs):
            request = self.make_request()
            session = self.client.send_request(request=request, stream=False, **kwargs)
            if session.http_response.status_code in [202]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200_201,
                    self.on_error,
                    lro_options={"final-state-via": "azure-async-operation"},
                    path_format_arguments=self.url_parameters,
                )
            if session.http_response.status_code in [200, 201]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200_201,
                    self.on_error,
                    lro_options={"final-state-via": "azure-async-operation"},
                    path_format_arguments=self.url_parameters,
                )

            return self.on_error(session.http_response)

        @property
        def url(self):
            return self.client.format_url(
                "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.AutonomousDevelopmentPlatform/accounts/{accountName}/dataPools/{dataPoolName}",
                **self.url_parameters
            )

        @property
        def method(self):
            return "PUT"

        @property
        def error_format(self):
            return "MgmtErrorFormat"

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "accountName", self.ctx.args.account_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "dataPoolName", self.ctx.args.data_pool_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "subscriptionId", self.ctx.subscription_id,
                    required=True,
                ),
            }
            return parameters

        @property
        def query_parameters(self):
            parameters = {
                **self.serialize_query_param(
                    "api-version", "2022-09-01-preview",
                    required=True,
                ),
            }
            return parameters

        @property
        def header_parameters(self):
            parameters = {
                **self.serialize_header_param(
                    "Content-Type", "application/json",
                ),
                **self.serialize_header_param(
                    "Accept", "application/json",
                ),
            }
            return parameters

        @property
        def content(self):
            _content_value, _builder = self.new_content_builder(
                self.ctx.args,
                value=self.ctx.vars.instance,
            )

            return self.serialize_content(_content_value)

        def on_200_201(self, session):
            data = self.deserialize_http_content(session)
            self.ctx.set_var(
                "instance",
                data,
                schema_builder=self._build_schema_on_200_201
            )

        _schema_on_200_201 = None

        @classmethod
        def _build_schema_on_200_201(cls):
            if cls._schema_on_200_201 is not None:
                return cls._schema_on_200_201

            cls._schema_on_200_201 = AAZObjectType()
            _build_schema_data_pool_read(cls._schema_on_200_201)

            return cls._schema_on_200_201

    class InstanceUpdateByJson(AAZJsonInstanceUpdateOperation):

        def __call__(self, *args, **kwargs):
            self._update_instance(self.ctx.vars.instance)

        def _update_instance(self, instance):
            _instance_value, _builder = self.new_content_builder(
                self.ctx.args,
                value=instance,
                typ=AAZObjectType
            )
            _builder.set_prop("properties", AAZObjectType, typ_kwargs={"flags": {"client_flatten": True}})

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("locations", AAZListType, ".locations", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("tags", AAZDictType, ".tags")

            locations = _builder.get(".properties.locations")
            if locations is not None:
                locations.set_elements(AAZObjectType, ".")

            _elements = _builder.get(".properties.locations[]")
            if _elements is not None:
                _elements.set_prop("encryption", AAZObjectType, ".encryption")
                _elements.set_prop("name", AAZStrType, ".name", typ_kwargs={"flags": {"required": True}})
                _elements.set_prop("storageAccountCount", AAZIntType, ".storage_account_count")
                _elements.set_prop("storageSku", AAZObjectType, ".storage_sku", typ_kwargs={"nullable": True})

            encryption = _builder.get(".properties.locations[].encryption")
            if encryption is not None:
                encryption.set_prop("keyName", AAZStrType, ".key_name", typ_kwargs={"flags": {"required": True}})
                encryption.set_prop("keyVaultUri", AAZStrType, ".key_vault_uri", typ_kwargs={"flags": {"required": True}})
                encryption.set_prop("keyVersion", AAZStrType, ".key_version")
                encryption.set_prop("userAssignedIdentity", AAZStrType, ".user_assigned_identity", typ_kwargs={"flags": {"required": True}})

            storage_sku = _builder.get(".properties.locations[].storageSku")
            if storage_sku is not None:
                storage_sku.set_prop("name", AAZStrType, ".name", typ_kwargs={"flags": {"required": True}})

            tags = _builder.get(".properties.tags")
            if tags is not None:
                tags.set_elements(AAZStrType, ".")

            return _instance_value

    class InstanceUpdateByGeneric(AAZGenericInstanceUpdateOperation):

        def __call__(self, *args, **kwargs):
            self._update_instance_by_generic(
                self.ctx.vars.instance,
                self.ctx.generic_update_args
            )


_schema_data_pool_read = None


def _build_schema_data_pool_read(_schema):
    global _schema_data_pool_read
    if _schema_data_pool_read is not None:
        _schema.id = _schema_data_pool_read.id
        _schema.name = _schema_data_pool_read.name
        _schema.properties = _schema_data_pool_read.properties
        _schema.system_data = _schema_data_pool_read.system_data
        _schema.type = _schema_data_pool_read.type
        return

    _schema_data_pool_read = AAZObjectType()

    data_pool_read = _schema_data_pool_read
    data_pool_read.id = AAZStrType(
        flags={"read_only": True},
    )
    data_pool_read.name = AAZStrType(
        flags={"read_only": True},
    )
    data_pool_read.properties = AAZObjectType(
        flags={"client_flatten": True},
    )
    data_pool_read.system_data = AAZObjectType(
        serialized_name="systemData",
        flags={"read_only": True},
    )
    data_pool_read.type = AAZStrType(
        flags={"read_only": True},
    )

    properties = _schema_data_pool_read.properties
    properties.data_pool_id = AAZStrType(
        serialized_name="dataPoolId",
        flags={"read_only": True},
    )
    properties.locations = AAZListType(
        flags={"required": True},
    )
    properties.provisioning_state = AAZStrType(
        serialized_name="provisioningState",
        flags={"read_only": True},
    )
    properties.tags = AAZDictType()

    locations = _schema_data_pool_read.properties.locations
    locations.Element = AAZObjectType()

    _element = _schema_data_pool_read.properties.locations.Element
    _element.encryption = AAZObjectType()
    _element.name = AAZStrType(
        flags={"required": True},
    )
    _element.storage_account_count = AAZIntType(
        serialized_name="storageAccountCount",
    )
    _element.storage_sku = AAZObjectType(
        serialized_name="storageSku",
        nullable=True,
    )

    encryption = _schema_data_pool_read.properties.locations.Element.encryption
    encryption.key_name = AAZStrType(
        serialized_name="keyName",
        flags={"required": True},
    )
    encryption.key_vault_uri = AAZStrType(
        serialized_name="keyVaultUri",
        flags={"required": True},
    )
    encryption.key_version = AAZStrType(
        serialized_name="keyVersion",
    )
    encryption.user_assigned_identity = AAZStrType(
        serialized_name="userAssignedIdentity",
        flags={"required": True},
    )

    storage_sku = _schema_data_pool_read.properties.locations.Element.storage_sku
    storage_sku.name = AAZStrType(
        flags={"required": True},
    )

    tags = _schema_data_pool_read.properties.tags
    tags.Element = AAZStrType()

    system_data = _schema_data_pool_read.system_data
    system_data.created_at = AAZStrType(
        serialized_name="createdAt",
        flags={"read_only": True},
    )
    system_data.created_by = AAZStrType(
        serialized_name="createdBy",
        flags={"read_only": True},
    )
    system_data.created_by_type = AAZStrType(
        serialized_name="createdByType",
        flags={"read_only": True},
    )
    system_data.last_modified_at = AAZStrType(
        serialized_name="lastModifiedAt",
        flags={"read_only": True},
    )
    system_data.last_modified_by = AAZStrType(
        serialized_name="lastModifiedBy",
        flags={"read_only": True},
    )
    system_data.last_modified_by_type = AAZStrType(
        serialized_name="lastModifiedByType",
        flags={"read_only": True},
    )

    _schema.id = _schema_data_pool_read.id
    _schema.name = _schema_data_pool_read.name
    _schema.properties = _schema_data_pool_read.properties
    _schema.system_data = _schema_data_pool_read.system_data
    _schema.type = _schema_data_pool_read.type


__all__ = ["Update"]
