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

package com.azure.resourcemanager.devcenter.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.devcenter.fluent.SchedulesClient;
import com.azure.resourcemanager.devcenter.fluent.models.ScheduleInner;
import com.azure.resourcemanager.devcenter.models.Schedule;
import com.azure.resourcemanager.devcenter.models.Schedules;

public final class SchedulesImpl implements Schedules {
    private static final ClientLogger LOGGER = new ClientLogger(SchedulesImpl.class);

    private final SchedulesClient innerClient;

    private final com.azure.resourcemanager.devcenter.DevCenterManager serviceManager;

    public SchedulesImpl(
        SchedulesClient innerClient, com.azure.resourcemanager.devcenter.DevCenterManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Schedule> listByPool(String resourceGroupName, String projectName, String poolName) {
        PagedIterable<ScheduleInner> inner = this.serviceClient().listByPool(resourceGroupName, projectName, poolName);
        return Utils.mapPage(inner, inner1 -> new ScheduleImpl(inner1, this.manager()));
    }

    public PagedIterable<Schedule> listByPool(
        String resourceGroupName, String projectName, String poolName, Integer top, Context context) {
        PagedIterable<ScheduleInner> inner =
            this.serviceClient().listByPool(resourceGroupName, projectName, poolName, top, context);
        return Utils.mapPage(inner, inner1 -> new ScheduleImpl(inner1, this.manager()));
    }

    public Response<Schedule> getWithResponse(
        String resourceGroupName,
        String projectName,
        String poolName,
        String scheduleName,
        Integer top,
        Context context) {
        Response<ScheduleInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, projectName, poolName, scheduleName, top, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ScheduleImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Schedule get(String resourceGroupName, String projectName, String poolName, String scheduleName) {
        ScheduleInner inner = this.serviceClient().get(resourceGroupName, projectName, poolName, scheduleName);
        if (inner != null) {
            return new ScheduleImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String projectName, String poolName, String scheduleName) {
        this.serviceClient().delete(resourceGroupName, projectName, poolName, scheduleName);
    }

    public void delete(
        String resourceGroupName,
        String projectName,
        String poolName,
        String scheduleName,
        Integer top,
        Context context) {
        this.serviceClient().delete(resourceGroupName, projectName, poolName, scheduleName, top, context);
    }

    public Schedule 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 projectName = Utils.getValueFromIdByName(id, "projects");
        if (projectName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'projects'.", id)));
        }
        String poolName = Utils.getValueFromIdByName(id, "pools");
        if (poolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'pools'.", id)));
        }
        String scheduleName = Utils.getValueFromIdByName(id, "schedules");
        if (scheduleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        Integer localTop = null;
        return this
            .getWithResponse(resourceGroupName, projectName, poolName, scheduleName, localTop, Context.NONE)
            .getValue();
    }

    public Response<Schedule> getByIdWithResponse(String id, Integer top, 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 projectName = Utils.getValueFromIdByName(id, "projects");
        if (projectName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'projects'.", id)));
        }
        String poolName = Utils.getValueFromIdByName(id, "pools");
        if (poolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'pools'.", id)));
        }
        String scheduleName = Utils.getValueFromIdByName(id, "schedules");
        if (scheduleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        return this.getWithResponse(resourceGroupName, projectName, poolName, scheduleName, top, 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 projectName = Utils.getValueFromIdByName(id, "projects");
        if (projectName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'projects'.", id)));
        }
        String poolName = Utils.getValueFromIdByName(id, "pools");
        if (poolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'pools'.", id)));
        }
        String scheduleName = Utils.getValueFromIdByName(id, "schedules");
        if (scheduleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        Integer localTop = null;
        this.delete(resourceGroupName, projectName, poolName, scheduleName, localTop, Context.NONE);
    }

    public void deleteByIdWithResponse(String id, Integer top, 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 projectName = Utils.getValueFromIdByName(id, "projects");
        if (projectName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'projects'.", id)));
        }
        String poolName = Utils.getValueFromIdByName(id, "pools");
        if (poolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'pools'.", id)));
        }
        String scheduleName = Utils.getValueFromIdByName(id, "schedules");
        if (scheduleName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        this.delete(resourceGroupName, projectName, poolName, scheduleName, top, context);
    }

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

    private com.azure.resourcemanager.devcenter.DevCenterManager manager() {
        return this.serviceManager;
    }

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