// 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.DevicesClient;
import com.azure.resourcemanager.sphere.fluent.models.DeviceInner;
import com.azure.resourcemanager.sphere.fluent.models.SignedCapabilityImageResponseInner;
import com.azure.resourcemanager.sphere.models.Device;
import com.azure.resourcemanager.sphere.models.Devices;
import com.azure.resourcemanager.sphere.models.GenerateCapabilityImageRequest;
import com.azure.resourcemanager.sphere.models.SignedCapabilityImageResponse;

public final class DevicesImpl implements Devices {
    private static final ClientLogger LOGGER = new ClientLogger(DevicesImpl.class);

    private final DevicesClient innerClient;

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

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

    public PagedIterable<Device> listByDeviceGroup(
        String resourceGroupName, String catalogName, String productName, String deviceGroupName) {
        PagedIterable<DeviceInner> inner =
            this.serviceClient().listByDeviceGroup(resourceGroupName, catalogName, productName, deviceGroupName);
        return Utils.mapPage(inner, inner1 -> new DeviceImpl(inner1, this.manager()));
    }

    public PagedIterable<Device> listByDeviceGroup(
        String resourceGroupName, String catalogName, String productName, String deviceGroupName, Context context) {
        PagedIterable<DeviceInner> inner =
            this
                .serviceClient()
                .listByDeviceGroup(resourceGroupName, catalogName, productName, deviceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new DeviceImpl(inner1, this.manager()));
    }

    public Response<Device> getWithResponse(
        String resourceGroupName,
        String catalogName,
        String productName,
        String deviceGroupName,
        String deviceName,
        Context context) {
        Response<DeviceInner> inner =
            this
                .serviceClient()
                .getWithResponse(resourceGroupName, catalogName, productName, deviceGroupName, deviceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new DeviceImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Device get(
        String resourceGroupName, String catalogName, String productName, String deviceGroupName, String deviceName) {
        DeviceInner inner =
            this.serviceClient().get(resourceGroupName, catalogName, productName, deviceGroupName, deviceName);
        if (inner != null) {
            return new DeviceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(
        String resourceGroupName, String catalogName, String productName, String deviceGroupName, String deviceName) {
        this.serviceClient().delete(resourceGroupName, catalogName, productName, deviceGroupName, deviceName);
    }

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

    public SignedCapabilityImageResponse generateCapabilityImage(
        String resourceGroupName,
        String catalogName,
        String productName,
        String deviceGroupName,
        String deviceName,
        GenerateCapabilityImageRequest generateDeviceCapabilityRequest) {
        SignedCapabilityImageResponseInner inner =
            this
                .serviceClient()
                .generateCapabilityImage(
                    resourceGroupName,
                    catalogName,
                    productName,
                    deviceGroupName,
                    deviceName,
                    generateDeviceCapabilityRequest);
        if (inner != null) {
            return new SignedCapabilityImageResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public SignedCapabilityImageResponse generateCapabilityImage(
        String resourceGroupName,
        String catalogName,
        String productName,
        String deviceGroupName,
        String deviceName,
        GenerateCapabilityImageRequest generateDeviceCapabilityRequest,
        Context context) {
        SignedCapabilityImageResponseInner inner =
            this
                .serviceClient()
                .generateCapabilityImage(
                    resourceGroupName,
                    catalogName,
                    productName,
                    deviceGroupName,
                    deviceName,
                    generateDeviceCapabilityRequest,
                    context);
        if (inner != null) {
            return new SignedCapabilityImageResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

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

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