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

package com.azure.resourcemanager.cognitiveservices.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.cognitiveservices.fluent.CommitmentPlansClient;
import com.azure.resourcemanager.cognitiveservices.fluent.models.CommitmentPlanAccountAssociationInner;
import com.azure.resourcemanager.cognitiveservices.fluent.models.CommitmentPlanInner;
import com.azure.resourcemanager.cognitiveservices.models.CommitmentPlan;
import com.azure.resourcemanager.cognitiveservices.models.CommitmentPlanAccountAssociation;
import com.azure.resourcemanager.cognitiveservices.models.CommitmentPlans;

public final class CommitmentPlansImpl implements CommitmentPlans {
    private static final ClientLogger LOGGER = new ClientLogger(CommitmentPlansImpl.class);

    private final CommitmentPlansClient innerClient;

    private final com.azure.resourcemanager.cognitiveservices.CognitiveServicesManager serviceManager;

    public CommitmentPlansImpl(
        CommitmentPlansClient innerClient,
        com.azure.resourcemanager.cognitiveservices.CognitiveServicesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<CommitmentPlan> list(String resourceGroupName, String accountName) {
        PagedIterable<CommitmentPlanInner> inner = this.serviceClient().list(resourceGroupName, accountName);
        return Utils.mapPage(inner, inner1 -> new CommitmentPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<CommitmentPlan> list(String resourceGroupName, String accountName, Context context) {
        PagedIterable<CommitmentPlanInner> inner = this.serviceClient().list(resourceGroupName, accountName, context);
        return Utils.mapPage(inner, inner1 -> new CommitmentPlanImpl(inner1, this.manager()));
    }

    public Response<CommitmentPlan> getWithResponse(
        String resourceGroupName, String accountName, String commitmentPlanName, Context context) {
        Response<CommitmentPlanInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, accountName, commitmentPlanName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CommitmentPlanImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CommitmentPlan get(String resourceGroupName, String accountName, String commitmentPlanName) {
        CommitmentPlanInner inner = this.serviceClient().get(resourceGroupName, accountName, commitmentPlanName);
        if (inner != null) {
            return new CommitmentPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<CommitmentPlan> createOrUpdateWithResponse(
        String resourceGroupName,
        String accountName,
        String commitmentPlanName,
        CommitmentPlanInner commitmentPlan,
        Context context) {
        Response<CommitmentPlanInner> inner =
            this
                .serviceClient()
                .createOrUpdateWithResponse(
                    resourceGroupName, accountName, commitmentPlanName, commitmentPlan, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CommitmentPlanImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CommitmentPlan createOrUpdate(
        String resourceGroupName, String accountName, String commitmentPlanName, CommitmentPlanInner commitmentPlan) {
        CommitmentPlanInner inner =
            this.serviceClient().createOrUpdate(resourceGroupName, accountName, commitmentPlanName, commitmentPlan);
        if (inner != null) {
            return new CommitmentPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String accountName, String commitmentPlanName) {
        this.serviceClient().delete(resourceGroupName, accountName, commitmentPlanName);
    }

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

    public void deletePlan(String resourceGroupName, String commitmentPlanName) {
        this.serviceClient().deletePlan(resourceGroupName, commitmentPlanName);
    }

    public void deletePlan(String resourceGroupName, String commitmentPlanName, Context context) {
        this.serviceClient().deletePlan(resourceGroupName, commitmentPlanName, context);
    }

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

    public CommitmentPlan getByResourceGroup(String resourceGroupName, String commitmentPlanName) {
        CommitmentPlanInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, commitmentPlanName);
        if (inner != null) {
            return new CommitmentPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public PagedIterable<CommitmentPlan> listPlansBySubscription() {
        PagedIterable<CommitmentPlanInner> inner = this.serviceClient().listPlansBySubscription();
        return Utils.mapPage(inner, inner1 -> new CommitmentPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<CommitmentPlan> listPlansBySubscription(Context context) {
        PagedIterable<CommitmentPlanInner> inner = this.serviceClient().listPlansBySubscription(context);
        return Utils.mapPage(inner, inner1 -> new CommitmentPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<CommitmentPlanAccountAssociation> listAssociations(
        String resourceGroupName, String commitmentPlanName) {
        PagedIterable<CommitmentPlanAccountAssociationInner> inner =
            this.serviceClient().listAssociations(resourceGroupName, commitmentPlanName);
        return Utils.mapPage(inner, inner1 -> new CommitmentPlanAccountAssociationImpl(inner1, this.manager()));
    }

    public PagedIterable<CommitmentPlanAccountAssociation> listAssociations(
        String resourceGroupName, String commitmentPlanName, Context context) {
        PagedIterable<CommitmentPlanAccountAssociationInner> inner =
            this.serviceClient().listAssociations(resourceGroupName, commitmentPlanName, context);
        return Utils.mapPage(inner, inner1 -> new CommitmentPlanAccountAssociationImpl(inner1, this.manager()));
    }

    public Response<CommitmentPlanAccountAssociation> getAssociationWithResponse(
        String resourceGroupName, String commitmentPlanName, String commitmentPlanAssociationName, Context context) {
        Response<CommitmentPlanAccountAssociationInner> inner =
            this
                .serviceClient()
                .getAssociationWithResponse(
                    resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CommitmentPlanAccountAssociationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CommitmentPlanAccountAssociation getAssociation(
        String resourceGroupName, String commitmentPlanName, String commitmentPlanAssociationName) {
        CommitmentPlanAccountAssociationInner inner =
            this.serviceClient().getAssociation(resourceGroupName, commitmentPlanName, commitmentPlanAssociationName);
        if (inner != null) {
            return new CommitmentPlanAccountAssociationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void deleteAssociation(
        String resourceGroupName, String commitmentPlanName, String commitmentPlanAssociationName) {
        this.serviceClient().deleteAssociation(resourceGroupName, commitmentPlanName, commitmentPlanAssociationName);
    }

    public void deleteAssociation(
        String resourceGroupName, String commitmentPlanName, String commitmentPlanAssociationName, Context context) {
        this
            .serviceClient()
            .deleteAssociation(resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, context);
    }

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

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

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

    public Response<CommitmentPlanAccountAssociation> getAssociationByIdWithResponse(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 commitmentPlanName = Utils.getValueFromIdByName(id, "commitmentPlans");
        if (commitmentPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'commitmentPlans'.", id)));
        }
        String commitmentPlanAssociationName = Utils.getValueFromIdByName(id, "accountAssociations");
        if (commitmentPlanAssociationName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'accountAssociations'.", id)));
        }
        return this
            .getAssociationWithResponse(resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, context);
    }

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

    public void deletePlanByIdWithResponse(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 commitmentPlanName = Utils.getValueFromIdByName(id, "commitmentPlans");
        if (commitmentPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'commitmentPlans'.", id)));
        }
        this.deletePlan(resourceGroupName, commitmentPlanName, context);
    }

    public void deleteAssociationById(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 commitmentPlanName = Utils.getValueFromIdByName(id, "commitmentPlans");
        if (commitmentPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'commitmentPlans'.", id)));
        }
        String commitmentPlanAssociationName = Utils.getValueFromIdByName(id, "accountAssociations");
        if (commitmentPlanAssociationName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'accountAssociations'.", id)));
        }
        this.deleteAssociation(resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, Context.NONE);
    }

    public void deleteAssociationByIdWithResponse(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 commitmentPlanName = Utils.getValueFromIdByName(id, "commitmentPlans");
        if (commitmentPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'commitmentPlans'.", id)));
        }
        String commitmentPlanAssociationName = Utils.getValueFromIdByName(id, "accountAssociations");
        if (commitmentPlanAssociationName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'accountAssociations'.", id)));
        }
        this.deleteAssociation(resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, context);
    }

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

    private com.azure.resourcemanager.cognitiveservices.CognitiveServicesManager manager() {
        return this.serviceManager;
    }

    public CommitmentPlanImpl definePlan(String name) {
        return new CommitmentPlanImpl(name, this.manager());
    }

    public CommitmentPlanAccountAssociationImpl defineAssociation(String name) {
        return new CommitmentPlanAccountAssociationImpl(name, this.manager());
    }
}
