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

package com.azure.resourcemanager.labservices.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.labservices.fluent.models.ImageInner;
import com.azure.resourcemanager.labservices.models.EnableState;
import com.azure.resourcemanager.labservices.models.Image;
import com.azure.resourcemanager.labservices.models.ImageUpdate;
import com.azure.resourcemanager.labservices.models.OsState;
import com.azure.resourcemanager.labservices.models.OsType;
import com.azure.resourcemanager.labservices.models.ProvisioningState;
import java.util.Collections;
import java.util.List;

public final class ImageImpl implements Image, Image.Definition, Image.Update {
    private ImageInner innerObject;

    private final com.azure.resourcemanager.labservices.LabServicesManager 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 ProvisioningState provisioningState() {
        return this.innerModel().provisioningState();
    }

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

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

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

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

    public OsType osType() {
        return this.innerModel().osType();
    }

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

    public EnableState termsStatus() {
        return this.innerModel().termsStatus();
    }

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

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

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

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

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

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

    public OsState osState() {
        return this.innerModel().osState();
    }

    public EnableState enabledState() {
        return this.innerModel().enabledState();
    }

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.labservices.LabServicesManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String labPlanName;

    private String imageName;

    private ImageUpdate updateBody;

    public ImageImpl withExistingLabPlan(String resourceGroupName, String labPlanName) {
        this.resourceGroupName = resourceGroupName;
        this.labPlanName = labPlanName;
        return this;
    }

    public Image create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getImages()
                .createOrUpdateWithResponse(resourceGroupName, labPlanName, imageName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public Image create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getImages()
                .createOrUpdateWithResponse(resourceGroupName, labPlanName, imageName, this.innerModel(), context)
                .getValue();
        return this;
    }

    ImageImpl(String name, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = new ImageInner();
        this.serviceManager = serviceManager;
        this.imageName = name;
    }

    public ImageImpl update() {
        this.updateBody = new ImageUpdate();
        return this;
    }

    public Image apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getImages()
                .updateWithResponse(resourceGroupName, labPlanName, imageName, updateBody, Context.NONE)
                .getValue();
        return this;
    }

    public Image apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getImages()
                .updateWithResponse(resourceGroupName, labPlanName, imageName, updateBody, context)
                .getValue();
        return this;
    }

    ImageImpl(ImageInner innerObject, com.azure.resourcemanager.labservices.LabServicesManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.labPlanName = Utils.getValueFromIdByName(innerObject.id(), "labPlans");
        this.imageName = Utils.getValueFromIdByName(innerObject.id(), "images");
    }

    public Image refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getImages()
                .getWithResponse(resourceGroupName, labPlanName, imageName, Context.NONE)
                .getValue();
        return this;
    }

    public Image refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getImages()
                .getWithResponse(resourceGroupName, labPlanName, imageName, context)
                .getValue();
        return this;
    }

    public ImageImpl withAvailableRegions(List<String> availableRegions) {
        this.innerModel().withAvailableRegions(availableRegions);
        return this;
    }

    public ImageImpl withEnabledState(EnableState enabledState) {
        if (isInCreateMode()) {
            this.innerModel().withEnabledState(enabledState);
            return this;
        } else {
            this.updateBody.withEnabledState(enabledState);
            return this;
        }
    }

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