# --------------------------------------------------------------------------------------------
# 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(
    "mobile-network sim create",
)
class Create(AAZCommand):
    """Create a SIM.

    :example: Create sim
        az mobile-network sim create -g rg --sim-group-name sim-group-name -n sim-name --international-msi 0000000000 --operator-key-code 00000000000000000000000000000000 --authentication-key 00000000000000000000000000000000
    """

    _aaz_info = {
        "version": "2022-11-01",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/microsoft.mobilenetwork/simgroups/{}/sims/{}", "2022-11-01"],
        ]
    }

    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.resource_group = AAZResourceGroupNameArg(
            required=True,
        )
        _args_schema.sim_group_name = AAZStrArg(
            options=["--sim-group-name"],
            help="The name of the SIM Group.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^[a-zA-Z0-9][a-zA-Z0-9_-]*$",
                max_length=64,
            ),
        )
        _args_schema.sim_name = AAZStrArg(
            options=["-n", "--name", "--sim-name"],
            help="The name of the SIM.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^[a-zA-Z0-9][a-zA-Z0-9_-]*$",
                max_length=64,
            ),
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.authentication_key = AAZStrArg(
            options=["--authentication-key"],
            arg_group="Properties",
            help="The Ki value for the SIM.",
            fmt=AAZStrArgFormat(
                pattern="^[0-9a-fA-F]{32}$",
            ),
        )
        _args_schema.device_type = AAZStrArg(
            options=["--device-type"],
            arg_group="Properties",
            help="An optional free-form text field that can be used to record the device type this SIM is associated with, for example 'Video camera'. The Azure portal allows SIMs to be grouped and filtered based on this value.",
        )
        _args_schema.icc_id = AAZStrArg(
            options=["--icc-id"],
            arg_group="Properties",
            help="The integrated circuit card ID (ICCID) for the SIM.",
            fmt=AAZStrArgFormat(
                pattern="^[0-9]{10,20}$",
            ),
        )
        _args_schema.international_msi = AAZStrArg(
            options=["--international-msi"],
            arg_group="Properties",
            help="The international mobile subscriber identity (IMSI) for the SIM.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^[0-9]{5,15}$",
            ),
        )
        _args_schema.operator_key_code = AAZStrArg(
            options=["--operator-key-code"],
            arg_group="Properties",
            help="The Opc value for the SIM.",
            fmt=AAZStrArgFormat(
                pattern="^[0-9a-fA-F]{32}$",
            ),
        )
        _args_schema.sim_policy = AAZObjectArg(
            options=["--sim-policy"],
            arg_group="Properties",
            help="The SIM policy used by this SIM.",
        )
        _args_schema.static_ip_config = AAZListArg(
            options=["--static-ip-config"],
            arg_group="Properties",
            help="A list of static IP addresses assigned to this SIM. Each address is assigned at a defined network scope, made up of {attached data network, slice}.",
            fmt=AAZListArgFormat(
                unique=True,
            ),
        )

        sim_policy = cls._args_schema.sim_policy
        sim_policy.id = AAZStrArg(
            options=["id"],
            help="SIM policy resource ID.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^/[sS][uU][bB][sS][cC][rR][iI][pP][tT][iI][oO][nN][sS]/[^/?#]+/[rR][eE][sS][oO][uU][rR][cC][eE][gG][rR][oO][uU][pP][sS]/[^/?#]+/[pP][rR][oO][vV][iI][dD][eE][rR][sS]/[mM][iI][cC][rR][oO][sS][oO][fF][tT]\.[mM][oO][bB][iI][lL][eE][nN][eE][tT][wW][oO][rR][kK]/[mM][oO][bB][iI][lL][eE][nN][eE][tT][wW][oO][rR][kK][sS]/[^/?#]+/[sS][iI][mM][pP][oO][lL][iI][cC][iI][eE][sS]/[^/?#]+$",
            ),
        )

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

        _element = cls._args_schema.static_ip_config.Element
        _element.attached_data_network = AAZObjectArg(
            options=["attached-data-network"],
            help="The attached data network on which the static IP address will be used. The combination of attached data network and slice defines the network scope of the IP address.",
        )
        _element.slice = AAZObjectArg(
            options=["slice"],
            help="The network slice on which the static IP address will be used. The combination of attached data network and slice defines the network scope of the IP address.",
        )
        _element.static_ip = AAZObjectArg(
            options=["static-ip"],
            help="The static IP configuration for the SIM to use at the defined network scope.",
        )

        attached_data_network = cls._args_schema.static_ip_config.Element.attached_data_network
        attached_data_network.id = AAZStrArg(
            options=["id"],
            help="Attached data network resource ID.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^/[sS][uU][bB][sS][cC][rR][iI][pP][tT][iI][oO][nN][sS]/[^/?#]+/[rR][eE][sS][oO][uU][rR][cC][eE][gG][rR][oO][uU][pP][sS]/[^/?#]+/[pP][rR][oO][vV][iI][dD][eE][rR][sS]/[mM][iI][cC][rR][oO][sS][oO][fF][tT]\.[mM][oO][bB][iI][lL][eE][nN][eE][tT][wW][oO][rR][kK]/[pP][aA][cC][kK][eE][tT][cC][oO][rR][eE][cC][oO][nN][tT][rR][oO][lL][pP][lL][aA][nN][eE][sS]/[^/?#]+/[pP][aA][cC][kK][eE][tT][cC][oO][rR][eE][dD][aA][tT][aA][pP][lL][aA][nN][eE][sS]/[^/?#]+/[aA][tT][tT][aA][cC][hH][eE][dD][dD][aA][tT][aA][nN][eE][tT][wW][oO][rR][kK][sS]/[^/?#]+$",
            ),
        )

        slice = cls._args_schema.static_ip_config.Element.slice
        slice.id = AAZStrArg(
            options=["id"],
            help="Slice resource ID.",
            required=True,
            fmt=AAZStrArgFormat(
                pattern="^/[sS][uU][bB][sS][cC][rR][iI][pP][tT][iI][oO][nN][sS]/[^/?#]+/[rR][eE][sS][oO][uU][rR][cC][eE][gG][rR][oO][uU][pP][sS]/[^/?#]+/[pP][rR][oO][vV][iI][dD][eE][rR][sS]/[mM][iI][cC][rR][oO][sS][oO][fF][tT]\.[mM][oO][bB][iI][lL][eE][nN][eE][tT][wW][oO][rR][kK]/[mM][oO][bB][iI][lL][eE][nN][eE][tT][wW][oO][rR][kK][sS]/[^/?#]+/[sS][lL][iI][cC][eE][sS]/[^/?#]+$",
            ),
        )

        static_ip = cls._args_schema.static_ip_config.Element.static_ip
        static_ip.ipv4_address = AAZStrArg(
            options=["ipv4-address"],
            help="The IPv4 address assigned to the SIM at this network scope. This address must be in the userEquipmentStaticAddressPoolPrefix defined in the attached data network.",
            fmt=AAZStrArgFormat(
                pattern="^(([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$",
            ),
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.SimsCreateOrUpdate(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 SimsCreateOrUpdate(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.MobileNetwork/simGroups/{simGroupName}/sims/{simName}",
                **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(
                    "resourceGroupName", self.ctx.args.resource_group,
                    required=True,
                ),
                **self.serialize_url_param(
                    "simGroupName", self.ctx.args.sim_group_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "simName", self.ctx.args.sim_name,
                    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-11-01",
                    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("properties", AAZObjectType, ".", typ_kwargs={"flags": {"required": True, "client_flatten": True}})

            properties = _builder.get(".properties")
            if properties is not None:
                properties.set_prop("authenticationKey", AAZStrType, ".authentication_key")
                properties.set_prop("deviceType", AAZStrType, ".device_type")
                properties.set_prop("integratedCircuitCardIdentifier", AAZStrType, ".icc_id")
                properties.set_prop("internationalMobileSubscriberIdentity", AAZStrType, ".international_msi", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("operatorKeyCode", AAZStrType, ".operator_key_code")
                properties.set_prop("simPolicy", AAZObjectType, ".sim_policy")
                properties.set_prop("staticIpConfiguration", AAZListType, ".static_ip_config")

            sim_policy = _builder.get(".properties.simPolicy")
            if sim_policy is not None:
                sim_policy.set_prop("id", AAZStrType, ".id", typ_kwargs={"flags": {"required": True}})

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

            _elements = _builder.get(".properties.staticIpConfiguration[]")
            if _elements is not None:
                _elements.set_prop("attachedDataNetwork", AAZObjectType, ".attached_data_network")
                _elements.set_prop("slice", AAZObjectType, ".slice")
                _elements.set_prop("staticIp", AAZObjectType, ".static_ip")

            attached_data_network = _builder.get(".properties.staticIpConfiguration[].attachedDataNetwork")
            if attached_data_network is not None:
                attached_data_network.set_prop("id", AAZStrType, ".id", typ_kwargs={"flags": {"required": True}})

            slice = _builder.get(".properties.staticIpConfiguration[].slice")
            if slice is not None:
                slice.set_prop("id", AAZStrType, ".id", typ_kwargs={"flags": {"required": True}})

            static_ip = _builder.get(".properties.staticIpConfiguration[].staticIp")
            if static_ip is not None:
                static_ip.set_prop("ipv4Address", AAZStrType, ".ipv4_address")

            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.name = AAZStrType(
                flags={"read_only": True},
            )
            _schema_on_200_201.properties = AAZObjectType(
                flags={"required": True, "client_flatten": True},
            )
            _schema_on_200_201.system_data = AAZObjectType(
                serialized_name="systemData",
                flags={"read_only": True},
            )
            _schema_on_200_201.type = AAZStrType(
                flags={"read_only": True},
            )

            properties = cls._schema_on_200_201.properties
            properties.device_type = AAZStrType(
                serialized_name="deviceType",
            )
            properties.integrated_circuit_card_identifier = AAZStrType(
                serialized_name="integratedCircuitCardIdentifier",
            )
            properties.international_mobile_subscriber_identity = AAZStrType(
                serialized_name="internationalMobileSubscriberIdentity",
                flags={"required": True},
            )
            properties.provisioning_state = AAZStrType(
                serialized_name="provisioningState",
                flags={"read_only": True},
            )
            properties.sim_policy = AAZObjectType(
                serialized_name="simPolicy",
            )
            properties.sim_state = AAZStrType(
                serialized_name="simState",
                flags={"read_only": True},
            )
            properties.site_provisioning_state = AAZDictType(
                serialized_name="siteProvisioningState",
                flags={"read_only": True},
            )
            properties.static_ip_configuration = AAZListType(
                serialized_name="staticIpConfiguration",
            )
            properties.vendor_key_fingerprint = AAZStrType(
                serialized_name="vendorKeyFingerprint",
                flags={"read_only": True},
            )
            properties.vendor_name = AAZStrType(
                serialized_name="vendorName",
                flags={"read_only": True},
            )

            sim_policy = cls._schema_on_200_201.properties.sim_policy
            sim_policy.id = AAZStrType(
                flags={"required": True},
            )

            site_provisioning_state = cls._schema_on_200_201.properties.site_provisioning_state
            site_provisioning_state.Element = AAZStrType(
                flags={"read_only": True},
            )

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

            _element = cls._schema_on_200_201.properties.static_ip_configuration.Element
            _element.attached_data_network = AAZObjectType(
                serialized_name="attachedDataNetwork",
            )
            _element.slice = AAZObjectType()
            _element.static_ip = AAZObjectType(
                serialized_name="staticIp",
            )

            attached_data_network = cls._schema_on_200_201.properties.static_ip_configuration.Element.attached_data_network
            attached_data_network.id = AAZStrType(
                flags={"required": True},
            )

            slice = cls._schema_on_200_201.properties.static_ip_configuration.Element.slice
            slice.id = AAZStrType(
                flags={"required": True},
            )

            static_ip = cls._schema_on_200_201.properties.static_ip_configuration.Element.static_ip
            static_ip.ipv4_address = AAZStrType(
                serialized_name="ipv4Address",
            )

            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",
            )

            return cls._schema_on_200_201


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


__all__ = ["Create"]
