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

package com.azure.resourcemanager.synapse.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.synapse.fluent.IpFirewallRulesClient;
import com.azure.resourcemanager.synapse.fluent.models.IpFirewallRuleInfoInner;
import com.azure.resourcemanager.synapse.fluent.models.ReplaceAllFirewallRulesOperationResponseInner;
import com.azure.resourcemanager.synapse.models.IpFirewallRuleInfo;
import com.azure.resourcemanager.synapse.models.IpFirewallRules;
import com.azure.resourcemanager.synapse.models.ReplaceAllFirewallRulesOperationResponse;
import com.azure.resourcemanager.synapse.models.ReplaceAllIpFirewallRulesRequest;

public final class IpFirewallRulesImpl implements IpFirewallRules {
    private static final ClientLogger LOGGER = new ClientLogger(IpFirewallRulesImpl.class);

    private final IpFirewallRulesClient innerClient;

    private final com.azure.resourcemanager.synapse.SynapseManager serviceManager;

    public IpFirewallRulesImpl(
        IpFirewallRulesClient innerClient, com.azure.resourcemanager.synapse.SynapseManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<IpFirewallRuleInfo> listByWorkspace(String resourceGroupName, String workspaceName) {
        PagedIterable<IpFirewallRuleInfoInner> inner =
            this.serviceClient().listByWorkspace(resourceGroupName, workspaceName);
        return Utils.mapPage(inner, inner1 -> new IpFirewallRuleInfoImpl(inner1, this.manager()));
    }

    public PagedIterable<IpFirewallRuleInfo> listByWorkspace(
        String resourceGroupName, String workspaceName, Context context) {
        PagedIterable<IpFirewallRuleInfoInner> inner =
            this.serviceClient().listByWorkspace(resourceGroupName, workspaceName, context);
        return Utils.mapPage(inner, inner1 -> new IpFirewallRuleInfoImpl(inner1, this.manager()));
    }

    public IpFirewallRuleInfo delete(String resourceGroupName, String workspaceName, String ruleName) {
        IpFirewallRuleInfoInner inner = this.serviceClient().delete(resourceGroupName, workspaceName, ruleName);
        if (inner != null) {
            return new IpFirewallRuleInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public IpFirewallRuleInfo delete(String resourceGroupName, String workspaceName, String ruleName, Context context) {
        IpFirewallRuleInfoInner inner =
            this.serviceClient().delete(resourceGroupName, workspaceName, ruleName, context);
        if (inner != null) {
            return new IpFirewallRuleInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<IpFirewallRuleInfo> getWithResponse(
        String resourceGroupName, String workspaceName, String ruleName, Context context) {
        Response<IpFirewallRuleInfoInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, workspaceName, ruleName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IpFirewallRuleInfoImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public IpFirewallRuleInfo get(String resourceGroupName, String workspaceName, String ruleName) {
        IpFirewallRuleInfoInner inner = this.serviceClient().get(resourceGroupName, workspaceName, ruleName);
        if (inner != null) {
            return new IpFirewallRuleInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public ReplaceAllFirewallRulesOperationResponse replaceAll(
        String resourceGroupName, String workspaceName, ReplaceAllIpFirewallRulesRequest request) {
        ReplaceAllFirewallRulesOperationResponseInner inner =
            this.serviceClient().replaceAll(resourceGroupName, workspaceName, request);
        if (inner != null) {
            return new ReplaceAllFirewallRulesOperationResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public ReplaceAllFirewallRulesOperationResponse replaceAll(
        String resourceGroupName, String workspaceName, ReplaceAllIpFirewallRulesRequest request, Context context) {
        ReplaceAllFirewallRulesOperationResponseInner inner =
            this.serviceClient().replaceAll(resourceGroupName, workspaceName, request, context);
        if (inner != null) {
            return new ReplaceAllFirewallRulesOperationResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public IpFirewallRuleInfo 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleName = Utils.getValueFromIdByName(id, "firewallRules");
        if (ruleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'firewallRules'.", id)));
        }
        return this.getWithResponse(resourceGroupName, workspaceName, ruleName, Context.NONE).getValue();
    }

    public Response<IpFirewallRuleInfo> 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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleName = Utils.getValueFromIdByName(id, "firewallRules");
        if (ruleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'firewallRules'.", id)));
        }
        return this.getWithResponse(resourceGroupName, workspaceName, ruleName, context);
    }

    public IpFirewallRuleInfo deleteById(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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleName = Utils.getValueFromIdByName(id, "firewallRules");
        if (ruleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'firewallRules'.", id)));
        }
        return this.delete(resourceGroupName, workspaceName, ruleName, Context.NONE);
    }

    public IpFirewallRuleInfo deleteByIdWithResponse(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 workspaceName = Utils.getValueFromIdByName(id, "workspaces");
        if (workspaceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'workspaces'.", id)));
        }
        String ruleName = Utils.getValueFromIdByName(id, "firewallRules");
        if (ruleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'firewallRules'.", id)));
        }
        return this.delete(resourceGroupName, workspaceName, ruleName, context);
    }

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

    private com.azure.resourcemanager.synapse.SynapseManager manager() {
        return this.serviceManager;
    }

    public IpFirewallRuleInfoImpl define(String name) {
        return new IpFirewallRuleInfoImpl(name, this.manager());
    }
}
