# --------------------------------------------------------------------------------------------
# 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(
    "voice-service gateway create",
)
class Create(AAZCommand):
    """Create a communications gateway

    :example: Create gateway
        az voice-service gateway create -n gw1 -g voicetest --service-locations "[{name:useast,PrimaryRegionProperties:{operatorAddresses:[198.51.100.1],allowedSignalingSourceAddressPrefixes:[10.1.1.0/24],allowedMediaSourceAddressPrefixes:[10.1.2.0/24]}},{name:useast2,PrimaryRegionProperties:{operatorAddresses:[198.51.100.2],allowedSignalingSourceAddressPrefixes:[10.2.1.0/24],allowedMediaSourceAddressPrefixes:[10.2.2.0/24]}}]" --connectivity PublicAddress --codecs "[PCMA]" --e911-type Standard --platforms "[OperatorConnect]"
    """

    _aaz_info = {
        "version": "2023-01-31",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.voiceservices/communicationsgateways/{}", "2023-01-31"],
        ]
    }

    AZ_SUPPORT_NO_WAIT = 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.gateway_name = AAZStrArg(
            options=["-n", "--name", "--gateway-name"],
            help="Unique identifier for this deployment",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^[a-zA-Z0-9-]{3,24}$",
            ),
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.domain_scope = AAZStrArg(
            options=["--domain-scope"],
            arg_group="Properties",
            help="The scope at which the auto-generated domain name can be re-used",
            default="TenantReuse",
            enum={"NoReuse": "NoReuse", "ResourceGroupReuse": "ResourceGroupReuse", "SubscriptionReuse": "SubscriptionReuse", "TenantReuse": "TenantReuse"},
        )
        _args_schema.codecs = AAZListArg(
            options=["--codecs"],
            arg_group="Properties",
            help="Voice codecs to support",
        )
        _args_schema.connectivity = AAZStrArg(
            options=["--connectivity"],
            arg_group="Properties",
            help="How to connect back to the operator network, e.g. MAPS",
            enum={"PublicAddress": "PublicAddress"},
        )
        _args_schema.e911_type = AAZStrArg(
            options=["--e911-type"],
            arg_group="Properties",
            help="How to handle 911 calls",
            enum={"DirectToEsrp": "DirectToEsrp", "Standard": "Standard"},
        )
        _args_schema.emergency_dial_strings = AAZListArg(
            options=["--emergency-dial-strings"],
            arg_group="Properties",
            help="A list of dial strings used for emergency calling.",
            default=["911", "933"],
        )
        _args_schema.on_prem_mcp_enabled = AAZBoolArg(
            options=["--on-prem-mcp-enabled"],
            arg_group="Properties",
            help="Whether an on-premises Mobile Control Point is in use.",
            default=False,
        )
        _args_schema.platforms = AAZListArg(
            options=["--platforms"],
            arg_group="Properties",
            help="What platforms to support",
        )
        _args_schema.service_locations = AAZListArg(
            options=["--service-locations"],
            arg_group="Properties",
            help="The regions in which to deploy the resources needed for Teams Calling",
        )
        _args_schema.teams_voicemail = AAZStrArg(
            options=["--teams-voicemail"],
            arg_group="Properties",
            help="This number is used in Teams Phone Mobile scenarios for access to the voicemail IVR from the native dialer.",
        )

        codecs = cls._args_schema.codecs
        codecs.Element = AAZStrArg(
            enum={"G722": "G722", "G722_2": "G722_2", "PCMA": "PCMA", "PCMU": "PCMU", "SILK_16": "SILK_16", "SILK_8": "SILK_8"},
        )

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

        platforms = cls._args_schema.platforms
        platforms.Element = AAZStrArg(
            enum={"OperatorConnect": "OperatorConnect", "TeamsPhoneMobile": "TeamsPhoneMobile"},
        )

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

        _element = cls._args_schema.service_locations.Element
        _element.name = AAZStrArg(
            options=["name"],
            help="The name of the region in which the resources needed for Teams Calling will be deployed.",
            required=True,
        )
        _element.primary_region_properties = AAZObjectArg(
            options=["primary-region-properties"],
            help="The configuration used in this region as primary, and other regions as backup.",
            required=True,
        )

        primary_region_properties = cls._args_schema.service_locations.Element.primary_region_properties
        primary_region_properties.allowed_media_source_address_prefixes = AAZListArg(
            options=["allowed-media-source-address-prefixes"],
            help="The allowed source IP address or CIDR ranges for media",
            default=[],
        )
        primary_region_properties.allowed_signaling_source_address_prefixes = AAZListArg(
            options=["allowed-signaling-source-address-prefixes"],
            help="The allowed source IP address or CIDR ranges for signaling",
            default=[],
        )
        primary_region_properties.esrp_addresses = AAZListArg(
            options=["esrp-addresses"],
            help="IP address to use to contact the ESRP from this region",
        )
        primary_region_properties.operator_addresses = AAZListArg(
            options=["operator-addresses"],
            help="IP address to use to contact the operator network from this region",
            required=True,
        )

        allowed_media_source_address_prefixes = cls._args_schema.service_locations.Element.primary_region_properties.allowed_media_source_address_prefixes
        allowed_media_source_address_prefixes.Element = AAZStrArg()

        allowed_signaling_source_address_prefixes = cls._args_schema.service_locations.Element.primary_region_properties.allowed_signaling_source_address_prefixes
        allowed_signaling_source_address_prefixes.Element = AAZStrArg()

        esrp_addresses = cls._args_schema.service_locations.Element.primary_region_properties.esrp_addresses
        esrp_addresses.Element = AAZStrArg()

        operator_addresses = cls._args_schema.service_locations.Element.primary_region_properties.operator_addresses
        operator_addresses.Element = AAZStrArg()

        # define Arg Group "Resource"

        _args_schema = cls._args_schema
        _args_schema.location = AAZResourceLocationArg(
            arg_group="Resource",
            help="The geo-location where the resource lives",
            required=True,
            fmt=AAZResourceLocationArgFormat(
                resource_group_arg="resource_group",
            ),
        )
        _args_schema.tags = AAZDictArg(
            options=["--tags"],
            arg_group="Resource",
            help="Resource tags.",
        )

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

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

    @register_callback
    def pre_operations(self):
        pass

    @register_callback
    def post_operations(self):
        pass

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

    class CommunicationsGatewaysCreateOrUpdate(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.VoiceServices/communicationsGateways/{communicationsGatewayName}",
                **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(
                    "communicationsGatewayName", self.ctx.args.gateway_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-31",
                    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,
                typ=AAZObjectType,
                typ_kwargs={"flags": {"required": True, "client_flatten": True}}
            )
            _builder.set_prop("location", AAZStrType, ".location", typ_kwargs={"flags": {"required": True}})
            _builder.set_prop("properties", AAZObjectType, typ_kwargs={"flags": {"client_flatten": True}})
            _builder.set_prop("tags", AAZDictType, ".tags")

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("autoGeneratedDomainNameLabelScope", AAZStrType, ".domain_scope")
                properties.set_prop("codecs", AAZListType, ".codecs", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("connectivity", AAZStrType, ".connectivity", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("e911Type", AAZStrType, ".e911_type", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("emergencyDialStrings", AAZListType, ".emergency_dial_strings")
                properties.set_prop("onPremMcpEnabled", AAZBoolType, ".on_prem_mcp_enabled")
                properties.set_prop("platforms", AAZListType, ".platforms", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("serviceLocations", AAZListType, ".service_locations", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("teamsVoicemailPilotNumber", AAZStrType, ".teams_voicemail")

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

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

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

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

            _elements = _builder.get(".properties.serviceLocations[]")
            if _elements is not None:
                _elements.set_prop("name", AAZStrType, ".name", typ_kwargs={"flags": {"required": True}})
                _elements.set_prop("primaryRegionProperties", AAZObjectType, ".primary_region_properties", typ_kwargs={"flags": {"required": True}})

            primary_region_properties = _builder.get(".properties.serviceLocations[].primaryRegionProperties")
            if primary_region_properties is not None:
                primary_region_properties.set_prop("allowedMediaSourceAddressPrefixes", AAZListType, ".allowed_media_source_address_prefixes")
                primary_region_properties.set_prop("allowedSignalingSourceAddressPrefixes", AAZListType, ".allowed_signaling_source_address_prefixes")
                primary_region_properties.set_prop("esrpAddresses", AAZListType, ".esrp_addresses")
                primary_region_properties.set_prop("operatorAddresses", AAZListType, ".operator_addresses", typ_kwargs={"flags": {"required": True}})

            allowed_media_source_address_prefixes = _builder.get(".properties.serviceLocations[].primaryRegionProperties.allowedMediaSourceAddressPrefixes")
            if allowed_media_source_address_prefixes is not None:
                allowed_media_source_address_prefixes.set_elements(AAZStrType, ".")

            allowed_signaling_source_address_prefixes = _builder.get(".properties.serviceLocations[].primaryRegionProperties.allowedSignalingSourceAddressPrefixes")
            if allowed_signaling_source_address_prefixes is not None:
                allowed_signaling_source_address_prefixes.set_elements(AAZStrType, ".")

            esrp_addresses = _builder.get(".properties.serviceLocations[].primaryRegionProperties.esrpAddresses")
            if esrp_addresses is not None:
                esrp_addresses.set_elements(AAZStrType, ".")

            operator_addresses = _builder.get(".properties.serviceLocations[].primaryRegionProperties.operatorAddresses")
            if operator_addresses is not None:
                operator_addresses.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_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()

            _schema_on_200_201 = cls._schema_on_200_201
            _schema_on_200_201.id = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.location = AAZStrType(
                flags={"required": True},
            )
            _schema_on_200_201.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.properties = AAZObjectType(
                flags={"client_flatten": True},
            )
            _schema_on_200_201.system_data = AAZObjectType(
                serialized_name="systemData",
                flags={"read_only": True},
            )
            _schema_on_200_201.tags = AAZDictType()
            _schema_on_200_201.type = AAZStrType(
                flags={"read_only": True},
            )

            properties = cls._schema_on_200_201.properties
            properties.auto_generated_domain_name_label = AAZStrType(
                serialized_name="autoGeneratedDomainNameLabel",
                flags={"read_only": True},
            )
            properties.auto_generated_domain_name_label_scope = AAZStrType(
                serialized_name="autoGeneratedDomainNameLabelScope",
            )
            properties.codecs = AAZListType(
                flags={"required": True},
            )
            properties.connectivity = AAZStrType(
                flags={"required": True},
            )
            properties.e911_type = AAZStrType(
                serialized_name="e911Type",
                flags={"required": True},
            )
            properties.emergency_dial_strings = AAZListType(
                serialized_name="emergencyDialStrings",
            )
            properties.on_prem_mcp_enabled = AAZBoolType(
                serialized_name="onPremMcpEnabled",
            )
            properties.platforms = AAZListType(
                flags={"required": True},
            )
            properties.provisioning_state = AAZStrType(
                serialized_name="provisioningState",
            )
            properties.service_locations = AAZListType(
                serialized_name="serviceLocations",
                flags={"required": True},
            )
            properties.status = AAZStrType()
            properties.teams_voicemail_pilot_number = AAZStrType(
                serialized_name="teamsVoicemailPilotNumber",
            )

            codecs = cls._schema_on_200_201.properties.codecs
            codecs.Element = AAZStrType()

            emergency_dial_strings = cls._schema_on_200_201.properties.emergency_dial_strings
            emergency_dial_strings.Element = AAZStrType()

            platforms = cls._schema_on_200_201.properties.platforms
            platforms.Element = AAZStrType()

            service_locations = cls._schema_on_200_201.properties.service_locations
            service_locations.Element = AAZObjectType()

            _element = cls._schema_on_200_201.properties.service_locations.Element
            _element.name = AAZStrType(
                flags={"required": True},
            )
            _element.primary_region_properties = AAZObjectType(
                serialized_name="primaryRegionProperties",
                flags={"required": True},
            )

            primary_region_properties = cls._schema_on_200_201.properties.service_locations.Element.primary_region_properties
            primary_region_properties.allowed_media_source_address_prefixes = AAZListType(
                serialized_name="allowedMediaSourceAddressPrefixes",
            )
            primary_region_properties.allowed_signaling_source_address_prefixes = AAZListType(
                serialized_name="allowedSignalingSourceAddressPrefixes",
            )
            primary_region_properties.esrp_addresses = AAZListType(
                serialized_name="esrpAddresses",
            )
            primary_region_properties.operator_addresses = AAZListType(
                serialized_name="operatorAddresses",
                flags={"required": True},
            )

            allowed_media_source_address_prefixes = cls._schema_on_200_201.properties.service_locations.Element.primary_region_properties.allowed_media_source_address_prefixes
            allowed_media_source_address_prefixes.Element = AAZStrType()

            allowed_signaling_source_address_prefixes = cls._schema_on_200_201.properties.service_locations.Element.primary_region_properties.allowed_signaling_source_address_prefixes
            allowed_signaling_source_address_prefixes.Element = AAZStrType()

            esrp_addresses = cls._schema_on_200_201.properties.service_locations.Element.primary_region_properties.esrp_addresses
            esrp_addresses.Element = AAZStrType()

            operator_addresses = cls._schema_on_200_201.properties.service_locations.Element.primary_region_properties.operator_addresses
            operator_addresses.Element = AAZStrType()

            system_data = cls._schema_on_200_201.system_data
            system_data.created_at = AAZStrType(
                serialized_name="createdAt",
            )
            system_data.created_by = AAZStrType(
                serialized_name="createdBy",
            )
            system_data.created_by_type = AAZStrType(
                serialized_name="createdByType",
            )
            system_data.last_modified_at = AAZStrType(
                serialized_name="lastModifiedAt",
            )
            system_data.last_modified_by = AAZStrType(
                serialized_name="lastModifiedBy",
            )
            system_data.last_modified_by_type = AAZStrType(
                serialized_name="lastModifiedByType",
            )

            tags = cls._schema_on_200_201.tags
            tags.Element = AAZStrType()

            return cls._schema_on_200_201


class _CreateHelper:
    """Helper class for Create"""


__all__ = ["Create"]
