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

package com.azure.resourcemanager.extendedlocation.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.extendedlocation.fluent.models.CustomLocationInner;
import com.azure.resourcemanager.extendedlocation.models.CustomLocation;
import com.azure.resourcemanager.extendedlocation.models.CustomLocationFindTargetResourceGroupProperties;
import com.azure.resourcemanager.extendedlocation.models.CustomLocationFindTargetResourceGroupResult;
import com.azure.resourcemanager.extendedlocation.models.CustomLocationPropertiesAuthentication;
import com.azure.resourcemanager.extendedlocation.models.HostType;
import com.azure.resourcemanager.extendedlocation.models.Identity;
import com.azure.resourcemanager.extendedlocation.models.PatchableCustomLocations;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class CustomLocationImpl implements CustomLocation, CustomLocation.Definition, CustomLocation.Update {
    private CustomLocationInner innerObject;

    private final com.azure.resourcemanager.extendedlocation.CustomLocationsManager 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 Identity identity() {
        return this.innerModel().identity();
    }

    public SystemData systemData() {
        return this.innerModel().systemData();
    }

    public CustomLocationPropertiesAuthentication authentication() {
        return this.innerModel().authentication();
    }

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

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

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

    public HostType hostType() {
        return this.innerModel().hostType();
    }

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

    public String 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 CustomLocationInner innerModel() {
        return this.innerObject;
    }

    private com.azure.resourcemanager.extendedlocation.CustomLocationsManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private PatchableCustomLocations updateParameters;

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

    public CustomLocation create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomLocations()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public CustomLocation create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomLocations()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), context);
        return this;
    }

    CustomLocationImpl(String name, com.azure.resourcemanager.extendedlocation.CustomLocationsManager serviceManager) {
        this.innerObject = new CustomLocationInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
    }

    public CustomLocationImpl update() {
        this.updateParameters = new PatchableCustomLocations();
        return this;
    }

    public CustomLocation apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomLocations()
                .updateWithResponse(resourceGroupName, resourceName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public CustomLocation apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getCustomLocations()
                .updateWithResponse(resourceGroupName, resourceName, updateParameters, context)
                .getValue();
        return this;
    }

    CustomLocationImpl(
        CustomLocationInner innerObject,
        com.azure.resourcemanager.extendedlocation.CustomLocationsManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "customLocations");
    }

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

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

    public CustomLocationFindTargetResourceGroupResult findTargetResourceGroup(
        CustomLocationFindTargetResourceGroupProperties parameters) {
        return serviceManager.customLocations().findTargetResourceGroup(resourceGroupName, resourceName, parameters);
    }

    public Response<CustomLocationFindTargetResourceGroupResult> findTargetResourceGroupWithResponse(
        CustomLocationFindTargetResourceGroupProperties parameters, Context context) {
        return serviceManager
            .customLocations()
            .findTargetResourceGroupWithResponse(resourceGroupName, resourceName, parameters, context);
    }

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

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

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

    public CustomLocationImpl withIdentity(Identity identity) {
        if (isInCreateMode()) {
            this.innerModel().withIdentity(identity);
            return this;
        } else {
            this.updateParameters.withIdentity(identity);
            return this;
        }
    }

    public CustomLocationImpl withAuthentication(CustomLocationPropertiesAuthentication authentication) {
        if (isInCreateMode()) {
            this.innerModel().withAuthentication(authentication);
            return this;
        } else {
            this.updateParameters.withAuthentication(authentication);
            return this;
        }
    }

    public CustomLocationImpl withClusterExtensionIds(List<String> clusterExtensionIds) {
        if (isInCreateMode()) {
            this.innerModel().withClusterExtensionIds(clusterExtensionIds);
            return this;
        } else {
            this.updateParameters.withClusterExtensionIds(clusterExtensionIds);
            return this;
        }
    }

    public CustomLocationImpl withDisplayName(String displayName) {
        if (isInCreateMode()) {
            this.innerModel().withDisplayName(displayName);
            return this;
        } else {
            this.updateParameters.withDisplayName(displayName);
            return this;
        }
    }

    public CustomLocationImpl withHostResourceId(String hostResourceId) {
        if (isInCreateMode()) {
            this.innerModel().withHostResourceId(hostResourceId);
            return this;
        } else {
            this.updateParameters.withHostResourceId(hostResourceId);
            return this;
        }
    }

    public CustomLocationImpl withHostType(HostType hostType) {
        if (isInCreateMode()) {
            this.innerModel().withHostType(hostType);
            return this;
        } else {
            this.updateParameters.withHostType(hostType);
            return this;
        }
    }

    public CustomLocationImpl withNamespace(String namespace) {
        if (isInCreateMode()) {
            this.innerModel().withNamespace(namespace);
            return this;
        } else {
            this.updateParameters.withNamespace(namespace);
            return this;
        }
    }

    public CustomLocationImpl withProvisioningState(String provisioningState) {
        if (isInCreateMode()) {
            this.innerModel().withProvisioningState(provisioningState);
            return this;
        } else {
            this.updateParameters.withProvisioningState(provisioningState);
            return this;
        }
    }

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