// 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.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.extendedlocation.fluent.models.ResourceSyncRuleInner;
import com.azure.resourcemanager.extendedlocation.models.PatchableResourceSyncRule;
import com.azure.resourcemanager.extendedlocation.models.ResourceSyncRule;
import com.azure.resourcemanager.extendedlocation.models.ResourceSyncRulePropertiesSelector;
import java.util.Collections;
import java.util.Map;

public final class ResourceSyncRuleImpl
    implements ResourceSyncRule, ResourceSyncRule.Definition, ResourceSyncRule.Update {
    private ResourceSyncRuleInner 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 SystemData systemData() {
        return this.innerModel().systemData();
    }

    public Integer priority() {
        return this.innerModel().priority();
    }

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

    public ResourceSyncRulePropertiesSelector selector() {
        return this.innerModel().selector();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String resourceName;

    private String childResourceName;

    private PatchableResourceSyncRule updateParameters;

    public ResourceSyncRuleImpl withExistingCustomLocation(String resourceGroupName, String resourceName) {
        this.resourceGroupName = resourceGroupName;
        this.resourceName = resourceName;
        return this;
    }

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

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

    ResourceSyncRuleImpl(
        String name, com.azure.resourcemanager.extendedlocation.CustomLocationsManager serviceManager) {
        this.innerObject = new ResourceSyncRuleInner();
        this.serviceManager = serviceManager;
        this.childResourceName = name;
    }

    public ResourceSyncRuleImpl update() {
        this.updateParameters = new PatchableResourceSyncRule();
        return this;
    }

    public ResourceSyncRule apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceSyncRules()
                .update(resourceGroupName, resourceName, childResourceName, updateParameters, Context.NONE);
        return this;
    }

    public ResourceSyncRule apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceSyncRules()
                .update(resourceGroupName, resourceName, childResourceName, updateParameters, context);
        return this;
    }

    ResourceSyncRuleImpl(
        ResourceSyncRuleInner 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");
        this.childResourceName = Utils.getValueFromIdByName(innerObject.id(), "resourceSyncRules");
    }

    public ResourceSyncRule refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceSyncRules()
                .getWithResponse(resourceGroupName, resourceName, childResourceName, Context.NONE)
                .getValue();
        return this;
    }

    public ResourceSyncRule refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getResourceSyncRules()
                .getWithResponse(resourceGroupName, resourceName, childResourceName, context)
                .getValue();
        return this;
    }

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

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

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

    public ResourceSyncRuleImpl withPriority(Integer priority) {
        if (isInCreateMode()) {
            this.innerModel().withPriority(priority);
            return this;
        } else {
            this.updateParameters.withPriority(priority);
            return this;
        }
    }

    public ResourceSyncRuleImpl withSelector(ResourceSyncRulePropertiesSelector selector) {
        if (isInCreateMode()) {
            this.innerModel().withSelector(selector);
            return this;
        } else {
            this.updateParameters.withSelector(selector);
            return this;
        }
    }

    public ResourceSyncRuleImpl withTargetResourceGroup(String targetResourceGroup) {
        if (isInCreateMode()) {
            this.innerModel().withTargetResourceGroup(targetResourceGroup);
            return this;
        } else {
            this.updateParameters.withTargetResourceGroup(targetResourceGroup);
            return this;
        }
    }

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