# --------------------------------------------------------------------------------------------
# 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(
    "palo-alto cloudngfw local-rulestack local-rule create",
)
class Create(AAZCommand):
    """Create a LocalRulesResource

    :example: Create a LocalRulesResource
        az palo-alto cloudngfw local-rulestack local-rule create -g MyResourceGroup --local-rulestack-name MyLocalRulestacks --rule-name MyLocalRule --priority "1" --action-type "Allow" --enable-logging "DISABLED" --rule-state "DISABLED"
    """

    _aaz_info = {
        "version": "2022-08-29",
        "resources": [
            ["mgmt-plane", "/subscriptions/{}/resourcegroups/{}/providers/paloaltonetworks.cloudngfw/localrulestacks/{}/localrules/{}", "2022-08-29"],
        ]
    }

    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.local_rulestack_name = AAZStrArg(
            options=["--local-rulestack-name"],
            help="LocalRulestack resource name",
            required=True,
        )
        _args_schema.priority = AAZStrArg(
            options=["--priority"],
            help="Local Rule priority",
            required=True,
        )
        _args_schema.resource_group = AAZResourceGroupNameArg(
            required=True,
        )

        # define Arg Group "Properties"

        _args_schema = cls._args_schema
        _args_schema.action_type = AAZStrArg(
            options=["--action-type"],
            arg_group="Properties",
            help="rule action",
            enum={"Allow": "Allow", "DenyResetBoth": "DenyResetBoth", "DenyResetServer": "DenyResetServer", "DenySilent": "DenySilent"},
        )
        _args_schema.applications = AAZListArg(
            options=["--applications"],
            arg_group="Properties",
            help="array of rule applications",
        )
        _args_schema.audit_comment = AAZStrArg(
            options=["--audit-comment"],
            arg_group="Properties",
            help="rule comment",
        )
        _args_schema.category = AAZObjectArg(
            options=["--category"],
            arg_group="Properties",
            help="rule category",
        )
        _args_schema.decryption_rule_type = AAZStrArg(
            options=["--decryption-rule-type"],
            arg_group="Properties",
            help="enable or disable decryption",
            enum={"None": "None", "SSLInboundInspection": "SSLInboundInspection", "SSLOutboundInspection": "SSLOutboundInspection"},
        )
        _args_schema.description = AAZStrArg(
            options=["--description"],
            arg_group="Properties",
            help="rule description",
        )
        _args_schema.destination = AAZObjectArg(
            options=["--destination"],
            arg_group="Properties",
            help="destination address",
        )
        _args_schema.enable_logging = AAZStrArg(
            options=["--enable-logging"],
            arg_group="Properties",
            help="enable or disable logging",
            enum={"DISABLED": "DISABLED", "ENABLED": "ENABLED"},
        )
        _args_schema.etag = AAZStrArg(
            options=["--etag"],
            arg_group="Properties",
            help="etag info",
        )
        _args_schema.inbound_inspection_certificate = AAZStrArg(
            options=["--inbound-certificate", "--inbound-inspection-certificate"],
            arg_group="Properties",
            help="inbound Inspection Certificate",
        )
        _args_schema.negate_destination = AAZStrArg(
            options=["--negate-destination"],
            arg_group="Properties",
            help="cidr should not be 'any'",
            enum={"FALSE": "FALSE", "TRUE": "TRUE"},
        )
        _args_schema.negate_source = AAZStrArg(
            options=["--negate-source"],
            arg_group="Properties",
            help="cidr should not be 'any'",
            enum={"FALSE": "FALSE", "TRUE": "TRUE"},
        )
        _args_schema.protocol = AAZStrArg(
            options=["--protocol"],
            arg_group="Properties",
            help="any, application-default, TCP:number, UDP:number",
            default="application-default",
        )
        _args_schema.protocol_port_list = AAZListArg(
            options=["--protocol-port-list"],
            arg_group="Properties",
            help="prot port list",
        )
        _args_schema.rule_name = AAZStrArg(
            options=["--rule-name"],
            arg_group="Properties",
            help="rule name",
            required=True,
        )
        _args_schema.rule_state = AAZStrArg(
            options=["--rule-state"],
            arg_group="Properties",
            help="state of this rule",
            enum={"DISABLED": "DISABLED", "ENABLED": "ENABLED"},
        )
        _args_schema.source = AAZObjectArg(
            options=["--source"],
            arg_group="Properties",
            help="source address",
        )
        _args_schema.tags = AAZListArg(
            options=["--tags"],
            arg_group="Properties",
            help="tag for rule",
        )

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

        category = cls._args_schema.category
        category.feeds = AAZListArg(
            options=["feeds"],
            help="feed list",
            required=True,
        )
        category.url_custom = AAZListArg(
            options=["url-custom"],
            help="custom URL",
            required=True,
        )

        feeds = cls._args_schema.category.feeds
        feeds.Element = AAZStrArg()

        url_custom = cls._args_schema.category.url_custom
        url_custom.Element = AAZStrArg()

        destination = cls._args_schema.destination
        destination.cidrs = AAZListArg(
            options=["cidrs"],
            help="special value 'any'",
        )
        destination.countries = AAZListArg(
            options=["countries"],
            help="list of countries",
        )
        destination.feeds = AAZListArg(
            options=["feeds"],
            help="list of feeds",
        )
        destination.fqdn_lists = AAZListArg(
            options=["fqdn-lists"],
            help="fqdn list",
        )
        destination.prefix_lists = AAZListArg(
            options=["prefix-lists"],
            help="prefix list",
        )

        cidrs = cls._args_schema.destination.cidrs
        cidrs.Element = AAZStrArg()

        countries = cls._args_schema.destination.countries
        countries.Element = AAZStrArg()

        feeds = cls._args_schema.destination.feeds
        feeds.Element = AAZStrArg()

        fqdn_lists = cls._args_schema.destination.fqdn_lists
        fqdn_lists.Element = AAZStrArg()

        prefix_lists = cls._args_schema.destination.prefix_lists
        prefix_lists.Element = AAZStrArg()

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

        source = cls._args_schema.source
        source.cidrs = AAZListArg(
            options=["cidrs"],
            help="special value 'any'",
        )
        source.countries = AAZListArg(
            options=["countries"],
            help="list of countries",
        )
        source.feeds = AAZListArg(
            options=["feeds"],
            help="list of feeds",
        )
        source.prefix_lists = AAZListArg(
            options=["prefix-lists"],
            help="prefix list",
        )

        cidrs = cls._args_schema.source.cidrs
        cidrs.Element = AAZStrArg()

        countries = cls._args_schema.source.countries
        countries.Element = AAZStrArg()

        feeds = cls._args_schema.source.feeds
        feeds.Element = AAZStrArg()

        prefix_lists = cls._args_schema.source.prefix_lists
        prefix_lists.Element = AAZStrArg()

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

        _element = cls._args_schema.tags.Element
        _element.key = AAZStrArg(
            options=["key"],
            help="tag name",
            required=True,
        )
        _element.value = AAZStrArg(
            options=["value"],
            help="tag value",
            required=True,
        )
        return cls._args_schema

    def _execute_operations(self):
        self.pre_operations()
        yield self.LocalRulesCreateOrUpdate(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 LocalRulesCreateOrUpdate(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/PaloAltoNetworks.Cloudngfw/localRulestacks/{localRulestackName}/localRules/{priority}",
                **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(
                    "localRulestackName", self.ctx.args.local_rulestack_name,
                    required=True,
                ),
                **self.serialize_url_param(
                    "priority", self.ctx.args.priority,
                    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-08-29",
                    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("actionType", AAZStrType, ".action_type")
                properties.set_prop("applications", AAZListType, ".applications")
                properties.set_prop("auditComment", AAZStrType, ".audit_comment")
                properties.set_prop("category", AAZObjectType, ".category")
                properties.set_prop("decryptionRuleType", AAZStrType, ".decryption_rule_type")
                properties.set_prop("description", AAZStrType, ".description")
                properties.set_prop("destination", AAZObjectType, ".destination")
                properties.set_prop("enableLogging", AAZStrType, ".enable_logging")
                properties.set_prop("etag", AAZStrType, ".etag")
                properties.set_prop("inboundInspectionCertificate", AAZStrType, ".inbound_inspection_certificate")
                properties.set_prop("negateDestination", AAZStrType, ".negate_destination")
                properties.set_prop("negateSource", AAZStrType, ".negate_source")
                properties.set_prop("protocol", AAZStrType, ".protocol")
                properties.set_prop("protocolPortList", AAZListType, ".protocol_port_list")
                properties.set_prop("ruleName", AAZStrType, ".rule_name", typ_kwargs={"flags": {"required": True}})
                properties.set_prop("ruleState", AAZStrType, ".rule_state")
                properties.set_prop("source", AAZObjectType, ".source")
                properties.set_prop("tags", AAZListType, ".tags")

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

            category = _builder.get(".properties.category")
            if category is not None:
                category.set_prop("feeds", AAZListType, ".feeds", typ_kwargs={"flags": {"required": True}})
                category.set_prop("urlCustom", AAZListType, ".url_custom", typ_kwargs={"flags": {"required": True}})

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

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

            destination = _builder.get(".properties.destination")
            if destination is not None:
                destination.set_prop("cidrs", AAZListType, ".cidrs")
                destination.set_prop("countries", AAZListType, ".countries")
                destination.set_prop("feeds", AAZListType, ".feeds")
                destination.set_prop("fqdnLists", AAZListType, ".fqdn_lists")
                destination.set_prop("prefixLists", AAZListType, ".prefix_lists")

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

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

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

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

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

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

            source = _builder.get(".properties.source")
            if source is not None:
                source.set_prop("cidrs", AAZListType, ".cidrs")
                source.set_prop("countries", AAZListType, ".countries")
                source.set_prop("feeds", AAZListType, ".feeds")
                source.set_prop("prefixLists", AAZListType, ".prefix_lists")

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

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

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

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

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

            _elements = _builder.get(".properties.tags[]")
            if _elements is not None:
                _elements.set_prop("key", AAZStrType, ".key", typ_kwargs={"flags": {"required": True}})
                _elements.set_prop("value", AAZStrType, ".value", typ_kwargs={"flags": {"required": True}})

            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.action_type = AAZStrType(
                serialized_name="actionType",
            )
            properties.applications = AAZListType()
            properties.audit_comment = AAZStrType(
                serialized_name="auditComment",
            )
            properties.category = AAZObjectType()
            properties.decryption_rule_type = AAZStrType(
                serialized_name="decryptionRuleType",
            )
            properties.description = AAZStrType()
            properties.destination = AAZObjectType()
            properties.enable_logging = AAZStrType(
                serialized_name="enableLogging",
            )
            properties.etag = AAZStrType()
            properties.inbound_inspection_certificate = AAZStrType(
                serialized_name="inboundInspectionCertificate",
            )
            properties.negate_destination = AAZStrType(
                serialized_name="negateDestination",
            )
            properties.negate_source = AAZStrType(
                serialized_name="negateSource",
            )
            properties.priority = AAZIntType(
                flags={"read_only": True},
            )
            properties.protocol = AAZStrType()
            properties.protocol_port_list = AAZListType(
                serialized_name="protocolPortList",
            )
            properties.provisioning_state = AAZStrType(
                serialized_name="provisioningState",
            )
            properties.rule_name = AAZStrType(
                serialized_name="ruleName",
                flags={"required": True},
            )
            properties.rule_state = AAZStrType(
                serialized_name="ruleState",
            )
            properties.source = AAZObjectType()
            properties.tags = AAZListType()

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

            category = cls._schema_on_200_201.properties.category
            category.feeds = AAZListType(
                flags={"required": True},
            )
            category.url_custom = AAZListType(
                serialized_name="urlCustom",
                flags={"required": True},
            )

            feeds = cls._schema_on_200_201.properties.category.feeds
            feeds.Element = AAZStrType()

            url_custom = cls._schema_on_200_201.properties.category.url_custom
            url_custom.Element = AAZStrType()

            destination = cls._schema_on_200_201.properties.destination
            destination.cidrs = AAZListType()
            destination.countries = AAZListType()
            destination.feeds = AAZListType()
            destination.fqdn_lists = AAZListType(
                serialized_name="fqdnLists",
            )
            destination.prefix_lists = AAZListType(
                serialized_name="prefixLists",
            )

            cidrs = cls._schema_on_200_201.properties.destination.cidrs
            cidrs.Element = AAZStrType()

            countries = cls._schema_on_200_201.properties.destination.countries
            countries.Element = AAZStrType()

            feeds = cls._schema_on_200_201.properties.destination.feeds
            feeds.Element = AAZStrType()

            fqdn_lists = cls._schema_on_200_201.properties.destination.fqdn_lists
            fqdn_lists.Element = AAZStrType()

            prefix_lists = cls._schema_on_200_201.properties.destination.prefix_lists
            prefix_lists.Element = AAZStrType()

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

            source = cls._schema_on_200_201.properties.source
            source.cidrs = AAZListType()
            source.countries = AAZListType()
            source.feeds = AAZListType()
            source.prefix_lists = AAZListType(
                serialized_name="prefixLists",
            )

            cidrs = cls._schema_on_200_201.properties.source.cidrs
            cidrs.Element = AAZStrType()

            countries = cls._schema_on_200_201.properties.source.countries
            countries.Element = AAZStrType()

            feeds = cls._schema_on_200_201.properties.source.feeds
            feeds.Element = AAZStrType()

            prefix_lists = cls._schema_on_200_201.properties.source.prefix_lists
            prefix_lists.Element = AAZStrType()

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

            _element = cls._schema_on_200_201.properties.tags.Element
            _element.key = AAZStrType(
                flags={"required": True},
            )
            _element.value = AAZStrType(
                flags={"required": True},
            )

            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"]
