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

package com.azure.resourcemanager.paloaltonetworks.ngfw.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.paloaltonetworks.ngfw.fluent.FirewallsClient;
import com.azure.resourcemanager.paloaltonetworks.ngfw.fluent.models.FirewallResourceInner;
import com.azure.resourcemanager.paloaltonetworks.ngfw.fluent.models.GlobalRulestackInfoInner;
import com.azure.resourcemanager.paloaltonetworks.ngfw.fluent.models.LogSettingsInner;
import com.azure.resourcemanager.paloaltonetworks.ngfw.fluent.models.SupportInfoInner;
import com.azure.resourcemanager.paloaltonetworks.ngfw.models.FirewallResource;
import com.azure.resourcemanager.paloaltonetworks.ngfw.models.Firewalls;
import com.azure.resourcemanager.paloaltonetworks.ngfw.models.GlobalRulestackInfo;
import com.azure.resourcemanager.paloaltonetworks.ngfw.models.LogSettings;
import com.azure.resourcemanager.paloaltonetworks.ngfw.models.SupportInfo;

public final class FirewallsImpl implements Firewalls {
    private static final ClientLogger LOGGER = new ClientLogger(FirewallsImpl.class);

    private final FirewallsClient innerClient;

    private final com.azure.resourcemanager.paloaltonetworks.ngfw.PaloAltoNetworksNgfwManager serviceManager;

    public FirewallsImpl(
        FirewallsClient innerClient,
        com.azure.resourcemanager.paloaltonetworks.ngfw.PaloAltoNetworksNgfwManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<FirewallResource> list() {
        PagedIterable<FirewallResourceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new FirewallResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<FirewallResource> list(Context context) {
        PagedIterable<FirewallResourceInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new FirewallResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<FirewallResource> listByResourceGroup(String resourceGroupName) {
        PagedIterable<FirewallResourceInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new FirewallResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<FirewallResource> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<FirewallResourceInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new FirewallResourceImpl(inner1, this.manager()));
    }

    public Response<FirewallResource> getByResourceGroupWithResponse(
        String resourceGroupName, String firewallName, Context context) {
        Response<FirewallResourceInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, firewallName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new FirewallResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public FirewallResource getByResourceGroup(String resourceGroupName, String firewallName) {
        FirewallResourceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, firewallName);
        if (inner != null) {
            return new FirewallResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void deleteByResourceGroup(String resourceGroupName, String firewallName) {
        this.serviceClient().delete(resourceGroupName, firewallName);
    }

    public void delete(String resourceGroupName, String firewallName, Context context) {
        this.serviceClient().delete(resourceGroupName, firewallName, context);
    }

    public Response<GlobalRulestackInfo> getGlobalRulestackWithResponse(
        String resourceGroupName, String firewallName, Context context) {
        Response<GlobalRulestackInfoInner> inner =
            this.serviceClient().getGlobalRulestackWithResponse(resourceGroupName, firewallName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new GlobalRulestackInfoImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public GlobalRulestackInfo getGlobalRulestack(String resourceGroupName, String firewallName) {
        GlobalRulestackInfoInner inner = this.serviceClient().getGlobalRulestack(resourceGroupName, firewallName);
        if (inner != null) {
            return new GlobalRulestackInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<LogSettings> getLogProfileWithResponse(
        String resourceGroupName, String firewallName, Context context) {
        Response<LogSettingsInner> inner =
            this.serviceClient().getLogProfileWithResponse(resourceGroupName, firewallName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new LogSettingsImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public LogSettings getLogProfile(String resourceGroupName, String firewallName) {
        LogSettingsInner inner = this.serviceClient().getLogProfile(resourceGroupName, firewallName);
        if (inner != null) {
            return new LogSettingsImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<SupportInfo> getSupportInfoWithResponse(
        String resourceGroupName, String firewallName, String email, Context context) {
        Response<SupportInfoInner> inner =
            this.serviceClient().getSupportInfoWithResponse(resourceGroupName, firewallName, email, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new SupportInfoImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public SupportInfo getSupportInfo(String resourceGroupName, String firewallName) {
        SupportInfoInner inner = this.serviceClient().getSupportInfo(resourceGroupName, firewallName);
        if (inner != null) {
            return new SupportInfoImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Void> saveLogProfileWithResponse(
        String resourceGroupName, String firewallName, LogSettingsInner logSettings, Context context) {
        return this.serviceClient().saveLogProfileWithResponse(resourceGroupName, firewallName, logSettings, context);
    }

    public void saveLogProfile(String resourceGroupName, String firewallName) {
        this.serviceClient().saveLogProfile(resourceGroupName, firewallName);
    }

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

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

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

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

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

    private com.azure.resourcemanager.paloaltonetworks.ngfw.PaloAltoNetworksNgfwManager manager() {
        return this.serviceManager;
    }

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