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

package com.azure.resourcemanager.videoanalyzer.implementation;

import com.azure.core.management.SystemData;
import com.azure.core.util.Context;
import com.azure.resourcemanager.videoanalyzer.fluent.models.PipelineJobInner;
import com.azure.resourcemanager.videoanalyzer.models.ParameterDefinition;
import com.azure.resourcemanager.videoanalyzer.models.PipelineJob;
import com.azure.resourcemanager.videoanalyzer.models.PipelineJobError;
import com.azure.resourcemanager.videoanalyzer.models.PipelineJobState;
import com.azure.resourcemanager.videoanalyzer.models.PipelineJobUpdate;
import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;

public final class PipelineJobImpl implements PipelineJob, PipelineJob.Definition, PipelineJob.Update {
    private PipelineJobInner innerObject;

    private final com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager 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 topologyName() {
        return this.innerModel().topologyName();
    }

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

    public PipelineJobState state() {
        return this.innerModel().state();
    }

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

    public PipelineJobError error() {
        return this.innerModel().error();
    }

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

    public String resourceGroupName() {
        return resourceGroupName;
    }

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

    private com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager manager() {
        return this.serviceManager;
    }

    private String resourceGroupName;

    private String accountName;

    private String pipelineJobName;

    private PipelineJobUpdate updateParameters;

    public PipelineJobImpl withExistingVideoAnalyzer(String resourceGroupName, String accountName) {
        this.resourceGroupName = resourceGroupName;
        this.accountName = accountName;
        return this;
    }

    public PipelineJob create() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPipelineJobs()
                .createOrUpdateWithResponse(
                    resourceGroupName, accountName, pipelineJobName, this.innerModel(), Context.NONE)
                .getValue();
        return this;
    }

    public PipelineJob create(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPipelineJobs()
                .createOrUpdateWithResponse(resourceGroupName, accountName, pipelineJobName, this.innerModel(), context)
                .getValue();
        return this;
    }

    PipelineJobImpl(String name, com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager serviceManager) {
        this.innerObject = new PipelineJobInner();
        this.serviceManager = serviceManager;
        this.pipelineJobName = name;
    }

    public PipelineJobImpl update() {
        this.updateParameters = new PipelineJobUpdate();
        return this;
    }

    public PipelineJob apply() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPipelineJobs()
                .updateWithResponse(resourceGroupName, accountName, pipelineJobName, updateParameters, Context.NONE)
                .getValue();
        return this;
    }

    public PipelineJob apply(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPipelineJobs()
                .updateWithResponse(resourceGroupName, accountName, pipelineJobName, updateParameters, context)
                .getValue();
        return this;
    }

    PipelineJobImpl(
        PipelineJobInner innerObject, com.azure.resourcemanager.videoanalyzer.VideoAnalyzerManager serviceManager) {
        this.innerObject = innerObject;
        this.serviceManager = serviceManager;
        this.resourceGroupName = Utils.getValueFromIdByName(innerObject.id(), "resourceGroups");
        this.accountName = Utils.getValueFromIdByName(innerObject.id(), "videoAnalyzers");
        this.pipelineJobName = Utils.getValueFromIdByName(innerObject.id(), "pipelineJobs");
    }

    public PipelineJob refresh() {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPipelineJobs()
                .getWithResponse(resourceGroupName, accountName, pipelineJobName, Context.NONE)
                .getValue();
        return this;
    }

    public PipelineJob refresh(Context context) {
        this.innerObject =
            serviceManager
                .serviceClient()
                .getPipelineJobs()
                .getWithResponse(resourceGroupName, accountName, pipelineJobName, context)
                .getValue();
        return this;
    }

    public void cancel() {
        serviceManager.pipelineJobs().cancel(resourceGroupName, accountName, pipelineJobName);
    }

    public void cancel(Context context) {
        serviceManager.pipelineJobs().cancel(resourceGroupName, accountName, pipelineJobName, context);
    }

    public PipelineJobImpl withTopologyName(String topologyName) {
        if (isInCreateMode()) {
            this.innerModel().withTopologyName(topologyName);
            return this;
        } else {
            this.updateParameters.withTopologyName(topologyName);
            return this;
        }
    }

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

    public PipelineJobImpl withParameters(List<ParameterDefinition> parameters) {
        if (isInCreateMode()) {
            this.innerModel().withParameters(parameters);
            return this;
        } else {
            this.updateParameters.withParameters(parameters);
            return this;
        }
    }

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