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

package com.azure.resourcemanager.resourcemover.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.resourcemover.fluent.MoveResourcesClient;
import com.azure.resourcemanager.resourcemover.fluent.models.MoveResourceInner;
import com.azure.resourcemanager.resourcemover.fluent.models.OperationStatusInner;
import com.azure.resourcemanager.resourcemover.models.MoveResource;
import com.azure.resourcemanager.resourcemover.models.MoveResources;
import com.azure.resourcemanager.resourcemover.models.OperationStatus;

public final class MoveResourcesImpl implements MoveResources {
    private static final ClientLogger LOGGER = new ClientLogger(MoveResourcesImpl.class);

    private final MoveResourcesClient innerClient;

    private final com.azure.resourcemanager.resourcemover.ResourceMoverManager serviceManager;

    public MoveResourcesImpl(
        MoveResourcesClient innerClient, com.azure.resourcemanager.resourcemover.ResourceMoverManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<MoveResource> list(String resourceGroupName, String moveCollectionName) {
        PagedIterable<MoveResourceInner> inner = this.serviceClient().list(resourceGroupName, moveCollectionName);
        return Utils.mapPage(inner, inner1 -> new MoveResourceImpl(inner1, this.manager()));
    }

    public PagedIterable<MoveResource> list(
        String resourceGroupName, String moveCollectionName, String filter, Context context) {
        PagedIterable<MoveResourceInner> inner =
            this.serviceClient().list(resourceGroupName, moveCollectionName, filter, context);
        return Utils.mapPage(inner, inner1 -> new MoveResourceImpl(inner1, this.manager()));
    }

    public OperationStatus delete(String resourceGroupName, String moveCollectionName, String moveResourceName) {
        OperationStatusInner inner =
            this.serviceClient().delete(resourceGroupName, moveCollectionName, moveResourceName);
        if (inner != null) {
            return new OperationStatusImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public OperationStatus delete(
        String resourceGroupName, String moveCollectionName, String moveResourceName, Context context) {
        OperationStatusInner inner =
            this.serviceClient().delete(resourceGroupName, moveCollectionName, moveResourceName, context);
        if (inner != null) {
            return new OperationStatusImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<MoveResource> getWithResponse(
        String resourceGroupName, String moveCollectionName, String moveResourceName, Context context) {
        Response<MoveResourceInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, moveCollectionName, moveResourceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new MoveResourceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public MoveResource get(String resourceGroupName, String moveCollectionName, String moveResourceName) {
        MoveResourceInner inner = this.serviceClient().get(resourceGroupName, moveCollectionName, moveResourceName);
        if (inner != null) {
            return new MoveResourceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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

    private com.azure.resourcemanager.resourcemover.ResourceMoverManager manager() {
        return this.serviceManager;
    }

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