// 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.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.http.rest.SimpleResponse;
import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.resourcemanager.containerservicefleet.fluent.UpdateRunsClient;
import com.azure.resourcemanager.containerservicefleet.fluent.models.UpdateRunInner;
import com.azure.resourcemanager.containerservicefleet.models.UpdateRun;
import com.azure.resourcemanager.containerservicefleet.models.UpdateRuns;

public final class UpdateRunsImpl implements UpdateRuns {
    private static final ClientLogger LOGGER = new ClientLogger(UpdateRunsImpl.class);

    private final UpdateRunsClient innerClient;

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

    public UpdateRunsImpl(
        UpdateRunsClient innerClient,
        com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<UpdateRun> listByFleet(String resourceGroupName, String fleetName) {
        PagedIterable<UpdateRunInner> inner = this.serviceClient().listByFleet(resourceGroupName, fleetName);
        return Utils.mapPage(inner, inner1 -> new UpdateRunImpl(inner1, this.manager()));
    }

    public PagedIterable<UpdateRun> listByFleet(String resourceGroupName, String fleetName, Context context) {
        PagedIterable<UpdateRunInner> inner = this.serviceClient().listByFleet(resourceGroupName, fleetName, context);
        return Utils.mapPage(inner, inner1 -> new UpdateRunImpl(inner1, this.manager()));
    }

    public Response<UpdateRun> getWithResponse(
        String resourceGroupName, String fleetName, String updateRunName, Context context) {
        Response<UpdateRunInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, fleetName, updateRunName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new UpdateRunImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public UpdateRun get(String resourceGroupName, String fleetName, String updateRunName) {
        UpdateRunInner inner = this.serviceClient().get(resourceGroupName, fleetName, updateRunName);
        if (inner != null) {
            return new UpdateRunImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String fleetName, String updateRunName) {
        this.serviceClient().delete(resourceGroupName, fleetName, updateRunName);
    }

    public void delete(
        String resourceGroupName, String fleetName, String updateRunName, String ifMatch, Context context) {
        this.serviceClient().delete(resourceGroupName, fleetName, updateRunName, ifMatch, context);
    }

    public UpdateRun start(String resourceGroupName, String fleetName, String updateRunName) {
        UpdateRunInner inner = this.serviceClient().start(resourceGroupName, fleetName, updateRunName);
        if (inner != null) {
            return new UpdateRunImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public UpdateRun start(
        String resourceGroupName, String fleetName, String updateRunName, String ifMatch, Context context) {
        UpdateRunInner inner =
            this.serviceClient().start(resourceGroupName, fleetName, updateRunName, ifMatch, context);
        if (inner != null) {
            return new UpdateRunImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public UpdateRun stop(String resourceGroupName, String fleetName, String updateRunName) {
        UpdateRunInner inner = this.serviceClient().stop(resourceGroupName, fleetName, updateRunName);
        if (inner != null) {
            return new UpdateRunImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public UpdateRun stop(
        String resourceGroupName, String fleetName, String updateRunName, String ifMatch, Context context) {
        UpdateRunInner inner = this.serviceClient().stop(resourceGroupName, fleetName, updateRunName, ifMatch, context);
        if (inner != null) {
            return new UpdateRunImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public UpdateRun getById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fleetName = Utils.getValueFromIdByName(id, "fleets");
        if (fleetName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'fleets'.", id)));
        }
        String updateRunName = Utils.getValueFromIdByName(id, "updateRuns");
        if (updateRunName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'updateRuns'.", id)));
        }
        return this.getWithResponse(resourceGroupName, fleetName, updateRunName, Context.NONE).getValue();
    }

    public Response<UpdateRun> getByIdWithResponse(String id, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fleetName = Utils.getValueFromIdByName(id, "fleets");
        if (fleetName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'fleets'.", id)));
        }
        String updateRunName = Utils.getValueFromIdByName(id, "updateRuns");
        if (updateRunName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'updateRuns'.", id)));
        }
        return this.getWithResponse(resourceGroupName, fleetName, updateRunName, context);
    }

    public void deleteById(String id) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fleetName = Utils.getValueFromIdByName(id, "fleets");
        if (fleetName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'fleets'.", id)));
        }
        String updateRunName = Utils.getValueFromIdByName(id, "updateRuns");
        if (updateRunName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'updateRuns'.", id)));
        }
        String localIfMatch = null;
        this.delete(resourceGroupName, fleetName, updateRunName, localIfMatch, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, String ifMatch, Context context) {
        String resourceGroupName = Utils.getValueFromIdByName(id, "resourceGroups");
        if (resourceGroupName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'resourceGroups'.", id)));
        }
        String fleetName = Utils.getValueFromIdByName(id, "fleets");
        if (fleetName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'fleets'.", id)));
        }
        String updateRunName = Utils.getValueFromIdByName(id, "updateRuns");
        if (updateRunName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'updateRuns'.", id)));
        }
        this.delete(resourceGroupName, fleetName, updateRunName, ifMatch, context);
    }

    private UpdateRunsClient serviceClient() {
        return this.innerClient;
    }

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

    public UpdateRunImpl define(String name) {
        return new UpdateRunImpl(name, this.manager());
    }
}
