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

package com.azure.resourcemanager.managedapplications.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.managedapplications.fluent.ApplicationsClient;
import com.azure.resourcemanager.managedapplications.fluent.models.ApplicationInner;
import com.azure.resourcemanager.managedapplications.models.Application;
import com.azure.resourcemanager.managedapplications.models.Applications;

public final class ApplicationsImpl implements Applications {
    private static final ClientLogger LOGGER = new ClientLogger(ApplicationsImpl.class);

    private final ApplicationsClient innerClient;

    private final com.azure.resourcemanager.managedapplications.ApplicationManager serviceManager;

    public ApplicationsImpl(
        ApplicationsClient innerClient,
        com.azure.resourcemanager.managedapplications.ApplicationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<Application> getByResourceGroupWithResponse(
        String resourceGroupName, String applicationName, Context context) {
        Response<ApplicationInner> inner =
            this.serviceClient().getByResourceGroupWithResponse(resourceGroupName, applicationName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplicationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Application getByResourceGroup(String resourceGroupName, String applicationName) {
        ApplicationInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, applicationName);
        if (inner != null) {
            return new ApplicationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

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

    public PagedIterable<Application> listByResourceGroup(String resourceGroupName, Context context) {
        PagedIterable<ApplicationInner> inner = this.serviceClient().listByResourceGroup(resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new ApplicationImpl(inner1, this.manager()));
    }

    public PagedIterable<Application> list() {
        PagedIterable<ApplicationInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new ApplicationImpl(inner1, this.manager()));
    }

    public PagedIterable<Application> list(Context context) {
        PagedIterable<ApplicationInner> inner = this.serviceClient().list(context);
        return Utils.mapPage(inner, inner1 -> new ApplicationImpl(inner1, this.manager()));
    }

    public Response<Application> getByIdWithResponse(String applicationId, Context context) {
        Response<ApplicationInner> inner = this.serviceClient().getByIdWithResponse(applicationId, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplicationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Application getById(String applicationId) {
        ApplicationInner inner = this.serviceClient().getById(applicationId);
        if (inner != null) {
            return new ApplicationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void deleteById(String applicationId) {
        this.serviceClient().deleteById(applicationId);
    }

    public void deleteById(String applicationId, Context context) {
        this.serviceClient().deleteById(applicationId, context);
    }

    public Application createOrUpdateById(String applicationId, ApplicationInner parameters) {
        ApplicationInner inner = this.serviceClient().createOrUpdateById(applicationId, parameters);
        if (inner != null) {
            return new ApplicationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Application createOrUpdateById(String applicationId, ApplicationInner parameters, Context context) {
        ApplicationInner inner = this.serviceClient().createOrUpdateById(applicationId, parameters, context);
        if (inner != null) {
            return new ApplicationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<Application> updateByIdWithResponse(
        String applicationId, ApplicationInner parameters, Context context) {
        Response<ApplicationInner> inner =
            this.serviceClient().updateByIdWithResponse(applicationId, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new ApplicationImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public Application updateById(String applicationId) {
        ApplicationInner inner = this.serviceClient().updateById(applicationId);
        if (inner != null) {
            return new ApplicationImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    private com.azure.resourcemanager.managedapplications.ApplicationManager manager() {
        return this.serviceManager;
    }

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