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

package com.azure.resourcemanager.containerservicefleet.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.containerservicefleet.fluent.FleetMembersClient;
import com.azure.resourcemanager.containerservicefleet.fluent.models.FleetMemberInner;
import com.azure.resourcemanager.containerservicefleet.models.FleetMember;
import com.azure.resourcemanager.containerservicefleet.models.FleetMembers;

public final class FleetMembersImpl implements FleetMembers {
    private static final ClientLogger LOGGER = new ClientLogger(FleetMembersImpl.class);

    private final FleetMembersClient innerClient;

    private final com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager;

    public FleetMembersImpl(
        FleetMembersClient innerClient,
        com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<FleetMember> listByFleet(String resourceGroupName, String fleetName) {
        PagedIterable<FleetMemberInner> inner = this.serviceClient().listByFleet(resourceGroupName, fleetName);
        return Utils.mapPage(inner, inner1 -> new FleetMemberImpl(inner1, this.manager()));
    }

    public PagedIterable<FleetMember> listByFleet(String resourceGroupName, String fleetName, Context context) {
        PagedIterable<FleetMemberInner> inner = this.serviceClient().listByFleet(resourceGroupName, fleetName, context);
        return Utils.mapPage(inner, inner1 -> new FleetMemberImpl(inner1, this.manager()));
    }

    public Response<FleetMember> getWithResponse(
        String resourceGroupName, String fleetName, String fleetMemberName, Context context) {
        Response<FleetMemberInner> inner =
            this.serviceClient().getWithResponse(resourceGroupName, fleetName, fleetMemberName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new FleetMemberImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public FleetMember get(String resourceGroupName, String fleetName, String fleetMemberName) {
        FleetMemberInner inner = this.serviceClient().get(resourceGroupName, fleetName, fleetMemberName);
        if (inner != null) {
            return new FleetMemberImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceGroupName, String fleetName, String fleetMemberName) {
        this.serviceClient().delete(resourceGroupName, fleetName, fleetMemberName);
    }

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

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

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

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

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

    private com.azure.resourcemanager.containerservicefleet.ContainerServiceFleetManager manager() {
        return this.serviceManager;
    }

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