// 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.BareMetalMachineKeySetInner;
import com.azure.resourcemanager.networkcloud.models.BareMetalMachineKeySet;
import com.azure.resourcemanager.networkcloud.models.BareMetalMachineKeySetDetailedStatus;
import com.azure.resourcemanager.networkcloud.models.BareMetalMachineKeySetPatchParameters;
import com.azure.resourcemanager.networkcloud.models.BareMetalMachineKeySetPrivilegeLevel;
import com.azure.resourcemanager.networkcloud.models.BareMetalMachineKeySetProvisioningState;
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 BareMetalMachineKeySetImpl
    implements BareMetalMachineKeySet, BareMetalMachineKeySet.Definition, BareMetalMachineKeySet.Update {
    private BareMetalMachineKeySetInner 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 BareMetalMachineKeySetDetailedStatus detailedStatus() {
        return this.innerModel().detailedStatus();
    }

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

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

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

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

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

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

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

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

    private String resourceGroupName;

    private String clusterName;

    private String bareMetalMachineKeySetName;

    private BareMetalMachineKeySetPatchParameters updateBareMetalMachineKeySetUpdateParameters;

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

    public BareMetalMachineKeySet create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBareMetalMachineKeySets()
                .createOrUpdate(
                    resourceGroupName, clusterName, bareMetalMachineKeySetName, this.innerModel(), Context.NONE);
        return this;
    }

    public BareMetalMachineKeySet create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBareMetalMachineKeySets()
                .createOrUpdate(resourceGroupName, clusterName, bareMetalMachineKeySetName, this.innerModel(), context);
        return this;
    }

    BareMetalMachineKeySetImpl(String name, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = new BareMetalMachineKeySetInner();
        this.serviceManager = serviceManager;
        this.bareMetalMachineKeySetName = name;
    }

    public BareMetalMachineKeySetImpl update() {
        this.updateBareMetalMachineKeySetUpdateParameters = new BareMetalMachineKeySetPatchParameters();
        return this;
    }

    public BareMetalMachineKeySet apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBareMetalMachineKeySets()
                .update(
                    resourceGroupName,
                    clusterName,
                    bareMetalMachineKeySetName,
                    updateBareMetalMachineKeySetUpdateParameters,
                    Context.NONE);
        return this;
    }

    public BareMetalMachineKeySet apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBareMetalMachineKeySets()
                .update(
                    resourceGroupName,
                    clusterName,
                    bareMetalMachineKeySetName,
                    updateBareMetalMachineKeySetUpdateParameters,
                    context);
        return this;
    }

    BareMetalMachineKeySetImpl(
        BareMetalMachineKeySetInner 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.bareMetalMachineKeySetName = Utils.getValueFromIdByName(innerObject.id(), "bareMetalMachineKeySets");
    }

    public BareMetalMachineKeySet refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBareMetalMachineKeySets()
                .getWithResponse(resourceGroupName, clusterName, bareMetalMachineKeySetName, Context.NONE)
                .getValue();
        return this;
    }

    public BareMetalMachineKeySet refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getBareMetalMachineKeySets()
                .getWithResponse(resourceGroupName, clusterName, bareMetalMachineKeySetName, context)
                .getValue();
        return this;
    }

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

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

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

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

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

    public BareMetalMachineKeySetImpl withJumpHostsAllowed(List<String> jumpHostsAllowed) {
        if (isInCreateMode()) {
            this.innerModel().withJumpHostsAllowed(jumpHostsAllowed);
            return this;
        } else {
            this.updateBareMetalMachineKeySetUpdateParameters.withJumpHostsAllowed(jumpHostsAllowed);
            return this;
        }
    }

    public BareMetalMachineKeySetImpl withPrivilegeLevel(BareMetalMachineKeySetPrivilegeLevel privilegeLevel) {
        this.innerModel().withPrivilegeLevel(privilegeLevel);
        return this;
    }

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

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

    public BareMetalMachineKeySetImpl withOsGroupName(String osGroupName) {
        this.innerModel().withOsGroupName(osGroupName);
        return this;
    }

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