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

package com.azure.resourcemanager.storagemover.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.storagemover.fluent.models.AgentInner;
import com.azure.resourcemanager.storagemover.models.Agent;
import com.azure.resourcemanager.storagemover.models.AgentPropertiesErrorDetails;
import com.azure.resourcemanager.storagemover.models.AgentStatus;
import com.azure.resourcemanager.storagemover.models.AgentUpdateParameters;
import com.azure.resourcemanager.storagemover.models.ProvisioningState;
import java.time.OffsetDateTime;

public final class AgentImpl implements Agent, Agent.Definition, Agent.Update {
    private AgentInner innerObject;

    private final com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager;

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

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

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

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

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

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

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

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

    public AgentStatus agentStatus() {
        return this.innerModel().agentStatus();
    }

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

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

    public Long memoryInMB() {
        return this.innerModel().memoryInMB();
    }

    public Long numberOfCores() {
        return this.innerModel().numberOfCores();
    }

    public Long uptimeInSeconds() {
        return this.innerModel().uptimeInSeconds();
    }

    public AgentPropertiesErrorDetails errorDetails() {
        return this.innerModel().errorDetails();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.storagemover.StorageMoverManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String storageMoverName;

    private String agentName;

    private AgentUpdateParameters updateAgent;

    public AgentImpl withExistingStorageMover(String resourceGroupName, String storageMoverName) {
        this.resourceGroupName = resourceGroupName;
        this.storageMoverName = storageMoverName;
        return this;
    }

    public Agent create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAgents()
                .createOrUpdateWithResponse(
                    resourceGroupName, storageMoverName, agentName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Agent create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAgents()
                .createOrUpdateWithResponse(resourceGroupName, storageMoverName, agentName, this.innerModel(), context)
                .getValue();
        return this;
    }

    AgentImpl(String name, com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager) {
        this.innerObject = new AgentInner();
        this.serviceManager = serviceManager;
        this.agentName = name;
    }

    public AgentImpl update() {
        this.updateAgent = new AgentUpdateParameters();
        return this;
    }

    public Agent apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAgents()
                .updateWithResponse(resourceGroupName, storageMoverName, agentName, updateAgent, Context.NONE)
                .getValue();
        return this;
    }

    public Agent apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAgents()
                .updateWithResponse(resourceGroupName, storageMoverName, agentName, updateAgent, context)
                .getValue();
        return this;
    }

    AgentImpl(AgentInner innerObject, com.azure.resourcemanager.storagemover.StorageMoverManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.storageMoverName = Utils.getValueFromIdByName(innerObject.id(), "storageMovers");
        this.agentName = Utils.getValueFromIdByName(innerObject.id(), "agents");
    }

    public Agent refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAgents()
                .getWithResponse(resourceGroupName, storageMoverName, agentName, Context.NONE)
                .getValue();
        return this;
    }

    public Agent refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAgents()
                .getWithResponse(resourceGroupName, storageMoverName, agentName, context)
                .getValue();
        return this;
    }

    public AgentImpl withArcResourceId(String arcResourceId) {
        this.innerModel().withArcResourceId(arcResourceId);
        return this;
    }

    public AgentImpl withArcVmUuid(String arcVmUuid) {
        this.innerModel().withArcVmUuid(arcVmUuid);
        return this;
    }

    public AgentImpl withDescription(String description) {
        if (isInCreateMode()) {
            this.innerModel().withDescription(description);
            return this;
        } else {
            this.updateAgent.withDescription(description);
            return this;
        }
    }

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