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

package com.azure.resourcemanager.delegatednetwork.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.delegatednetwork.fluent.models.DelegatedSubnetInner;
import com.azure.resourcemanager.delegatednetwork.models.ControllerDetails;
import com.azure.resourcemanager.delegatednetwork.models.DelegatedSubnet;
import com.azure.resourcemanager.delegatednetwork.models.DelegatedSubnetState;
import com.azure.resourcemanager.delegatednetwork.models.ResourceUpdateParameters;
import com.azure.resourcemanager.delegatednetwork.models.SubnetDetails;
import java.util.Collections;
import java.util.Map;

public final class DelegatedSubnetImpl implements DelegatedSubnet, DelegatedSubnet.Definition, DelegatedSubnet.Update {
    private DelegatedSubnetInner innerObject;

    private final com.azure.resourcemanager.delegatednetwork.DelegatedNetworkManager 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 resourceGuid() {
        return this.innerModel().resourceGuid();
    }

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

    public SubnetDetails subnetDetails() {
        return this.innerModel().subnetDetails();
    }

    public ControllerDetails controllerDetails() {
        return this.innerModel().controllerDetails();
    }

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.delegatednetwork.DelegatedNetworkManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private ResourceUpdateParameters updateParameters;

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

    public DelegatedSubnet create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDelegatedSubnetServices()
                .putDetails(resourceGroupName, resourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public DelegatedSubnet create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDelegatedSubnetServices()
                .putDetails(resourceGroupName, resourceName, this.innerModel(), context);
        return this;
    }

    DelegatedSubnetImpl(
        String name, com.azure.resourcemanager.delegatednetwork.DelegatedNetworkManager serviceManager) {
        this.innerObject = new DelegatedSubnetInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
    }

    public DelegatedSubnetImpl update() {
        this.updateParameters = new ResourceUpdateParameters();
        return this;
    }

    public DelegatedSubnet apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDelegatedSubnetServices()
                .patchDetails(resourceGroupName, resourceName, updateParameters, Context.NONE);
        return this;
    }

    public DelegatedSubnet apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDelegatedSubnetServices()
                .patchDetails(resourceGroupName, resourceName, updateParameters, context);
        return this;
    }

    DelegatedSubnetImpl(
        DelegatedSubnetInner innerObject,
        com.azure.resourcemanager.delegatednetwork.DelegatedNetworkManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "delegatedSubnets");
    }

    public DelegatedSubnet refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDelegatedSubnetServices()
                .getByResourceGroupWithResponse(resourceGroupName, resourceName, Context.NONE)
                .getValue();
        return this;
    }

    public DelegatedSubnet refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getDelegatedSubnetServices()
                .getByResourceGroupWithResponse(resourceGroupName, resourceName, context)
                .getValue();
        return this;
    }

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

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

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

    public DelegatedSubnetImpl withSubnetDetails(SubnetDetails subnetDetails) {
        this.innerModel().withSubnetDetails(subnetDetails);
        return this;
    }

    public DelegatedSubnetImpl withControllerDetails(ControllerDetails controllerDetails) {
        this.innerModel().withControllerDetails(controllerDetails);
        return this;
    }

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