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

package com.azure.resourcemanager.resourceconnector.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.resourceconnector.fluent.models.ApplianceInner;
import com.azure.resourcemanager.resourceconnector.models.Appliance;
import com.azure.resourcemanager.resourceconnector.models.ApplianceListCredentialResults;
import com.azure.resourcemanager.resourceconnector.models.ApplianceListKeysResults;
import com.azure.resourcemanager.resourceconnector.models.AppliancePropertiesInfrastructureConfig;
import com.azure.resourcemanager.resourceconnector.models.Distro;
import com.azure.resourcemanager.resourceconnector.models.Identity;
import com.azure.resourcemanager.resourceconnector.models.PatchableAppliance;
import com.azure.resourcemanager.resourceconnector.models.Status;
import java.util.Collections;
import java.util.Map;

public final class ApplianceImpl implements Appliance, Appliance.Definition, Appliance.Update {
    private ApplianceInner innerObject;

    private final com.azure.resourcemanager.resourceconnector.AppliancesManager 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 Identity identity() {
        return this.innerModel().identity();
    }

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

    public Distro distro() {
        return this.innerModel().distro();
    }

    public AppliancePropertiesInfrastructureConfig infrastructureConfig() {
        return this.innerModel().infrastructureConfig();
    }

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

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

    public Status status() {
        return this.innerModel().status();
    }

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.resourceconnector.AppliancesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String resourceName;

    private PatchableAppliance updateParameters;

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

    public Appliance create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAppliances()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), Context.NONE);
        return this;
    }

    public Appliance create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAppliances()
                .createOrUpdate(resourceGroupName, resourceName, this.innerModel(), context);
        return this;
    }

    ApplianceImpl(String name, com.azure.resourcemanager.resourceconnector.AppliancesManager serviceManager) {
        this.innerObject = new ApplianceInner();
        this.serviceManager = serviceManager;
        this.resourceName = name;
    }

    public ApplianceImpl update() {
        this.updateParameters = new PatchableAppliance();
        return this;
    }

    public Appliance apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAppliances()
                .updateWithResponse(resourceGroupName, resourceName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Appliance apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAppliances()
                .updateWithResponse(resourceGroupName, resourceName, updateParameters, context)
                .getValue();
        return this;
    }

    ApplianceImpl(
        ApplianceInner innerObject, com.azure.resourcemanager.resourceconnector.AppliancesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.resourceName = Utils.getValueFromIdByName(innerObject.id(), "appliances");
    }

    public Appliance refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAppliances()
                .getByResourceGroupWithResponse(resourceGroupName, resourceName, Context.NONE)
                .getValue();
        return this;
    }

    public Appliance refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getAppliances()
                .getByResourceGroupWithResponse(resourceGroupName, resourceName, context)
                .getValue();
        return this;
    }

    public Response<ApplianceListCredentialResults> listClusterUserCredentialWithResponse(Context context) {
        return serviceManager
            .appliances()
            .listClusterUserCredentialWithResponse(resourceGroupName, resourceName, context);
    }

    public ApplianceListCredentialResults listClusterUserCredential() {
        return serviceManager.appliances().listClusterUserCredential(resourceGroupName, resourceName);
    }

    public Response<ApplianceListKeysResults> listKeysWithResponse(Context context) {
        return serviceManager.appliances().listKeysWithResponse(resourceGroupName, resourceName, context);
    }

    public ApplianceListKeysResults listKeys() {
        return serviceManager.appliances().listKeys(resourceGroupName, resourceName);
    }

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

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

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

    public ApplianceImpl withIdentity(Identity identity) {
        this.innerModel().withIdentity(identity);
        return this;
    }

    public ApplianceImpl withDistro(Distro distro) {
        this.innerModel().withDistro(distro);
        return this;
    }

    public ApplianceImpl withInfrastructureConfig(AppliancePropertiesInfrastructureConfig infrastructureConfig) {
        this.innerModel().withInfrastructureConfig(infrastructureConfig);
        return this;
    }

    public ApplianceImpl withPublicKey(String publicKey) {
        this.innerModel().withPublicKey(publicKey);
        return this;
    }

    public ApplianceImpl withVersion(String version) {
        this.innerModel().withVersion(version);
        return this;
    }

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