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

package com.azure.resourcemanager.devtestlabs.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.devtestlabs.fluent.GlobalSchedulesClient;
import com.azure.resourcemanager.devtestlabs.fluent.models.ScheduleInner;
import com.azure.resourcemanager.devtestlabs.models.GlobalSchedules;
import com.azure.resourcemanager.devtestlabs.models.RetargetScheduleProperties;
import com.azure.resourcemanager.devtestlabs.models.Schedule;

public final class GlobalSchedulesImpl implements GlobalSchedules {
    private static final ClientLogger LOGGER = new ClientLogger(GlobalSchedulesImpl.class);

    private final GlobalSchedulesClient innerClient;

    private final com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager;

    public GlobalSchedulesImpl(
        GlobalSchedulesClient innerClient, com.azure.resourcemanager.devtestlabs.DevTestLabsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

    public PagedIterable<Schedule> list(String expand, String filter, Integer top, String orderby, Context context) {
        PagedIterable<ScheduleInner> inner = this.serviceClient().list(expand, filter, top, orderby, context);
        return Utils.mapPage(inner, inner1 -> new ScheduleImpl(inner1, this.manager()));
    }

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

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

    public Response<Schedule> getByResourceGroupWithResponse(
        String resourceGroupName, String name, String expand, Context context) {
        Response<ScheduleInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, name, expand, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ScheduleImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

    public Response<Void> deleteByResourceGroupWithResponse(String resourceGroupName, String name, Context context) {
        return this.serviceClient().deleteWithResponse(resourceGroupName, name, context);
    }

    public void deleteByResourceGroup(String resourceGroupName, String name) {
        this.serviceClient().delete(resourceGroupName, name);
    }

    public void execute(String resourceGroupName, String name) {
        this.serviceClient().execute(resourceGroupName, name);
    }

    public void execute(String resourceGroupName, String name, Context context) {
        this.serviceClient().execute(resourceGroupName, name, context);
    }

    public void retarget(String resourceGroupName, String name, RetargetScheduleProperties retargetScheduleProperties) {
        this.serviceClient().retarget(resourceGroupName, name, retargetScheduleProperties);
    }

    public void retarget(
        String resourceGroupName, String name, RetargetScheduleProperties retargetScheduleProperties, Context context) {
        this.serviceClient().retarget(resourceGroupName, name, retargetScheduleProperties, 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 name = Utils.getValueFromIdByName(id, "schedules");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        String localExpand = null;
        return this.getByResourceGroupWithResponse(resourceGroupName, name, localExpand, Context.NONE).getValue();
    }

    public Response<Schedule> getByIdWithResponse(String id, String expand, 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 name = Utils.getValueFromIdByName(id, "schedules");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        return this.getByResourceGroupWithResponse(resourceGroupName, name, expand, 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 name = Utils.getValueFromIdByName(id, "schedules");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        this.deleteByResourceGroupWithResponse(resourceGroupName, name, Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(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 name = Utils.getValueFromIdByName(id, "schedules");
        if (name == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'schedules'.", id)));
        }
        return this.deleteByResourceGroupWithResponse(resourceGroupName, name, context);
    }

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

    private com.azure.resourcemanager.devtestlabs.DevTestLabsManager manager() {
        return this.serviceManager;
    }

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