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

package com.azure.resourcemanager.devspaces.implementation;

import com.azure.core.http.rest.Response;
import com.azure.core.management.Region;
import com.azure.core.util.Context;
import com.azure.resourcemanager.devspaces.fluent.models.ControllerInner;
import com.azure.resourcemanager.devspaces.models.Controller;
import com.azure.resourcemanager.devspaces.models.ControllerConnectionDetailsList;
import com.azure.resourcemanager.devspaces.models.ControllerUpdateParameters;
import com.azure.resourcemanager.devspaces.models.ListConnectionDetailsParameters;
import com.azure.resourcemanager.devspaces.models.ProvisioningState;
import com.azure.resourcemanager.devspaces.models.Sku;
import java.util.Collections;
import java.util.Map;

public final class ControllerImpl implements Controller, Controller.Definition, Controller.Update {
    private ControllerInner innerObject;

    private final com.azure.resourcemanager.devspaces.DevSpacesManager 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 Sku sku() {
        return this.innerModel().sku();
    }

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

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

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

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

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

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

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

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.devspaces.DevSpacesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String name;

    private ControllerUpdateParameters updateControllerUpdateParameters;

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

    public Controller create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getControllers()
                .create(resourceGroupName, name, this.innerModel(), Context.NONE);
        return this;
    }

    public Controller create(Context context) {
        this.innerObject =
            serviceManager.serviceClient().getControllers().create(resourceGroupName, name, this.innerModel(), context);
        return this;
    }

    ControllerImpl(String name, com.azure.resourcemanager.devspaces.DevSpacesManager serviceManager) {
        this.innerObject = new ControllerInner();
        this.serviceManager = serviceManager;
        this.name = name;
    }

    public ControllerImpl update() {
        this.updateControllerUpdateParameters = new ControllerUpdateParameters();
        return this;
    }

    public Controller apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getControllers()
                .updateWithResponse(resourceGroupName, name, updateControllerUpdateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public Controller apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getControllers()
                .updateWithResponse(resourceGroupName, name, updateControllerUpdateParameters, context)
                .getValue();
        return this;
    }

    ControllerImpl(ControllerInner innerObject, com.azure.resourcemanager.devspaces.DevSpacesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.name = Utils.getValueFromIdByName(innerObject.id(), "controllers");
    }

    public Controller refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getControllers()
                .getByResourceGroupWithResponse(resourceGroupName, name, Context.NONE)
                .getValue();
        return this;
    }

    public Controller refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getControllers()
                .getByResourceGroupWithResponse(resourceGroupName, name, context)
                .getValue();
        return this;
    }

    public Response<ControllerConnectionDetailsList> listConnectionDetailsWithResponse(
        ListConnectionDetailsParameters listConnectionDetailsParameters, Context context) {
        return serviceManager
            .controllers()
            .listConnectionDetailsWithResponse(resourceGroupName, name, listConnectionDetailsParameters, context);
    }

    public ControllerConnectionDetailsList listConnectionDetails(
        ListConnectionDetailsParameters listConnectionDetailsParameters) {
        return serviceManager
            .controllers()
            .listConnectionDetails(resourceGroupName, name, listConnectionDetailsParameters);
    }

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

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

    public ControllerImpl withSku(Sku sku) {
        this.innerModel().withSku(sku);
        return this;
    }

    public ControllerImpl withTargetContainerHostResourceId(String targetContainerHostResourceId) {
        this.innerModel().withTargetContainerHostResourceId(targetContainerHostResourceId);
        return this;
    }

    public ControllerImpl withTargetContainerHostCredentialsBase64(String targetContainerHostCredentialsBase64) {
        if (isInCreateMode()) {
            this.innerModel().withTargetContainerHostCredentialsBase64(targetContainerHostCredentialsBase64);
            return this;
        } else {
            this
                .updateControllerUpdateParameters
                .withTargetContainerHostCredentialsBase64(targetContainerHostCredentialsBase64);
            return this;
        }
    }

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

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