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

package com.azure.resourcemanager.digitaltwins.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.digitaltwins.fluent.DigitalTwinsClient;
import com.azure.resourcemanager.digitaltwins.fluent.models.CheckNameResultInner;
import com.azure.resourcemanager.digitaltwins.fluent.models.DigitalTwinsDescriptionInner;
import com.azure.resourcemanager.digitaltwins.models.CheckNameRequest;
import com.azure.resourcemanager.digitaltwins.models.CheckNameResult;
import com.azure.resourcemanager.digitaltwins.models.DigitalTwins;
import com.azure.resourcemanager.digitaltwins.models.DigitalTwinsDescription;

public final class DigitalTwinsImpl implements DigitalTwins {
    private static final ClientLogger LOGGER = new ClientLogger(DigitalTwinsImpl.class);

    private final DigitalTwinsClient innerClient;

    private final com.azure.resourcemanager.digitaltwins.AzureDigitalTwinsManager serviceManager;

    public DigitalTwinsImpl(
        DigitalTwinsClient innerClient,
        com.azure.resourcemanager.digitaltwins.AzureDigitalTwinsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public DigitalTwinsDescription getByResourceGroup(String resourceGroupName, String resourceName) {
        DigitalTwinsDescriptionInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, resourceName);
        if (inner != null) {
            return new DigitalTwinsDescriptionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public DigitalTwinsDescription deleteByResourceGroup(String resourceGroupName, String resourceName) {
        DigitalTwinsDescriptionInner inner = this.serviceClient().delete(resourceGroupName, resourceName);
        if (inner != null) {
            return new DigitalTwinsDescriptionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public DigitalTwinsDescription delete(String resourceGroupName, String resourceName, Context context) {
        DigitalTwinsDescriptionInner inner = this.serviceClient().delete(resourceGroupName, resourceName, context);
        if (inner != null) {
            return new DigitalTwinsDescriptionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<DigitalTwinsDescription> list() {
        PagedIterable<DigitalTwinsDescriptionInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new DigitalTwinsDescriptionImpl(inner1, this.manager()));
    }

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

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

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

    public Response<CheckNameResult> checkNameAvailabilityWithResponse(
        String location, CheckNameRequest digitalTwinsInstanceCheckName, Context context) {
        Response<CheckNameResultInner> inner =
            this.serviceClient().checkNameAvailabilityWithResponse(location, digitalTwinsInstanceCheckName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CheckNameResultImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CheckNameResult checkNameAvailability(String location, CheckNameRequest digitalTwinsInstanceCheckName) {
        CheckNameResultInner inner =
            this.serviceClient().checkNameAvailability(location, digitalTwinsInstanceCheckName);
        if (inner != null) {
            return new CheckNameResultImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.digitaltwins.AzureDigitalTwinsManager manager() {
        return this.serviceManager;
    }

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