// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
// Code generated by Microsoft (R) AutoRest Code Generator.

package com.azure.resourcemanager.mariadb.implementation;

import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.mariadb.fluent.ServerSecurityAlertPoliciesClient;
import com.azure.resourcemanager.mariadb.fluent.models.ServerSecurityAlertPolicyInner;
import com.azure.resourcemanager.mariadb.models.SecurityAlertPolicyName;
import com.azure.resourcemanager.mariadb.models.ServerSecurityAlertPolicies;
import com.azure.resourcemanager.mariadb.models.ServerSecurityAlertPolicy;

public final class ServerSecurityAlertPoliciesImpl implements ServerSecurityAlertPolicies {
    private static final ClientLogger LOGGER = new ClientLogger(ServerSecurityAlertPoliciesImpl.class);

    private final ServerSecurityAlertPoliciesClient innerClient;

    private final com.azure.resourcemanager.mariadb.MariaDBManager serviceManager;

    public ServerSecurityAlertPoliciesImpl(
        ServerSecurityAlertPoliciesClient innerClient,
        com.azure.resourcemanager.mariadb.MariaDBManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ServerSecurityAlertPolicy> getWithResponse(
        String resourceGroupName, String serverName, SecurityAlertPolicyName securityAlertPolicyName, Context context) {
        Response<ServerSecurityAlertPolicyInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, serverName, securityAlertPolicyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ServerSecurityAlertPolicyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ServerSecurityAlertPolicy get(
        String resourceGroupName, String serverName, SecurityAlertPolicyName securityAlertPolicyName) {
        ServerSecurityAlertPolicyInner inner =
            this.serviceClient().get(resourceGroupName, serverName, securityAlertPolicyName);
        if (inner != null) {
            return new ServerSecurityAlertPolicyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<ServerSecurityAlertPolicy> listByServer(String resourceGroupName, String serverName) {
        PagedIterable<ServerSecurityAlertPolicyInner> inner =
            this.serviceClient().listByServer(resourceGroupName, serverName);
        return Utils.mapPage(inner, inner1 -> new ServerSecurityAlertPolicyImpl(inner1, this.manager()));
    }

    public PagedIterable<ServerSecurityAlertPolicy> listByServer(
        String resourceGroupName, String serverName, Context context) {
        PagedIterable<ServerSecurityAlertPolicyInner> inner =
            this.serviceClient().listByServer(resourceGroupName, serverName, context);
        return Utils.mapPage(inner, inner1 -> new ServerSecurityAlertPolicyImpl(inner1, this.manager()));
    }

    public ServerSecurityAlertPolicy getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String serverName = Utils.getValueFromIdByName(id, "servers");
        if (serverName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'servers'.", id)));
        }
        String securityAlertPolicyNameLocal = Utils.getValueFromIdByName(id, "securityAlertPolicies");
        if (securityAlertPolicyNameLocal == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'securityAlertPolicies'.",
                                id)));
        }
        SecurityAlertPolicyName securityAlertPolicyName =
            SecurityAlertPolicyName.fromString(securityAlertPolicyNameLocal);
        return this.getWithResponse(resourceGroupName, serverName, securityAlertPolicyName, Context.NONE).getValue();
    }

    public Response<ServerSecurityAlertPolicy> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String serverName = Utils.getValueFromIdByName(id, "servers");
        if (serverName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'servers'.", id)));
        }
        String securityAlertPolicyNameLocal = Utils.getValueFromIdByName(id, "securityAlertPolicies");
        if (securityAlertPolicyNameLocal == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'securityAlertPolicies'.",
                                id)));
        }
        SecurityAlertPolicyName securityAlertPolicyName =
            SecurityAlertPolicyName.fromString(securityAlertPolicyNameLocal);
        return this.getWithResponse(resourceGroupName, serverName, securityAlertPolicyName, context);
    }

    private ServerSecurityAlertPoliciesClient serviceClient() {
        return this.innerClient;
    }

    private com.azure.resourcemanager.mariadb.MariaDBManager manager() {
        return this.serviceManager;
    }

    public ServerSecurityAlertPolicyImpl define(SecurityAlertPolicyName name) {
        return new ServerSecurityAlertPolicyImpl(name, this.manager());
    }
}
