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

package com.azure.resourcemanager.mediaservices.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.mediaservices.fluent.MediaservicesClient;
import com.azure.resourcemanager.mediaservices.fluent.models.EdgePoliciesInner;
import com.azure.resourcemanager.mediaservices.fluent.models.MediaServiceInner;
import com.azure.resourcemanager.mediaservices.models.EdgePolicies;
import com.azure.resourcemanager.mediaservices.models.ListEdgePoliciesInput;
import com.azure.resourcemanager.mediaservices.models.MediaService;
import com.azure.resourcemanager.mediaservices.models.Mediaservices;
import com.azure.resourcemanager.mediaservices.models.SyncStorageKeysInput;

public final class MediaservicesImpl implements Mediaservices {
    private static final ClientLogger LOGGER = new ClientLogger(MediaservicesImpl.class);

    private final MediaservicesClient innerClient;

    private final com.azure.resourcemanager.mediaservices.MediaServicesManager serviceManager;

    public MediaservicesImpl(
        MediaservicesClient innerClient, com.azure.resourcemanager.mediaservices.MediaServicesManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

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

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

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

    public MediaService getByResourceGroup(String resourceGroupName, String accountName) {
        MediaServiceInner inner = this.serviceClient().getByResourceGroup(resourceGroupName, accountName);
        if (inner != null) {
            return new MediaServiceImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

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

    public Response<Void> syncStorageKeysWithResponse(
        String resourceGroupName, String accountName, SyncStorageKeysInput parameters, Context context) {
        return this.serviceClient().syncStorageKeysWithResponse(resourceGroupName, accountName, parameters, context);
    }

    public void syncStorageKeys(String resourceGroupName, String accountName, SyncStorageKeysInput parameters) {
        this.serviceClient().syncStorageKeys(resourceGroupName, accountName, parameters);
    }

    public Response<EdgePolicies> listEdgePoliciesWithResponse(
        String resourceGroupName, String accountName, ListEdgePoliciesInput parameters, Context context) {
        Response<EdgePoliciesInner> inner =
            this.serviceClient().listEdgePoliciesWithResponse(resourceGroupName, accountName, parameters, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new EdgePoliciesImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public EdgePolicies listEdgePolicies(
        String resourceGroupName, String accountName, ListEdgePoliciesInput parameters) {
        EdgePoliciesInner inner = this.serviceClient().listEdgePolicies(resourceGroupName, accountName, parameters);
        if (inner != null) {
            return new EdgePoliciesImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public PagedIterable<MediaService> list() {
        PagedIterable<MediaServiceInner> inner = this.serviceClient().list();
        return Utils.mapPage(inner, inner1 -> new MediaServiceImpl(inner1, this.manager()));
    }

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

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

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

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

    private com.azure.resourcemanager.mediaservices.MediaServicesManager manager() {
        return this.serviceManager;
    }

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