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

package com.azure.resourcemanager.devcenter.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devcenter.fluent.models.DevCenterInner;
import com.azure.resourcemanager.devcenter.models.DevCenter;
import com.azure.resourcemanager.devcenter.models.DevCenterUpdate;
import com.azure.resourcemanager.devcenter.models.ManagedServiceIdentity;
import com.azure.resourcemanager.devcenter.models.ProvisioningState;
import java.util.Collections;
import java.util.Map;

public final class DevCenterImpl implements DevCenter, DevCenter.Definition, DevCenter.Update {
    private DevCenterInner innerObject;

    private final com.azure.resourcemanager.devcenter.DevCenterManager 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 ManagedServiceIdentity identity() {
        return this.innerModel().identity();
    }

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

    public ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.devcenter.DevCenterManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String devCenterName;

    private DevCenterUpdate updateBody;

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

    public DevCenter create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevCenters()
                .createOrUpdate(resourceGroupName, devCenterName, this.innerModel(), Context.NONE);
        return this;
    }

    public DevCenter create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevCenters()
                .createOrUpdate(resourceGroupName, devCenterName, this.innerModel(), context);
        return this;
    }

    DevCenterImpl(String name, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerObject = new DevCenterInner();
        this.serviceManager = serviceManager;
        this.devCenterName = name;
    }

    public DevCenterImpl update() {
        this.updateBody = new DevCenterUpdate();
        return this;
    }

    public DevCenter apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevCenters()
                .update(resourceGroupName, devCenterName, updateBody, Context.NONE);
        return this;
    }

    public DevCenter apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevCenters()
                .update(resourceGroupName, devCenterName, updateBody, context);
        return this;
    }

    DevCenterImpl(DevCenterInner innerObject, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.devCenterName = Utils.getValueFromIdByName(innerObject.id(), "devcenters");
    }

    public DevCenter refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevCenters()
                .getByResourceGroupWithResponse(resourceGroupName, devCenterName, Context.NONE)
                .getValue();
        return this;
    }

    public DevCenter refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDevCenters()
                .getByResourceGroupWithResponse(resourceGroupName, devCenterName, context)
                .getValue();
        return this;
    }

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

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

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

    public DevCenterImpl withIdentity(ManagedServiceIdentity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateBody.withIdentity(identity);
            return this;
        }
    }

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