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

package com.azure.resourcemanager.networkcloud.implementation;

import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.networkcloud.fluent.models.BmcKeySetInner;
import com.azure.resourcemanager.networkcloud.models.BmcKeySet;
import com.azure.resourcemanager.networkcloud.models.BmcKeySetDetailedStatus;
import com.azure.resourcemanager.networkcloud.models.BmcKeySetPatchParameters;
import com.azure.resourcemanager.networkcloud.models.BmcKeySetPrivilegeLevel;
import com.azure.resourcemanager.networkcloud.models.BmcKeySetProvisioningState;
import com.azure.resourcemanager.networkcloud.models.ExtendedLocation;
import com.azure.resourcemanager.networkcloud.models.KeySetUser;
import com.azure.resourcemanager.networkcloud.models.KeySetUserStatus;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class BmcKeySetImpl implements BmcKeySet, BmcKeySet.Definition, BmcKeySet.Update {
    private BmcKeySetInner innerObject;

    private final com.azure.resourcemanager.networkcloud.NetworkCloudManager 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 ExtendedLocation extendedLocation() {
        return this.innerModel().extendedLocation();
    }

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

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

    public BmcKeySetDetailedStatus detailedStatus() {
        return this.innerModel().detailedStatus();
    }

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

    public OffsetDateTime expiration() {
        return this.innerModel().expiration();
    }

    public OffsetDateTime lastValidation() {
        return this.innerModel().lastValidation();
    }

    public BmcKeySetPrivilegeLevel privilegeLevel() {
        return this.innerModel().privilegeLevel();
    }

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.networkcloud.NetworkCloudManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String clusterName;

    private String bmcKeySetName;

    private BmcKeySetPatchParameters updateBmcKeySetUpdateParameters;

    public BmcKeySetImpl withExistingCluster(String resourceGroupName, String clusterName) {
        this.resourceGroupName = resourceGroupName;
        this.clusterName = clusterName;
        return this;
    }

    public BmcKeySet create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBmcKeySets()
                .createOrUpdate(resourceGroupName, clusterName, bmcKeySetName, this.innerModel(), Context.NONE);
        return this;
    }

    public BmcKeySet create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBmcKeySets()
                .createOrUpdate(resourceGroupName, clusterName, bmcKeySetName, this.innerModel(), context);
        return this;
    }

    BmcKeySetImpl(String name, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = new BmcKeySetInner();
        this.serviceManager = serviceManager;
        this.bmcKeySetName = name;
    }

    public BmcKeySetImpl update() {
        this.updateBmcKeySetUpdateParameters = new BmcKeySetPatchParameters();
        return this;
    }

    public BmcKeySet apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBmcKeySets()
                .update(resourceGroupName, clusterName, bmcKeySetName, updateBmcKeySetUpdateParameters, Context.NONE);
        return this;
    }

    public BmcKeySet apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBmcKeySets()
                .update(resourceGroupName, clusterName, bmcKeySetName, updateBmcKeySetUpdateParameters, context);
        return this;
    }

    BmcKeySetImpl(
        BmcKeySetInner innerObject, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.clusterName = Utils.getValueFromIdByName(innerObject.id(), "clusters");
        this.bmcKeySetName = Utils.getValueFromIdByName(innerObject.id(), "bmcKeySets");
    }

    public BmcKeySet refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBmcKeySets()
                .getWithResponse(resourceGroupName, clusterName, bmcKeySetName, Context.NONE)
                .getValue();
        return this;
    }

    public BmcKeySet refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBmcKeySets()
                .getWithResponse(resourceGroupName, clusterName, bmcKeySetName, context)
                .getValue();
        return this;
    }

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

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

    public BmcKeySetImpl withExtendedLocation(ExtendedLocation extendedLocation) {
        this.innerModel().withExtendedLocation(extendedLocation);
        return this;
    }

    public BmcKeySetImpl withAzureGroupId(String azureGroupId) {
        this.innerModel().withAzureGroupId(azureGroupId);
        return this;
    }

    public BmcKeySetImpl withExpiration(OffsetDateTime expiration) {
        if (isInCreateMode()) {
            this.innerModel().withExpiration(expiration);
            return this;
        } else {
            this.updateBmcKeySetUpdateParameters.withExpiration(expiration);
            return this;
        }
    }

    public BmcKeySetImpl withPrivilegeLevel(BmcKeySetPrivilegeLevel privilegeLevel) {
        this.innerModel().withPrivilegeLevel(privilegeLevel);
        return this;
    }

    public BmcKeySetImpl withUserList(List<KeySetUser> userList) {
        if (isInCreateMode()) {
            this.innerModel().withUserList(userList);
            return this;
        } else {
            this.updateBmcKeySetUpdateParameters.withUserList(userList);
            return this;
        }
    }

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

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