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

package com.azure.resourcemanager.desktopvirtualization.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.desktopvirtualization.fluent.MsixPackagesClient;
import com.azure.resourcemanager.desktopvirtualization.fluent.models.MsixPackageInner;
import com.azure.resourcemanager.desktopvirtualization.models.MsixPackage;
import com.azure.resourcemanager.desktopvirtualization.models.MsixPackages;

public final class MsixPackagesImpl implements MsixPackages {
    private static final ClientLogger LOGGER = new ClientLogger(MsixPackagesImpl.class);

    private final MsixPackagesClient innerClient;

    private final com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager;

    public MsixPackagesImpl(
        MsixPackagesClient innerClient,
        com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public Response<MsixPackage> getWithResponse(
        String resourceGroupName, String hostPoolName, String msixPackageFullName, Context context) {
        Response<MsixPackageInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, hostPoolName, msixPackageFullName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new MsixPackageImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public MsixPackage get(String resourceGroupName, String hostPoolName, String msixPackageFullName) {
        MsixPackageInner inner = this.serviceClient().get(resourceGroupName, hostPoolName, msixPackageFullName);
        if (inner != null) {
            return new MsixPackageImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public void delete(String resourceGroupName, String hostPoolName, String msixPackageFullName) {
        this.serviceClient().delete(resourceGroupName, hostPoolName, msixPackageFullName);
    }

    public PagedIterable<MsixPackage> list(String resourceGroupName, String hostPoolName) {
        PagedIterable<MsixPackageInner> inner = this.serviceClient().list(resourceGroupName, hostPoolName);
        return Utils.mapPage(inner, inner1 -> new MsixPackageImpl(inner1, this.manager()));
    }

    public PagedIterable<MsixPackage> list(
        String resourceGroupName,
        String hostPoolName,
        Integer pageSize,
        Boolean isDescending,
        Integer initialSkip,
        Context context) {
        PagedIterable<MsixPackageInner> inner =
            this.serviceClient().list(resourceGroupName, hostPoolName, pageSize, isDescending, initialSkip, context);
        return Utils.mapPage(inner, inner1 -> new MsixPackageImpl(inner1, this.manager()));
    }

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

    public Response<MsixPackage> 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 hostPoolName = Utils.getValueFromIdByName(id, "hostPools");
        if (hostPoolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hostPools'.", id)));
        }
        String msixPackageFullName = Utils.getValueFromIdByName(id, "msixPackages");
        if (msixPackageFullName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'msixPackages'.", id)));
        }
        return this.getWithResponse(resourceGroupName, hostPoolName, msixPackageFullName, 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 hostPoolName = Utils.getValueFromIdByName(id, "hostPools");
        if (hostPoolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hostPools'.", id)));
        }
        String msixPackageFullName = Utils.getValueFromIdByName(id, "msixPackages");
        if (msixPackageFullName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'msixPackages'.", id)));
        }
        this.deleteWithResponse(resourceGroupName, hostPoolName, msixPackageFullName, 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 hostPoolName = Utils.getValueFromIdByName(id, "hostPools");
        if (hostPoolName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'hostPools'.", id)));
        }
        String msixPackageFullName = Utils.getValueFromIdByName(id, "msixPackages");
        if (msixPackageFullName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'msixPackages'.", id)));
        }
        return this.deleteWithResponse(resourceGroupName, hostPoolName, msixPackageFullName, context);
    }

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

    private com.azure.resourcemanager.desktopvirtualization.DesktopVirtualizationManager manager() {
        return this.serviceManager;
    }

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