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

package com.azure.resourcemanager.mobilenetwork.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.mobilenetwork.fluent.models.MobileNetworkInner;
import com.azure.resourcemanager.mobilenetwork.models.MobileNetwork;
import com.azure.resourcemanager.mobilenetwork.models.PlmnId;
import com.azure.resourcemanager.mobilenetwork.models.ProvisioningState;
import com.azure.resourcemanager.mobilenetwork.models.TagsObject;
import java.util.Collections;
import java.util.Map;

public final class MobileNetworkImpl implements MobileNetwork, MobileNetwork.Definition, MobileNetwork.Update {
    private MobileNetworkInner innerObject;

    private final com.azure.resourcemanager.mobilenetwork.MobileNetworkManager 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 ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

    public PlmnId publicLandMobileNetworkIdentifier() {
        return this.innerModel().publicLandMobileNetworkIdentifier();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.mobilenetwork.MobileNetworkManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String mobileNetworkName;

    private TagsObject updateParameters;

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

    public MobileNetwork create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMobileNetworks()
                .createOrUpdate(resourceGroupName, mobileNetworkName, this.innerModel(), Context.NONE);
        return this;
    }

    public MobileNetwork create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMobileNetworks()
                .createOrUpdate(resourceGroupName, mobileNetworkName, this.innerModel(), context);
        return this;
    }

    MobileNetworkImpl(String name, com.azure.resourcemanager.mobilenetwork.MobileNetworkManager serviceManager) {
        this.innerObject = new MobileNetworkInner();
        this.serviceManager = serviceManager;
        this.mobileNetworkName = name;
    }

    public MobileNetworkImpl update() {
        this.updateParameters = new TagsObject();
        return this;
    }

    public MobileNetwork apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMobileNetworks()
                .updateTagsWithResponse(resourceGroupName, mobileNetworkName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public MobileNetwork apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMobileNetworks()
                .updateTagsWithResponse(resourceGroupName, mobileNetworkName, updateParameters, context)
                .getValue();
        return this;
    }

    MobileNetworkImpl(
        MobileNetworkInner innerObject, com.azure.resourcemanager.mobilenetwork.MobileNetworkManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.mobileNetworkName = Utils.getValueFromIdByName(innerObject.id(), "mobileNetworks");
    }

    public MobileNetwork refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMobileNetworks()
                .getByResourceGroupWithResponse(resourceGroupName, mobileNetworkName, Context.NONE)
                .getValue();
        return this;
    }

    public MobileNetwork refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getMobileNetworks()
                .getByResourceGroupWithResponse(resourceGroupName, mobileNetworkName, context)
                .getValue();
        return this;
    }

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

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

    public MobileNetworkImpl withPublicLandMobileNetworkIdentifier(PlmnId publicLandMobileNetworkIdentifier) {
        this.innerModel().withPublicLandMobileNetworkIdentifier(publicLandMobileNetworkIdentifier);
        return this;
    }

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

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