// 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.ConsoleInner;
import com.azure.resourcemanager.networkcloud.models.Console;
import com.azure.resourcemanager.networkcloud.models.ConsoleDetailedStatus;
import com.azure.resourcemanager.networkcloud.models.ConsoleEnabled;
import com.azure.resourcemanager.networkcloud.models.ConsolePatchParameters;
import com.azure.resourcemanager.networkcloud.models.ConsoleProvisioningState;
import com.azure.resourcemanager.networkcloud.models.ExtendedLocation;
import com.azure.resourcemanager.networkcloud.models.SshPublicKey;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.Map;

public final class ConsoleImpl implements Console, Console.Definition, Console.Update {
    private ConsoleInner 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 ConsoleDetailedStatus detailedStatus() {
        return this.innerModel().detailedStatus();
    }

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

    public ConsoleEnabled enabled() {
        return this.innerModel().enabled();
    }

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

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

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

    public SshPublicKey sshPublicKey() {
        return this.innerModel().sshPublicKey();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

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

    private String resourceGroupName;

    private String virtualMachineName;

    private String consoleName;

    private ConsolePatchParameters updateConsoleUpdateParameters;

    public ConsoleImpl withExistingVirtualMachine(String resourceGroupName, String virtualMachineName) {
        this.resourceGroupName = resourceGroupName;
        this.virtualMachineName = virtualMachineName;
        return this;
    }

    public Console create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConsoles()
                .createOrUpdate(resourceGroupName, virtualMachineName, consoleName, this.innerModel(), Context.NONE);
        return this;
    }

    public Console create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConsoles()
                .createOrUpdate(resourceGroupName, virtualMachineName, consoleName, this.innerModel(), context);
        return this;
    }

    ConsoleImpl(String name, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = new ConsoleInner();
        this.serviceManager = serviceManager;
        this.consoleName = name;
    }

    public ConsoleImpl update() {
        this.updateConsoleUpdateParameters = new ConsolePatchParameters();
        return this;
    }

    public Console apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConsoles()
                .update(
                    resourceGroupName, virtualMachineName, consoleName, updateConsoleUpdateParameters, Context.NONE);
        return this;
    }

    public Console apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConsoles()
                .update(resourceGroupName, virtualMachineName, consoleName, updateConsoleUpdateParameters, context);
        return this;
    }

    ConsoleImpl(ConsoleInner innerObject, com.azure.resourcemanager.networkcloud.NetworkCloudManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.virtualMachineName = Utils.getValueFromIdByName(innerObject.id(), "virtualMachines");
        this.consoleName = Utils.getValueFromIdByName(innerObject.id(), "consoles");
    }

    public Console refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConsoles()
                .getWithResponse(resourceGroupName, virtualMachineName, consoleName, Context.NONE)
                .getValue();
        return this;
    }

    public Console refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getConsoles()
                .getWithResponse(resourceGroupName, virtualMachineName, consoleName, context)
                .getValue();
        return this;
    }

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

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

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

    public ConsoleImpl withEnabled(ConsoleEnabled enabled) {
        if (isInCreateMode()) {
            this.innerModel().withEnabled(enabled);
            return this;
        } else {
            this.updateConsoleUpdateParameters.withEnabled(enabled);
            return this;
        }
    }

    public ConsoleImpl withSshPublicKey(SshPublicKey sshPublicKey) {
        if (isInCreateMode()) {
            this.innerModel().withSshPublicKey(sshPublicKey);
            return this;
        } else {
            this.updateConsoleUpdateParameters.withSshPublicKey(sshPublicKey);
            return this;
        }
    }

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

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

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