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

package com.azure.resourcemanager.servicenetworking.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.servicenetworking.fluent.models.TrafficControllerInner;
import com.azure.resourcemanager.servicenetworking.models.ProvisioningState;
import com.azure.resourcemanager.servicenetworking.models.ResourceId;
import com.azure.resourcemanager.servicenetworking.models.TrafficController;
import com.azure.resourcemanager.servicenetworking.models.TrafficControllerUpdate;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class TrafficControllerImpl
    implements TrafficController, TrafficController.Definition, TrafficController.Update {
    private TrafficControllerInner innerObject;

    private final com.azure.resourcemanager.servicenetworking.TrafficControllerManager 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 List<String> configurationEndpoints() {
        List<String> inner = this.innerModel().configurationEndpoints();
        if (inner != null) {
            return Collections.unmodifiableList(inner);
        } else {
            return Collections.emptyList();
        }
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.servicenetworking.TrafficControllerManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String trafficControllerName;

    private TrafficControllerUpdate updateProperties;

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

    public TrafficController create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTrafficControllerInterfaces()
                .createOrUpdate(resourceGroupName, trafficControllerName, this.innerModel(), Context.NONE);
        return this;
    }

    public TrafficController create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTrafficControllerInterfaces()
                .createOrUpdate(resourceGroupName, trafficControllerName, this.innerModel(), context);
        return this;
    }

    TrafficControllerImpl(
        String name, com.azure.resourcemanager.servicenetworking.TrafficControllerManager serviceManager) {
        this.innerObject = new TrafficControllerInner();
        this.serviceManager = serviceManager;
        this.trafficControllerName = name;
    }

    public TrafficControllerImpl update() {
        this.updateProperties = new TrafficControllerUpdate();
        return this;
    }

    public TrafficController apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTrafficControllerInterfaces()
                .updateWithResponse(resourceGroupName, trafficControllerName, updateProperties, Context.NONE)
                .getValue();
        return this;
    }

    public TrafficController apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTrafficControllerInterfaces()
                .updateWithResponse(resourceGroupName, trafficControllerName, updateProperties, context)
                .getValue();
        return this;
    }

    TrafficControllerImpl(
        TrafficControllerInner innerObject,
        com.azure.resourcemanager.servicenetworking.TrafficControllerManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.trafficControllerName = Utils.getValueFromIdByName(innerObject.id(), "trafficControllers");
    }

    public TrafficController refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTrafficControllerInterfaces()
                .getByResourceGroupWithResponse(resourceGroupName, trafficControllerName, Context.NONE)
                .getValue();
        return this;
    }

    public TrafficController refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getTrafficControllerInterfaces()
                .getByResourceGroupWithResponse(resourceGroupName, trafficControllerName, context)
                .getValue();
        return this;
    }

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

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

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

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