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

package com.azure.resourcemanager.managednetworkfabric.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.managednetworkfabric.fluent.models.IpExtendedCommunityInner;
import com.azure.resourcemanager.managednetworkfabric.models.AdministrativeState;
import com.azure.resourcemanager.managednetworkfabric.models.ConfigurationState;
import com.azure.resourcemanager.managednetworkfabric.models.IpExtendedCommunity;
import com.azure.resourcemanager.managednetworkfabric.models.IpExtendedCommunityPatch;
import com.azure.resourcemanager.managednetworkfabric.models.IpExtendedCommunityRule;
import com.azure.resourcemanager.managednetworkfabric.models.ProvisioningState;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class IpExtendedCommunityImpl
    implements IpExtendedCommunity, IpExtendedCommunity.Definition, IpExtendedCommunity.Update {
    private IpExtendedCommunityInner innerObject;

    private final com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public ConfigurationState configurationState() {
        return this.innerModel().configurationState();
    }

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

    public AdministrativeState administrativeState() {
        return this.innerModel().administrativeState();
    }

    public List<IpExtendedCommunityRule> ipExtendedCommunityRules() {
        List<IpExtendedCommunityRule> inner = this.innerModel().ipExtendedCommunityRules();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String ipExtendedCommunityName;

    private IpExtendedCommunityPatch updateBody;

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

    public IpExtendedCommunity create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIpExtendedCommunities()
                .create(resourceGroupName, ipExtendedCommunityName, this.innerModel(), Context.NONE);
        return this;
    }

    public IpExtendedCommunity create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIpExtendedCommunities()
                .create(resourceGroupName, ipExtendedCommunityName, this.innerModel(), context);
        return this;
    }

    IpExtendedCommunityImpl(
        String name, com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = new IpExtendedCommunityInner();
        this.serviceManager = serviceManager;
        this.ipExtendedCommunityName = name;
    }

    public IpExtendedCommunityImpl update() {
        this.updateBody = new IpExtendedCommunityPatch();
        return this;
    }

    public IpExtendedCommunity apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIpExtendedCommunities()
                .update(resourceGroupName, ipExtendedCommunityName, updateBody, Context.NONE);
        return this;
    }

    public IpExtendedCommunity apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIpExtendedCommunities()
                .update(resourceGroupName, ipExtendedCommunityName, updateBody, context);
        return this;
    }

    IpExtendedCommunityImpl(
        IpExtendedCommunityInner innerObject,
        com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.ipExtendedCommunityName = Utils.getValueFromIdByName(innerObject.id(), "ipExtendedCommunities");
    }

    public IpExtendedCommunity refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIpExtendedCommunities()
                .getByResourceGroupWithResponse(resourceGroupName, ipExtendedCommunityName, Context.NONE)
                .getValue();
        return this;
    }

    public IpExtendedCommunity refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getIpExtendedCommunities()
                .getByResourceGroupWithResponse(resourceGroupName, ipExtendedCommunityName, context)
                .getValue();
        return this;
    }

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

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

    public IpExtendedCommunityImpl withIpExtendedCommunityRules(
        List<IpExtendedCommunityRule> ipExtendedCommunityRules) {
        if (isInCreateMode()) {
            this.innerModel().withIpExtendedCommunityRules(ipExtendedCommunityRules);
            return this;
        } else {
            this.updateBody.withIpExtendedCommunityRules(ipExtendedCommunityRules);
            return this;
        }
    }

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

    public IpExtendedCommunityImpl withAnnotation(String annotation) {
        if (isInCreateMode()) {
            this.innerModel().withAnnotation(annotation);
            return this;
        } else {
            this.updateBody.withAnnotation(annotation);
            return this;
        }
    }

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