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

package com.azure.resourcemanager.avs.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.avs.fluent.PlacementPoliciesClient;
import com.azure.resourcemanager.avs.fluent.models.PlacementPolicyInner;
import com.azure.resourcemanager.avs.models.PlacementPolicies;
import com.azure.resourcemanager.avs.models.PlacementPolicy;
import com.fasterxml.jackson.annotation.JsonIgnore;

public final class PlacementPoliciesImpl implements PlacementPolicies {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(PlacementPoliciesImpl.class);

    private final PlacementPoliciesClient innerClient;

    private final com.azure.resourcemanager.avs.AvsManager serviceManager;

    public PlacementPoliciesImpl(
        PlacementPoliciesClient innerClient, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<PlacementPolicy> list(String resourceGroupName, String privateCloudName, String clusterName) {
        PagedIterable<PlacementPolicyInner> inner =
            this.serviceClient().list(resourceGroupName, privateCloudName, clusterName);
        return Utils.mapPage(inner, inner1 -> new PlacementPolicyImpl(inner1, this.manager()));
    }

    public PagedIterable<PlacementPolicy> list(
        String resourceGroupName, String privateCloudName, String clusterName, Context context) {
        PagedIterable<PlacementPolicyInner> inner =
            this.serviceClient().list(resourceGroupName, privateCloudName, clusterName, context);
        return Utils.mapPage(inner, inner1 -> new PlacementPolicyImpl(inner1, this.manager()));
    }

    public PlacementPolicy get(
        String resourceGroupName, String privateCloudName, String clusterName, String placementPolicyName) {
        PlacementPolicyInner inner =
            this.serviceClient().get(resourceGroupName, privateCloudName, clusterName, placementPolicyName);
        if (inner != null) {
            return new PlacementPolicyImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<PlacementPolicy> getWithResponse(
        String resourceGroupName,
        String privateCloudName,
        String clusterName,
        String placementPolicyName,
        Context context) {
        Response<PlacementPolicyInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceGroupName, privateCloudName, clusterName, placementPolicyName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new PlacementPolicyImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(
        String resourceGroupName, String privateCloudName, String clusterName, String placementPolicyName) {
        this.serviceClient().delete(resourceGroupName, privateCloudName, clusterName, placementPolicyName);
    }

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

    public PlacementPolicy 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 privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String clusterName = Utils.getValueFromIdByName(id, "clusters");
        if (clusterName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'clusters'.", id)));
        }
        String placementPolicyName = Utils.getValueFromIdByName(id, "placementPolicies");
        if (placementPolicyName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'placementPolicies'.", id)));
        }
        return this
            .getWithResponse(resourceGroupName, privateCloudName, clusterName, placementPolicyName, Context.NONE)
            .getValue();
    }

    public Response<PlacementPolicy> 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 privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String clusterName = Utils.getValueFromIdByName(id, "clusters");
        if (clusterName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'clusters'.", id)));
        }
        String placementPolicyName = Utils.getValueFromIdByName(id, "placementPolicies");
        if (placementPolicyName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'placementPolicies'.", id)));
        }
        return this.getWithResponse(resourceGroupName, privateCloudName, clusterName, placementPolicyName, 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 privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String clusterName = Utils.getValueFromIdByName(id, "clusters");
        if (clusterName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'clusters'.", id)));
        }
        String placementPolicyName = Utils.getValueFromIdByName(id, "placementPolicies");
        if (placementPolicyName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'placementPolicies'.", id)));
        }
        this.delete(resourceGroupName, privateCloudName, clusterName, placementPolicyName, 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 privateCloudName = Utils.getValueFromIdByName(id, "privateClouds");
        if (privateCloudName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'privateClouds'.", id)));
        }
        String clusterName = Utils.getValueFromIdByName(id, "clusters");
        if (clusterName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'clusters'.", id)));
        }
        String placementPolicyName = Utils.getValueFromIdByName(id, "placementPolicies");
        if (placementPolicyName == null) {
            throw logger
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'placementPolicies'.", id)));
        }
        this.delete(resourceGroupName, privateCloudName, clusterName, placementPolicyName, context);
    }

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

    private com.azure.resourcemanager.avs.AvsManager manager() {
        return this.serviceManager;
    }

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