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

package com.azure.resourcemanager.imagebuilder.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.imagebuilder.fluent.TriggersClient;
import com.azure.resourcemanager.imagebuilder.fluent.models.TriggerInner;
import com.azure.resourcemanager.imagebuilder.models.Trigger;
import com.azure.resourcemanager.imagebuilder.models.Triggers;

public final class TriggersImpl implements Triggers {
    private static final ClientLogger LOGGER = new ClientLogger(TriggersImpl.class);

    private final TriggersClient innerClient;

    private final com.azure.resourcemanager.imagebuilder.ImageBuilderManager serviceManager;

    public TriggersImpl(
        TriggersClient innerClient, com.azure.resourcemanager.imagebuilder.ImageBuilderManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<Trigger> listByImageTemplate(String resourceGroupName, String imageTemplateName) {
        PagedIterable<TriggerInner> inner =
            this.serviceClient().listByImageTemplate(resourceGroupName, imageTemplateName);
        return Utils.mapPage(inner, inner1 -> new TriggerImpl(inner1, this.manager()));
    }

    public PagedIterable<Trigger> listByImageTemplate(
        String resourceGroupName, String imageTemplateName, Context context) {
        PagedIterable<TriggerInner> inner =
            this.serviceClient().listByImageTemplate(resourceGroupName, imageTemplateName, context);
        return Utils.mapPage(inner, inner1 -> new TriggerImpl(inner1, this.manager()));
    }

    public Response<Trigger> getWithResponse(
        String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        Response<TriggerInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, imageTemplateName, triggerName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new TriggerImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Trigger get(String resourceGroupName, String imageTemplateName, String triggerName) {
        TriggerInner inner = this.serviceClient().get(resourceGroupName, imageTemplateName, triggerName);
        if (inner != null) {
            return new TriggerImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String imageTemplateName, String triggerName) {
        this.serviceClient().delete(resourceGroupName, imageTemplateName, triggerName);
    }

    public void delete(String resourceGroupName, String imageTemplateName, String triggerName, Context context) {
        this.serviceClient().delete(resourceGroupName, imageTemplateName, triggerName, context);
    }

    public Trigger 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 imageTemplateName = Utils.getValueFromIdByName(id, "imageTemplates");
        if (imageTemplateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'imageTemplates'.", id)));
        }
        String triggerName = Utils.getValueFromIdByName(id, "triggers");
        if (triggerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'triggers'.", id)));
        }
        return this.getWithResponse(resourceGroupName, imageTemplateName, triggerName, Context.NONE).getValue();
    }

    public Response<Trigger> 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 imageTemplateName = Utils.getValueFromIdByName(id, "imageTemplates");
        if (imageTemplateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'imageTemplates'.", id)));
        }
        String triggerName = Utils.getValueFromIdByName(id, "triggers");
        if (triggerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'triggers'.", id)));
        }
        return this.getWithResponse(resourceGroupName, imageTemplateName, triggerName, 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 imageTemplateName = Utils.getValueFromIdByName(id, "imageTemplates");
        if (imageTemplateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'imageTemplates'.", id)));
        }
        String triggerName = Utils.getValueFromIdByName(id, "triggers");
        if (triggerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'triggers'.", id)));
        }
        this.delete(resourceGroupName, imageTemplateName, triggerName, Context.NONE);
    }

    public 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 imageTemplateName = Utils.getValueFromIdByName(id, "imageTemplates");
        if (imageTemplateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format("The resource ID '%s' is not valid. Missing path segment 'imageTemplates'.", id)));
        }
        String triggerName = Utils.getValueFromIdByName(id, "triggers");
        if (triggerName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'triggers'.", id)));
        }
        this.delete(resourceGroupName, imageTemplateName, triggerName, context);
    }

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

    private com.azure.resourcemanager.imagebuilder.ImageBuilderManager manager() {
        return this.serviceManager;
    }

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