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

package com.azure.resourcemanager.storagepool.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.storagepool.fluent.IscsiTargetsClient;
import com.azure.resourcemanager.storagepool.fluent.models.IscsiTargetInner;
import com.azure.resourcemanager.storagepool.models.IscsiTarget;
import com.azure.resourcemanager.storagepool.models.IscsiTargets;

public final class IscsiTargetsImpl implements IscsiTargets {
    private static final ClientLogger LOGGER = new ClientLogger(IscsiTargetsImpl.class);

    private final IscsiTargetsClient innerClient;

    private final com.azure.resourcemanager.storagepool.StoragePoolManager serviceManager;

    public IscsiTargetsImpl(
        IscsiTargetsClient innerClient, com.azure.resourcemanager.storagepool.StoragePoolManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<IscsiTarget> listByDiskPool(String resourceGroupName, String diskPoolName) {
        PagedIterable<IscsiTargetInner> inner = this.serviceClient().listByDiskPool(resourceGroupName, diskPoolName);
        return Utils.mapPage(inner, inner1 -> new IscsiTargetImpl(inner1, this.manager()));
    }

    public PagedIterable<IscsiTarget> listByDiskPool(String resourceGroupName, String diskPoolName, Context context) {
        PagedIterable<IscsiTargetInner> inner =
            this.serviceClient().listByDiskPool(resourceGroupName, diskPoolName, context);
        return Utils.mapPage(inner, inner1 -> new IscsiTargetImpl(inner1, this.manager()));
    }

    public void delete(String resourceGroupName, String diskPoolName, String iscsiTargetName) {
        this.serviceClient().delete(resourceGroupName, diskPoolName, iscsiTargetName);
    }

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

    public Response<IscsiTarget> getWithResponse(
        String resourceGroupName, String diskPoolName, String iscsiTargetName, Context context) {
        Response<IscsiTargetInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, diskPoolName, iscsiTargetName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new IscsiTargetImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public IscsiTarget get(String resourceGroupName, String diskPoolName, String iscsiTargetName) {
        IscsiTargetInner inner = this.serviceClient().get(resourceGroupName, diskPoolName, iscsiTargetName);
        if (inner != null) {
            return new IscsiTargetImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    private com.azure.resourcemanager.storagepool.StoragePoolManager manager() {
        return this.serviceManager;
    }

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