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

package com.azure.resourcemanager.confidentialledger.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.confidentialledger.fluent.ManagedCcfsClient;
import com.azure.resourcemanager.confidentialledger.fluent.models.ManagedCcfInner;
import com.azure.resourcemanager.confidentialledger.models.ManagedCcf;
import com.azure.resourcemanager.confidentialledger.models.ManagedCcfs;

public final class ManagedCcfsImpl implements ManagedCcfs {
    private static final ClientLogger LOGGER = new ClientLogger(ManagedCcfsImpl.class);

    private final ManagedCcfsClient innerClient;

    private final com.azure.resourcemanager.confidentialledger.ConfidentialLedgerManager serviceManager;

    public ManagedCcfsImpl(
        ManagedCcfsClient innerClient,
        com.azure.resourcemanager.confidentialledger.ConfidentialLedgerManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public ManagedCcf getByResourceGroup(String resourceGroupName, String appName) {
        ManagedCcfInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, appName);
        if (inner != null) {
            return new ManagedCcfImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public void update(String resourceGroupName, String appName, ManagedCcfInner managedCcf) {
        this.serviceClient().update(resourceGroupName, appName, managedCcf);
    }

    public void update(String resourceGroupName, String appName, ManagedCcfInner managedCcf, Context context) {
        this.serviceClient().update(resourceGroupName, appName, managedCcf, context);
    }

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

    public PagedIterable<ManagedCcf> listByResourceGroup(String resourceGroupName, String filter, Context context) {
        PagedIterable<ManagedCcfInner> inner =
            this.serviceClient().listByResourceGroup(resourceGroupName, filter, context);
        return Utils.mapPage(inner, inner1 -> new ManagedCcfImpl(inner1, this.manager()));
    }

    public PagedIterable<ManagedCcf> list() {
        PagedIterable<ManagedCcfInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ManagedCcfImpl(inner1, this.manager()));
    }

    public PagedIterable<ManagedCcf> list(String filter, Context context) {
        PagedIterable<ManagedCcfInner> inner = this.serviceClient().list(filter, context);
        return Utils.mapPage(inner, inner1 -> new ManagedCcfImpl(inner1, this.manager()));
    }

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

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

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

    private com.azure.resourcemanager.confidentialledger.ConfidentialLedgerManager manager() {
        return this.serviceManager;
    }

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