// 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.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.cognitiveservices.fluent.models.CommitmentPlanAccountAssociationInner;
import com.azure.resourcemanager.cognitiveservices.models.CommitmentPlanAccountAssociation;

public final class CommitmentPlanAccountAssociationImpl
    implements CommitmentPlanAccountAssociation,
        CommitmentPlanAccountAssociation.Definition,
        CommitmentPlanAccountAssociation.Update {
    private CommitmentPlanAccountAssociationInner innerObject;

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

    public String id() {
        return this.innerModel().id();
    }

    public String name() {
        return this.innerModel().name();
    }

    public String type() {
        return this.innerModel().type();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

    public String etag() {
        return this.innerModel().etag();
    }

    public String accountId() {
        return this.innerModel().accountId();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

    public CommitmentPlanAccountAssociationInner innerModel() {
        return this.innerObject;
    }

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

    private String resourceGroupName;

    private String commitmentPlanName;

    private String commitmentPlanAssociationName;

    public CommitmentPlanAccountAssociationImpl withExistingCommitmentPlan(
        String resourceGroupName, String commitmentPlanName) {
        this.resourceGroupName = resourceGroupName;
        this.commitmentPlanName = commitmentPlanName;
        return this;
    }

    public CommitmentPlanAccountAssociation create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCommitmentPlans()
                .createOrUpdateAssociation(
                    resourceGroupName,
                    commitmentPlanName,
                    commitmentPlanAssociationName,
                    this.innerModel(),
                    Context.NONE);
        return this;
    }

    public CommitmentPlanAccountAssociation create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCommitmentPlans()
                .createOrUpdateAssociation(
                    resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, this.innerModel(), context);
        return this;
    }

    CommitmentPlanAccountAssociationImpl(
        String name, com.azure.resourcemanager.cognitiveservices.CognitiveServicesManager serviceManager) {
        this.innerObject = new CommitmentPlanAccountAssociationInner();
        this.serviceManager = serviceManager;
        this.commitmentPlanAssociationName = name;
    }

    public CommitmentPlanAccountAssociationImpl update() {
        return this;
    }

    public CommitmentPlanAccountAssociation apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCommitmentPlans()
                .createOrUpdateAssociation(
                    resourceGroupName,
                    commitmentPlanName,
                    commitmentPlanAssociationName,
                    this.innerModel(),
                    Context.NONE);
        return this;
    }

    public CommitmentPlanAccountAssociation apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCommitmentPlans()
                .createOrUpdateAssociation(
                    resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, this.innerModel(), context);
        return this;
    }

    CommitmentPlanAccountAssociationImpl(
        CommitmentPlanAccountAssociationInner innerObject,
        com.azure.resourcemanager.cognitiveservices.CognitiveServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.commitmentPlanName = Utils.getValueFromIdByName(innerObject.id(), "commitmentPlans");
        this.commitmentPlanAssociationName = Utils.getValueFromIdByName(innerObject.id(), "accountAssociations");
    }

    public CommitmentPlanAccountAssociation refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCommitmentPlans()
                .getAssociationWithResponse(
                    resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, Context.NONE)
                .getValue();
        return this;
    }

    public CommitmentPlanAccountAssociation refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCommitmentPlans()
                .getAssociationWithResponse(
                    resourceGroupName, commitmentPlanName, commitmentPlanAssociationName, context)
                .getValue();
        return this;
    }

    public CommitmentPlanAccountAssociationImpl withAccountId(String accountId) {
        this.innerModel().withAccountId(accountId);
        return this;
    }
}
