# --------------------------------------------------------------------------------------------
# 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(
    "confidentialledger managedccfs update",
    is_preview=True,
)
class Update(AAZCommand):
    """Patch a Managed CCF instance.
    """

    _aaz_info = {
        "version": "2023-01-26-preview",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.confidentialledger/managedccfs/{}", "2023-01-26-preview"],
        ]
    }

    AZ_SUPPORT_NO_WAIT = True

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

    _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.name = AAZStrArg(
            options=["-n", "--name"],
            help="A unique name for the instance.",
            required=True,
            id_part="name",
            fmt=AAZStrArgFormat(
                pattern="^[^-0-9][A-Za-z0-9-]{1,33}[A-Za-z0-9]$",
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "ManagedCCF"

        _args_schema = cls._args_schema
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="ManagedCCF",
            help="Resource tags.",
        )

        tags = cls._args_schema.tags
        tags.Element = AAZStrArg()

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.deployment_type = AAZObjectArg(
            options=["--deployment-type"],
            arg_group="Properties",
            help="Deployment Type of Managed CCF",
        )
        _args_schema.member_certificates = AAZListArg(
            options=["--member-certificates"],
            arg_group="Properties",
            help="List of member identity certificates for Managed CCF",
        )
        _args_schema.node_count = AAZIntArg(
            options=["--node-count"],
            arg_group="Properties",
            help="Node consensus requires odd number of nodes. Select a number between 3 and 9.",
            is_preview=True,
        )

        deployment_type = cls._args_schema.deployment_type
        deployment_type.app_source_uri = AAZStrArg(
            options=["app-source-uri"],
            help={"short-summary": "Set it to 'customImage' to deploy a custom JS application or 'sample' to deploy the sample JS application.", "long-summary": "Determines the type of the JS application to deploy."},
            is_preview=True,
        )
        deployment_type.language_runtime = AAZStrArg(
            options=["language-runtime"],
            help="The language runtime value is 'JS'",
            enum={"CPP": "CPP", "JS": "JS"},
        )

        member_certificates = cls._args_schema.member_certificates
        member_certificates.Element = AAZObjectArg()

        _element = cls._args_schema.member_certificates.Element
        _element.certificate = AAZStrArg(
            options=["certificate"],
            help="Member Identity Certificate",
        )
        _element.encryptionkey = AAZStrArg(
            options=["encryptionkey"],
            help="Member Identity Certificate Encryption Key",
        )
        _element.tags = AAZDictArg(
            options=["tags"],
            help="Tags for Managed CCF Certificates",
        )

        tags = cls._args_schema.member_certificates.Element.tags
        tags.Element = AAZStrArg()
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.ManagedCCFUpdate(ctx=self.ctx)()
        self.post_operations()

    @register_callback
    def pre_operations(self):
        pass

    @register_callback
    def post_operations(self):
        pass

    class ManagedCCFUpdate(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,
                    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]:
                return self.client.build_lro_polling(
                    self.ctx.args.no_wait,
                    session,
                    self.on_200,
                    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.ConfidentialLedger/managedCCFs/{appName}",
                **self.url_parameters
            )

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

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

        @property
        def url_parameters(self):
            parameters = {
                **self.serialize_url_param(
                    "appName", self.ctx.args.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", "2023-01-26-preview",
                    required=True,
                ),
            }
            return parameters

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

        @property
        def content(self):
            _content_value, _builder = self.new_content_builder(
                self.ctx.args,
                typ=AAZObjectType,
                typ_kwargs={"flags": {"required": True, "client_flatten": True}}
            )
            _builder.set_prop("properties", AAZObjectType)
            _builder.set_prop("tags", AAZDictType, ".tags")

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("deploymentType", AAZObjectType, ".deployment_type")
                properties.set_prop("memberIdentityCertificates", AAZListType, ".member_certificates")
                properties.set_prop("nodeCount", AAZIntType, ".node_count")

            deployment_type = _builder.get(".properties.deploymentType")
            if deployment_type is not None:
                deployment_type.set_prop("appSourceUri", AAZStrType, ".app_source_uri")
                deployment_type.set_prop("languageRuntime", AAZStrType, ".language_runtime")

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

            _elements = _builder.get(".properties.memberIdentityCertificates[]")
            if _elements is not None:
                _elements.set_prop("certificate", AAZStrType, ".certificate")
                _elements.set_prop("encryptionkey", AAZStrType, ".encryptionkey")
                _elements.set_prop("tags", AAZDictType, ".tags")

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

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

            return self.serialize_content(_content_value)

        def on_200(self, session):
            pass


class _UpdateHelper:
    """Helper class for Update"""


__all__ = ["Update"]
