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

package com.azure.resourcemanager.sphere.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.sphere.fluent.ImagesClient;
import com.azure.resourcemanager.sphere.fluent.models.ImageInner;
import com.azure.resourcemanager.sphere.models.Image;
import com.azure.resourcemanager.sphere.models.Images;

public final class ImagesImpl implements Images {
    private static final ClientLogger LOGGER = new ClientLogger(ImagesImpl.class);

    private final ImagesClient innerClient;

    private final com.azure.resourcemanager.sphere.AzureSphereManager serviceManager;

    public ImagesImpl(ImagesClient innerClient, com.azure.resourcemanager.sphere.AzureSphereManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Image> listByCatalog(String resourceGroupName, String catalogName) {
        PagedIterable<ImageInner> inner = this.serviceClient().listByCatalog(resourceGroupName, catalogName);
        return Utils.mapPage(inner, inner1 -> new ImageImpl(inner1, this.manager()));
    }

    public PagedIterable<Image> listByCatalog(
        String resourceGroupName,
        String catalogName,
        String filter,
        Integer top,
        Integer skip,
        Integer maxpagesize,
        Context context) {
        PagedIterable<ImageInner> inner =
            this.serviceClient().listByCatalog(resourceGroupName, catalogName, filter, top, skip, maxpagesize, context);
        return Utils.mapPage(inner, inner1 -> new ImageImpl(inner1, this.manager()));
    }

    public Response<Image> getWithResponse(
        String resourceGroupName, String catalogName, String imageName, Context context) {
        Response<ImageInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, catalogName, imageName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ImageImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Image get(String resourceGroupName, String catalogName, String imageName) {
        ImageInner inner = this.serviceClient().get(resourceGroupName, catalogName, imageName);
        if (inner != null) {
            return new ImageImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String catalogName, String imageName) {
        this.serviceClient().delete(resourceGroupName, catalogName, imageName);
    }

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

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

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

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

    private com.azure.resourcemanager.sphere.AzureSphereManager manager() {
        return this.serviceManager;
    }

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