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

package com.azure.resourcemanager.servicefabric.implementation;

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.servicefabric.fluent.ApplicationTypeVersionsClient;
import com.azure.resourcemanager.servicefabric.fluent.models.ApplicationTypeVersionResourceInner;
import com.azure.resourcemanager.servicefabric.fluent.models.ApplicationTypeVersionResourceListInner;
import com.azure.resourcemanager.servicefabric.models.ApplicationTypeVersionResource;
import com.azure.resourcemanager.servicefabric.models.ApplicationTypeVersionResourceList;
import com.azure.resourcemanager.servicefabric.models.ApplicationTypeVersions;

public final class ApplicationTypeVersionsImpl implements ApplicationTypeVersions {
    private static final ClientLogger LOGGER = new ClientLogger(ApplicationTypeVersionsImpl.class);

    private final ApplicationTypeVersionsClient innerClient;

    private final com.azure.resourcemanager.servicefabric.ServiceFabricManager serviceManager;

    public ApplicationTypeVersionsImpl(
        ApplicationTypeVersionsClient innerClient,
        com.azure.resourcemanager.servicefabric.ServiceFabricManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<ApplicationTypeVersionResource> getWithResponse(
        String resourceGroupName, String clusterName, String applicationTypeName, String version, Context context) {
        Response<ApplicationTypeVersionResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, clusterName, applicationTypeName, version, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplicationTypeVersionResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ApplicationTypeVersionResource get(
        String resourceGroupName, String clusterName, String applicationTypeName, String version) {
        ApplicationTypeVersionResourceInner inner =
            this.serviceClient().get(resourceGroupName, clusterName, applicationTypeName, version);
        if (inner != null) {
            return new ApplicationTypeVersionResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String clusterName, String applicationTypeName, String version) {
        this.serviceClient().delete(resourceGroupName, clusterName, applicationTypeName, version);
    }

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

    public Response<ApplicationTypeVersionResourceList> listWithResponse(
        String resourceGroupName, String clusterName, String applicationTypeName, Context context) {
        Response<ApplicationTypeVersionResourceListInner> inner =
            this.serviceClient().listWithResponse(resourceGroupName, clusterName, applicationTypeName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplicationTypeVersionResourceListImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ApplicationTypeVersionResourceList list(
        String resourceGroupName, String clusterName, String applicationTypeName) {
        ApplicationTypeVersionResourceListInner inner =
            this.serviceClient().list(resourceGroupName, clusterName, applicationTypeName);
        if (inner != null) {
            return new ApplicationTypeVersionResourceListImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.servicefabric.ServiceFabricManager manager() {
        return this.serviceManager;
    }

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