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

package com.azure.resourcemanager.peering.implementation;

import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.peering.fluent.models.PeeringServiceInner;
import com.azure.resourcemanager.peering.models.PeeringService;
import com.azure.resourcemanager.peering.models.PeeringServiceSku;
import com.azure.resourcemanager.peering.models.ProvisioningState;
import com.azure.resourcemanager.peering.models.ResourceTags;
import java.util.Collections;
import java.util.Map;

public final class PeeringServiceImpl implements PeeringService, PeeringService.Definition, PeeringService.Update {
    private PeeringServiceInner innerObject;

    private final com.azure.resourcemanager.peering.PeeringManager serviceManager;

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

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

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

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

    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 peeringServiceLocation() {
        return this.innerModel().peeringServiceLocation();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.peering.PeeringManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String peeringServiceName;

    private ResourceTags updateTags;

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

    public PeeringService create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPeeringServices()
                .createOrUpdateWithResponse(resourceGroupName, peeringServiceName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public PeeringService create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPeeringServices()
                .createOrUpdateWithResponse(resourceGroupName, peeringServiceName, this.innerModel(), context)
                .getValue();
        return this;
    }

    PeeringServiceImpl(String name, com.azure.resourcemanager.peering.PeeringManager serviceManager) {
        this.innerObject = new PeeringServiceInner();
        this.serviceManager = serviceManager;
        this.peeringServiceName = name;
    }

    public PeeringServiceImpl update() {
        this.updateTags = new ResourceTags();
        return this;
    }

    public PeeringService apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPeeringServices()
                .updateWithResponse(resourceGroupName, peeringServiceName, updateTags, Context.NONE)
                .getValue();
        return this;
    }

    public PeeringService apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPeeringServices()
                .updateWithResponse(resourceGroupName, peeringServiceName, updateTags, context)
                .getValue();
        return this;
    }

    PeeringServiceImpl(
        PeeringServiceInner innerObject, com.azure.resourcemanager.peering.PeeringManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.peeringServiceName = Utils.getValueFromIdByName(innerObject.id(), "peeringServices");
    }

    public PeeringService refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPeeringServices()
                .getByResourceGroupWithResponse(resourceGroupName, peeringServiceName, Context.NONE)
                .getValue();
        return this;
    }

    public PeeringService refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPeeringServices()
                .getByResourceGroupWithResponse(resourceGroupName, peeringServiceName, context)
                .getValue();
        return this;
    }

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

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

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

    public PeeringServiceImpl withSku(PeeringServiceSku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public PeeringServiceImpl withPeeringServiceLocation(String peeringServiceLocation) {
        this.innerModel().withPeeringServiceLocation(peeringServiceLocation);
        return this;
    }

    public PeeringServiceImpl withPeeringServiceProvider(String peeringServiceProvider) {
        this.innerModel().withPeeringServiceProvider(peeringServiceProvider);
        return this;
    }

    public PeeringServiceImpl withProviderPrimaryPeeringLocation(String providerPrimaryPeeringLocation) {
        this.innerModel().withProviderPrimaryPeeringLocation(providerPrimaryPeeringLocation);
        return this;
    }

    public PeeringServiceImpl withProviderBackupPeeringLocation(String providerBackupPeeringLocation) {
        this.innerModel().withProviderBackupPeeringLocation(providerBackupPeeringLocation);
        return this;
    }

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