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

package com.azure.resourcemanager.resourceconnector.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.resourceconnector.fluent.AppliancesClient;
import com.azure.resourcemanager.resourceconnector.fluent.models.ApplianceGetTelemetryConfigResultInner;
import com.azure.resourcemanager.resourceconnector.fluent.models.ApplianceInner;
import com.azure.resourcemanager.resourceconnector.fluent.models.ApplianceListCredentialResultsInner;
import com.azure.resourcemanager.resourceconnector.fluent.models.ApplianceListKeysResultsInner;
import com.azure.resourcemanager.resourceconnector.fluent.models.ApplianceOperationInner;
import com.azure.resourcemanager.resourceconnector.fluent.models.UpgradeGraphInner;
import com.azure.resourcemanager.resourceconnector.models.Appliance;
import com.azure.resourcemanager.resourceconnector.models.ApplianceGetTelemetryConfigResult;
import com.azure.resourcemanager.resourceconnector.models.ApplianceListCredentialResults;
import com.azure.resourcemanager.resourceconnector.models.ApplianceListKeysResults;
import com.azure.resourcemanager.resourceconnector.models.ApplianceOperation;
import com.azure.resourcemanager.resourceconnector.models.Appliances;
import com.azure.resourcemanager.resourceconnector.models.UpgradeGraph;

public final class AppliancesImpl implements Appliances {
    private static final ClientLogger LOGGER = new ClientLogger(AppliancesImpl.class);

    private final AppliancesClient innerClient;

    private final com.azure.resourcemanager.resourceconnector.AppliancesManager serviceManager;

    public AppliancesImpl(
        AppliancesClient innerClient, com.azure.resourcemanager.resourceconnector.AppliancesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<ApplianceOperation> listOperations() {
        PagedIterable<ApplianceOperationInner> inner = this.serviceClient().listOperations();
        return Utils.mapPage(inner, inner1 -> new ApplianceOperationImpl(inner1, this.manager()));
    }

    public PagedIterable<ApplianceOperation> listOperations(Context context) {
        PagedIterable<ApplianceOperationInner> inner = this.serviceClient().listOperations(context);
        return Utils.mapPage(inner, inner1 -> new ApplianceOperationImpl(inner1, this.manager()));
    }

    public PagedIterable<Appliance> list() {
        PagedIterable<ApplianceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ApplianceImpl(inner1, this.manager()));
    }

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

    public Response<ApplianceGetTelemetryConfigResult> getTelemetryConfigWithResponse(Context context) {
        Response<ApplianceGetTelemetryConfigResultInner> inner =
            this.serviceClient().getTelemetryConfigWithResponse(context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplianceGetTelemetryConfigResultImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ApplianceGetTelemetryConfigResult getTelemetryConfig() {
        ApplianceGetTelemetryConfigResultInner inner = this.serviceClient().getTelemetryConfig();
        if (inner != null) {
            return new ApplianceGetTelemetryConfigResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public Appliance getByResourceGroup(String resourceGroupName, String resourceName) {
        ApplianceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, resourceName);
        if (inner != null) {
            return new ApplianceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<ApplianceListCredentialResults> listClusterUserCredentialWithResponse(
        String resourceGroupName, String resourceName, Context context) {
        Response<ApplianceListCredentialResultsInner> inner =
            this.serviceClient().listClusterUserCredentialWithResponse(resourceGroupName, resourceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplianceListCredentialResultsImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ApplianceListCredentialResults listClusterUserCredential(String resourceGroupName, String resourceName) {
        ApplianceListCredentialResultsInner inner =
            this.serviceClient().listClusterUserCredential(resourceGroupName, resourceName);
        if (inner != null) {
            return new ApplianceListCredentialResultsImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<ApplianceListKeysResults> listKeysWithResponse(
        String resourceGroupName, String resourceName, Context context) {
        Response<ApplianceListKeysResultsInner> inner =
            this.serviceClient().listKeysWithResponse(resourceGroupName, resourceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplianceListKeysResultsImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public ApplianceListKeysResults listKeys(String resourceGroupName, String resourceName) {
        ApplianceListKeysResultsInner inner = this.serviceClient().listKeys(resourceGroupName, resourceName);
        if (inner != null) {
            return new ApplianceListKeysResultsImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<UpgradeGraph> getUpgradeGraphWithResponse(
        String resourceGroupName, String resourceName, String upgradeGraph, Context context) {
        Response<UpgradeGraphInner> inner =
            this.serviceClient().getUpgradeGraphWithResponse(resourceGroupName, resourceName, upgradeGraph, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new UpgradeGraphImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public UpgradeGraph getUpgradeGraph(String resourceGroupName, String resourceName, String upgradeGraph) {
        UpgradeGraphInner inner = this.serviceClient().getUpgradeGraph(resourceGroupName, resourceName, upgradeGraph);
        if (inner != null) {
            return new UpgradeGraphImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.resourceconnector.AppliancesManager manager() {
        return this.serviceManager;
    }

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