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

package com.azure.resourcemanager.desktopvirtualization.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.desktopvirtualization.fluent.models.ApplicationGroupInner;
import com.azure.resourcemanager.desktopvirtualization.models.ApplicationGroup;
import com.azure.resourcemanager.desktopvirtualization.models.ApplicationGroupPatch;
import com.azure.resourcemanager.desktopvirtualization.models.ApplicationGroupType;
import com.azure.resourcemanager.desktopvirtualization.models.ResourceModelWithAllowedPropertySetIdentity;
import com.azure.resourcemanager.desktopvirtualization.models.ResourceModelWithAllowedPropertySetPlan;
import com.azure.resourcemanager.desktopvirtualization.models.ResourceModelWithAllowedPropertySetSku;
import java.util.Collections;
import java.util.Map;

public final class ApplicationGroupImpl
    implements ApplicationGroup, ApplicationGroup.Definition, ApplicationGroup.Update {
    private ApplicationGroupInner innerObject;

    private final com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager;

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

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

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

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

    public Map<String, String> tags() {
        Map<String, String> inner = this.innerModel().tags();
        if (inner != null) {
            return Collections.unmodifiableMap(inner);
        } else {
            return Collections.emptyMap();
        }
    }

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

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

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

    public ResourceModelWithAllowedPropertySetIdentity identity() {
        return this.innerModel().identity();
    }

    public ResourceModelWithAllowedPropertySetSku sku() {
        return this.innerModel().sku();
    }

    public ResourceModelWithAllowedPropertySetPlan plan() {
        return this.innerModel().plan();
    }

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

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

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

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

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

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

    public ApplicationGroupType applicationGroupType() {
        return this.innerModel().applicationGroupType();
    }

    public Boolean cloudPcResource() {
        return this.innerModel().cloudPcResource();
    }

    public Region region() {
        return Region.fromName(this.regionName());
    }

    public String regionName() {
        return this.location();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String applicationGroupName;

    private ApplicationGroupPatch updateApplicationGroup;

    public ApplicationGroupImpl withExistingResourceGroup(String resourceGroupName) {
        this.resourceGroupName = resourceGroupName;
        return this;
    }

    public ApplicationGroup create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApplicationGroups()
                .createOrUpdateWithResponse(resourceGroupName, applicationGroupName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public ApplicationGroup create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApplicationGroups()
                .createOrUpdateWithResponse(resourceGroupName, applicationGroupName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ApplicationGroupImpl(
        String name, com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager) {
        this.innerObject = new ApplicationGroupInner();
        this.serviceManager = serviceManager;
        this.applicationGroupName = name;
    }

    public ApplicationGroupImpl update() {
        this.updateApplicationGroup = new ApplicationGroupPatch();
        return this;
    }

    public ApplicationGroup apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApplicationGroups()
                .updateWithResponse(resourceGroupName, applicationGroupName, updateApplicationGroup, Context.NONE)
                .getValue();
        return this;
    }

    public ApplicationGroup apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApplicationGroups()
                .updateWithResponse(resourceGroupName, applicationGroupName, updateApplicationGroup, context)
                .getValue();
        return this;
    }

    ApplicationGroupImpl(
        ApplicationGroupInner innerObject,
        com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.applicationGroupName = Utils.getValueFromIdByName(innerObject.id(), "applicationGroups");
    }

    public ApplicationGroup refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApplicationGroups()
                .getByResourceGroupWithResponse(resourceGroupName, applicationGroupName, Context.NONE)
                .getValue();
        return this;
    }

    public ApplicationGroup refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getApplicationGroups()
                .getByResourceGroupWithResponse(resourceGroupName, applicationGroupName, context)
                .getValue();
        return this;
    }

    public ApplicationGroupImpl withRegion(Region location) {
        this.innerModel().withLocation(location.toString());
        return this;
    }

    public ApplicationGroupImpl withRegion(String location) {
        this.innerModel().withLocation(location);
        return this;
    }

    public ApplicationGroupImpl withHostPoolArmPath(String hostPoolArmPath) {
        this.innerModel().withHostPoolArmPath(hostPoolArmPath);
        return this;
    }

    public ApplicationGroupImpl withApplicationGroupType(ApplicationGroupType applicationGroupType) {
        this.innerModel().withApplicationGroupType(applicationGroupType);
        return this;
    }

    public ApplicationGroupImpl withTags(Map<String, String> tags) {
        if (isInCreateMode()) {
            this.innerModel().withTags(tags);
            return this;
        } else {
            this.updateApplicationGroup.withTags(tags);
            return this;
        }
    }

    public ApplicationGroupImpl withManagedBy(String managedBy) {
        this.innerModel().withManagedBy(managedBy);
        return this;
    }

    public ApplicationGroupImpl withKind(String kind) {
        this.innerModel().withKind(kind);
        return this;
    }

    public ApplicationGroupImpl withIdentity(ResourceModelWithAllowedPropertySetIdentity identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public ApplicationGroupImpl withSku(ResourceModelWithAllowedPropertySetSku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public ApplicationGroupImpl withPlan(ResourceModelWithAllowedPropertySetPlan plan) {
        this.innerModel().withPlan(plan);
        return this;
    }

    public ApplicationGroupImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.innerModel().withDescription(description);
            return this;
        } else {
            this.updateApplicationGroup.withDescription(description);
            return this;
        }
    }

    public ApplicationGroupImpl withFriendlyName(String friendlyName) {
        if (isInCreateMode()) {
            this.innerModel().withFriendlyName(friendlyName);
            return this;
        } else {
            this.updateApplicationGroup.withFriendlyName(friendlyName);
            return this;
        }
    }

    private boolean isInCreateMode() {
        return this.innerModel().id() == null;
    }
}
