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

package com.azure.resourcemanager.oep.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.oep.fluent.EnergyServicesClient;
import com.azure.resourcemanager.oep.fluent.models.EnergyServiceInner;
import com.azure.resourcemanager.oep.models.EnergyService;
import com.azure.resourcemanager.oep.models.EnergyServices;
import com.fasterxml.jackson.annotation.JsonIgnore;

public final class EnergyServicesImpl implements EnergyServices {
    @JsonIgnore private final ClientLogger logger = new ClientLogger(EnergyServicesImpl.class);

    private final EnergyServicesClient innerClient;

    private final com.azure.resourcemanager.oep.OepManager serviceManager;

    public EnergyServicesImpl(
        EnergyServicesClient innerClient, com.azure.resourcemanager.oep.OepManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

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

    public PagedIterable<EnergyService> list() {
        PagedIterable<EnergyServiceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new EnergyServiceImpl(inner1, this.manager()));
    }

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

    public EnergyService getByResourceGroup(String resourceGroupName, String resourceName) {
        EnergyServiceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, resourceName);
        if (inner != null) {
            return new EnergyServiceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void deleteByResourceGroup(String resourceGroupName, String resourceName) {
        this.serviceClient().delete(resourceGroupName, resourceName);
    }

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

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

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

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

    private com.azure.resourcemanager.oep.OepManager manager() {
        return this.serviceManager;
    }

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