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

package com.azure.resourcemanager.avs.implementation;

import com.azure.core.util.Context;
import com.azure.resourcemanager.avs.fluent.models.PlacementPolicyInner;
import com.azure.resourcemanager.avs.models.AffinityStrength;
import com.azure.resourcemanager.avs.models.AzureHybridBenefitType;
import com.azure.resourcemanager.avs.models.PlacementPolicy;
import com.azure.resourcemanager.avs.models.PlacementPolicyProperties;
import com.azure.resourcemanager.avs.models.PlacementPolicyState;
import com.azure.resourcemanager.avs.models.PlacementPolicyUpdate;
import java.util.List;

public final class PlacementPolicyImpl implements PlacementPolicy, PlacementPolicy.Definition, PlacementPolicy.Update {
    private PlacementPolicyInner innerObject;

    private final com.azure.resourcemanager.avs.AvsManager serviceManager;

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

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

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

    public PlacementPolicyProperties properties() {
        return this.innerModel().properties();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.avs.AvsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String privateCloudName;

    private String clusterName;

    private String placementPolicyName;

    private PlacementPolicyUpdate updatePlacementPolicyUpdate;

    public PlacementPolicyImpl withExistingCluster(
        String resourceGroupName, String privateCloudName, String clusterName) {
        this.resourceGroupName = resourceGroupName;
        this.privateCloudName = privateCloudName;
        this.clusterName = clusterName;
        return this;
    }

    public PlacementPolicy create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPlacementPolicies()
                .createOrUpdate(
                    resourceGroupName,
                    privateCloudName,
                    clusterName,
                    placementPolicyName,
                    this.innerModel(),
                    Context.NONE);
        return this;
    }

    public PlacementPolicy create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPlacementPolicies()
                .createOrUpdate(
                    resourceGroupName, privateCloudName, clusterName, placementPolicyName, this.innerModel(), context);
        return this;
    }

    PlacementPolicyImpl(String name, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerObject = new PlacementPolicyInner();
        this.serviceManager = serviceManager;
        this.placementPolicyName = name;
    }

    public PlacementPolicyImpl update() {
        this.updatePlacementPolicyUpdate = new PlacementPolicyUpdate();
        return this;
    }

    public PlacementPolicy apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPlacementPolicies()
                .update(
                    resourceGroupName,
                    privateCloudName,
                    clusterName,
                    placementPolicyName,
                    updatePlacementPolicyUpdate,
                    Context.NONE);
        return this;
    }

    public PlacementPolicy apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPlacementPolicies()
                .update(
                    resourceGroupName,
                    privateCloudName,
                    clusterName,
                    placementPolicyName,
                    updatePlacementPolicyUpdate,
                    context);
        return this;
    }

    PlacementPolicyImpl(PlacementPolicyInner innerObject, com.azure.resourcemanager.avs.AvsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.privateCloudName = Utils.getValueFromIdByName(innerObject.id(), "privateClouds");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
        this.placementPolicyName = Utils.getValueFromIdByName(innerObject.id(), "placementPolicies");
    }

    public PlacementPolicy refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPlacementPolicies()
                .getWithResponse(resourceGroupName, privateCloudName, clusterName, placementPolicyName, Context.NONE)
                .getValue();
        return this;
    }

    public PlacementPolicy refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPlacementPolicies()
                .getWithResponse(resourceGroupName, privateCloudName, clusterName, placementPolicyName, context)
                .getValue();
        return this;
    }

    public PlacementPolicyImpl withProperties(PlacementPolicyProperties properties) {
        this.innerModel().withProperties(properties);
        return this;
    }

    public PlacementPolicyImpl withState(PlacementPolicyState state) {
        this.updatePlacementPolicyUpdate.withState(state);
        return this;
    }

    public PlacementPolicyImpl withVmMembers(List<String> vmMembers) {
        this.updatePlacementPolicyUpdate.withVmMembers(vmMembers);
        return this;
    }

    public PlacementPolicyImpl withHostMembers(List<String> hostMembers) {
        this.updatePlacementPolicyUpdate.withHostMembers(hostMembers);
        return this;
    }

    public PlacementPolicyImpl withAffinityStrength(AffinityStrength affinityStrength) {
        this.updatePlacementPolicyUpdate.withAffinityStrength(affinityStrength);
        return this;
    }

    public PlacementPolicyImpl withAzureHybridBenefitType(AzureHybridBenefitType azureHybridBenefitType) {
        this.updatePlacementPolicyUpdate.withAzureHybridBenefitType(azureHybridBenefitType);
        return this;
    }
}
