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

package com.azure.resourcemanager.managednetworkfabric.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.managednetworkfabric.fluent.models.AccessControlListInner;
import com.azure.resourcemanager.managednetworkfabric.models.AccessControlList;
import com.azure.resourcemanager.managednetworkfabric.models.AccessControlListMatchConfiguration;
import com.azure.resourcemanager.managednetworkfabric.models.AccessControlListPatch;
import com.azure.resourcemanager.managednetworkfabric.models.AdministrativeState;
import com.azure.resourcemanager.managednetworkfabric.models.CommonDynamicMatchConfiguration;
import com.azure.resourcemanager.managednetworkfabric.models.CommonPostActionResponseForStateUpdate;
import com.azure.resourcemanager.managednetworkfabric.models.ConfigurationState;
import com.azure.resourcemanager.managednetworkfabric.models.ConfigurationType;
import com.azure.resourcemanager.managednetworkfabric.models.ProvisioningState;
import com.azure.resourcemanager.managednetworkfabric.models.UpdateAdministrativeState;
import com.azure.resourcemanager.managednetworkfabric.models.ValidateConfigurationResponse;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class AccessControlListImpl
    implements AccessControlList, AccessControlList.Definition, AccessControlList.Update {
    private AccessControlListInner innerObject;

    private final com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager 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 OffsetDateTime lastSyncedTime() {
        return this.innerModel().lastSyncedTime();
    }

    public ConfigurationState configurationState() {
        return this.innerModel().configurationState();
    }

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

    public AdministrativeState administrativeState() {
        return this.innerModel().administrativeState();
    }

    public ConfigurationType configurationType() {
        return this.innerModel().configurationType();
    }

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accessControlListName;

    private AccessControlListPatch updateBody;

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

    public AccessControlList create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccessControlLists()
                .create(resourceGroupName, accessControlListName, this.innerModel(), Context.NONE);
        return this;
    }

    public AccessControlList create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccessControlLists()
                .create(resourceGroupName, accessControlListName, this.innerModel(), context);
        return this;
    }

    AccessControlListImpl(
        String name, com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = new AccessControlListInner();
        this.serviceManager = serviceManager;
        this.accessControlListName = name;
    }

    public AccessControlListImpl update() {
        this.updateBody = new AccessControlListPatch();
        return this;
    }

    public AccessControlList apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccessControlLists()
                .update(resourceGroupName, accessControlListName, updateBody, Context.NONE);
        return this;
    }

    public AccessControlList apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccessControlLists()
                .update(resourceGroupName, accessControlListName, updateBody, context);
        return this;
    }

    AccessControlListImpl(
        AccessControlListInner innerObject,
        com.azure.resourcemanager.managednetworkfabric.ManagedNetworkFabricManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accessControlListName = Utils.getValueFromIdByName(innerObject.id(), "accessControlLists");
    }

    public AccessControlList refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccessControlLists()
                .getByResourceGroupWithResponse(resourceGroupName, accessControlListName, Context.NONE)
                .getValue();
        return this;
    }

    public AccessControlList refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAccessControlLists()
                .getByResourceGroupWithResponse(resourceGroupName, accessControlListName, context)
                .getValue();
        return this;
    }

    public CommonPostActionResponseForStateUpdate updateAdministrativeState(UpdateAdministrativeState body) {
        return serviceManager
            .accessControlLists()
            .updateAdministrativeState(resourceGroupName, accessControlListName, body);
    }

    public CommonPostActionResponseForStateUpdate updateAdministrativeState(
        UpdateAdministrativeState body, Context context) {
        return serviceManager
            .accessControlLists()
            .updateAdministrativeState(resourceGroupName, accessControlListName, body, context);
    }

    public CommonPostActionResponseForStateUpdate resync() {
        return serviceManager.accessControlLists().resync(resourceGroupName, accessControlListName);
    }

    public CommonPostActionResponseForStateUpdate resync(Context context) {
        return serviceManager.accessControlLists().resync(resourceGroupName, accessControlListName, context);
    }

    public ValidateConfigurationResponse validateConfiguration() {
        return serviceManager.accessControlLists().validateConfiguration(resourceGroupName, accessControlListName);
    }

    public ValidateConfigurationResponse validateConfiguration(Context context) {
        return serviceManager
            .accessControlLists()
            .validateConfiguration(resourceGroupName, accessControlListName, context);
    }

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

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

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

    public AccessControlListImpl withConfigurationType(ConfigurationType configurationType) {
        if (isInCreateMode()) {
            this.innerModel().withConfigurationType(configurationType);
            return this;
        } else {
            this.updateBody.withConfigurationType(configurationType);
            return this;
        }
    }

    public AccessControlListImpl withAclsUrl(String aclsUrl) {
        if (isInCreateMode()) {
            this.innerModel().withAclsUrl(aclsUrl);
            return this;
        } else {
            this.updateBody.withAclsUrl(aclsUrl);
            return this;
        }
    }

    public AccessControlListImpl withMatchConfigurations(
        List<AccessControlListMatchConfiguration> matchConfigurations) {
        if (isInCreateMode()) {
            this.innerModel().withMatchConfigurations(matchConfigurations);
            return this;
        } else {
            this.updateBody.withMatchConfigurations(matchConfigurations);
            return this;
        }
    }

    public AccessControlListImpl withDynamicMatchConfigurations(
        List<CommonDynamicMatchConfiguration> dynamicMatchConfigurations) {
        if (isInCreateMode()) {
            this.innerModel().withDynamicMatchConfigurations(dynamicMatchConfigurations);
            return this;
        } else {
            this.updateBody.withDynamicMatchConfigurations(dynamicMatchConfigurations);
            return this;
        }
    }

    public AccessControlListImpl withAnnotation(String annotation) {
        if (isInCreateMode()) {
            this.innerModel().withAnnotation(annotation);
            return this;
        } else {
            this.updateBody.withAnnotation(annotation);
            return this;
        }
    }

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