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

package com.azure.resourcemanager.containerservicefleet.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.containerservicefleet.fluent.models.UpdateRunInner;
import com.azure.resourcemanager.containerservicefleet.models.ManagedClusterUpdate;
import com.azure.resourcemanager.containerservicefleet.models.UpdateRun;
import com.azure.resourcemanager.containerservicefleet.models.UpdateRunProvisioningState;
import com.azure.resourcemanager.containerservicefleet.models.UpdateRunStatus;
import com.azure.resourcemanager.containerservicefleet.models.UpdateRunStrategy;

public final class UpdateRunImpl implements UpdateRun, UpdateRun.Definition, UpdateRun.Update {
    private UpdateRunInner innerObject;

    private final com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager;

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

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

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

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

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

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

    public UpdateRunStrategy strategy() {
        return this.innerModel().strategy();
    }

    public ManagedClusterUpdate managedClusterUpdate() {
        return this.innerModel().managedClusterUpdate();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String fleetName;

    private String updateRunName;

    private String createIfMatch;

    private String createIfNoneMatch;

    private String updateIfMatch;

    private String updateIfNoneMatch;

    public UpdateRunImpl withExistingFleet(String resourceGroupName, String fleetName) {
        this.resourceGroupName = resourceGroupName;
        this.fleetName = fleetName;
        return this;
    }

    public UpdateRun create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUpdateRuns()
                .createOrUpdate(
                    resourceGroupName,
                    fleetName,
                    updateRunName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    Context.NONE);
        return this;
    }

    public UpdateRun create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUpdateRuns()
                .createOrUpdate(
                    resourceGroupName,
                    fleetName,
                    updateRunName,
                    this.innerModel(),
                    createIfMatch,
                    createIfNoneMatch,
                    context);
        return this;
    }

    UpdateRunImpl(
        String name, com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager) {
        this.innerObject = new UpdateRunInner();
        this.serviceManager = serviceManager;
        this.updateRunName = name;
        this.createIfMatch = null;
        this.createIfNoneMatch = null;
    }

    public UpdateRunImpl update() {
        this.updateIfMatch = null;
        this.updateIfNoneMatch = null;
        return this;
    }

    public UpdateRun apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUpdateRuns()
                .createOrUpdate(
                    resourceGroupName,
                    fleetName,
                    updateRunName,
                    this.innerModel(),
                    updateIfMatch,
                    updateIfNoneMatch,
                    Context.NONE);
        return this;
    }

    public UpdateRun apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUpdateRuns()
                .createOrUpdate(
                    resourceGroupName,
                    fleetName,
                    updateRunName,
                    this.innerModel(),
                    updateIfMatch,
                    updateIfNoneMatch,
                    context);
        return this;
    }

    UpdateRunImpl(
        UpdateRunInner innerObject,
        com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.fleetName = Utils.getValueFromIdByName(innerObject.id(), "fleets");
        this.updateRunName = Utils.getValueFromIdByName(innerObject.id(), "updateRuns");
    }

    public UpdateRun refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUpdateRuns()
                .getWithResponse(resourceGroupName, fleetName, updateRunName, Context.NONE)
                .getValue();
        return this;
    }

    public UpdateRun refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getUpdateRuns()
                .getWithResponse(resourceGroupName, fleetName, updateRunName, context)
                .getValue();
        return this;
    }

    public UpdateRun start() {
        return serviceManager.updateRuns().start(resourceGroupName, fleetName, updateRunName);
    }

    public UpdateRun start(String ifMatch, Context context) {
        return serviceManager.updateRuns().start(resourceGroupName, fleetName, updateRunName, ifMatch, context);
    }

    public UpdateRun stop() {
        return serviceManager.updateRuns().stop(resourceGroupName, fleetName, updateRunName);
    }

    public UpdateRun stop(String ifMatch, Context context) {
        return serviceManager.updateRuns().stop(resourceGroupName, fleetName, updateRunName, ifMatch, context);
    }

    public UpdateRunImpl withStrategy(UpdateRunStrategy strategy) {
        this.innerModel().withStrategy(strategy);
        return this;
    }

    public UpdateRunImpl withManagedClusterUpdate(ManagedClusterUpdate managedClusterUpdate) {
        this.innerModel().withManagedClusterUpdate(managedClusterUpdate);
        return this;
    }

    public UpdateRunImpl withIfMatch(String ifMatch) {
        if (isInCreateMode()) {
            this.createIfMatch = ifMatch;
            return this;
        } else {
            this.updateIfMatch = ifMatch;
            return this;
        }
    }

    public UpdateRunImpl withIfNoneMatch(String ifNoneMatch) {
        if (isInCreateMode()) {
            this.createIfNoneMatch = ifNoneMatch;
            return this;
        } else {
            this.updateIfNoneMatch = ifNoneMatch;
            return this;
        }
    }

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