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

package com.azure.resourcemanager.recoveryservicessiterecovery.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.recoveryservicessiterecovery.fluent.ReplicationRecoveryPlansClient;
import com.azure.resourcemanager.recoveryservicessiterecovery.fluent.models.RecoveryPlanInner;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.RecoveryPlan;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.RecoveryPlanPlannedFailoverInput;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.RecoveryPlanTestFailoverCleanupInput;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.RecoveryPlanTestFailoverInput;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.RecoveryPlanUnplannedFailoverInput;
import com.azure.resourcemanager.recoveryservicessiterecovery.models.ReplicationRecoveryPlans;

public final class ReplicationRecoveryPlansImpl implements ReplicationRecoveryPlans {
    private static final ClientLogger LOGGER = new ClientLogger(ReplicationRecoveryPlansImpl.class);

    private final ReplicationRecoveryPlansClient innerClient;

    private final com.azure.resourcemanager.recoveryservicessiterecovery.SiteRecoveryManager serviceManager;

    public ReplicationRecoveryPlansImpl(
        ReplicationRecoveryPlansClient innerClient,
        com.azure.resourcemanager.recoveryservicessiterecovery.SiteRecoveryManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public PagedIterable<RecoveryPlan> list(String resourceName, String resourceGroupName) {
        PagedIterable<RecoveryPlanInner> inner = this.serviceClient().list(resourceName, resourceGroupName);
        return Utils.mapPage(inner, inner1 -> new RecoveryPlanImpl(inner1, this.manager()));
    }

    public PagedIterable<RecoveryPlan> list(String resourceName, String resourceGroupName, Context context) {
        PagedIterable<RecoveryPlanInner> inner = this.serviceClient().list(resourceName, resourceGroupName, context);
        return Utils.mapPage(inner, inner1 -> new RecoveryPlanImpl(inner1, this.manager()));
    }

    public Response<RecoveryPlan> getWithResponse(
        String resourceName, String resourceGroupName, String recoveryPlanName, Context context) {
        Response<RecoveryPlanInner> inner =
            this.serviceClient().getWithResponse(resourceName, resourceGroupName, recoveryPlanName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new RecoveryPlanImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public RecoveryPlan get(String resourceName, String resourceGroupName, String recoveryPlanName) {
        RecoveryPlanInner inner = this.serviceClient().get(resourceName, resourceGroupName, recoveryPlanName);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public void delete(String resourceName, String resourceGroupName, String recoveryPlanName) {
        this.serviceClient().delete(resourceName, resourceGroupName, recoveryPlanName);
    }

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

    public RecoveryPlan failoverCancel(String resourceName, String resourceGroupName, String recoveryPlanName) {
        RecoveryPlanInner inner =
            this.serviceClient().failoverCancel(resourceName, resourceGroupName, recoveryPlanName);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan failoverCancel(
        String resourceName, String resourceGroupName, String recoveryPlanName, Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().failoverCancel(resourceName, resourceGroupName, recoveryPlanName, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan failoverCommit(String resourceName, String resourceGroupName, String recoveryPlanName) {
        RecoveryPlanInner inner =
            this.serviceClient().failoverCommit(resourceName, resourceGroupName, recoveryPlanName);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan failoverCommit(
        String resourceName, String resourceGroupName, String recoveryPlanName, Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().failoverCommit(resourceName, resourceGroupName, recoveryPlanName, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan plannedFailover(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanPlannedFailoverInput input) {
        RecoveryPlanInner inner =
            this.serviceClient().plannedFailover(resourceName, resourceGroupName, recoveryPlanName, input);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan plannedFailover(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanPlannedFailoverInput input,
        Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().plannedFailover(resourceName, resourceGroupName, recoveryPlanName, input, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan reprotect(String resourceName, String resourceGroupName, String recoveryPlanName) {
        RecoveryPlanInner inner = this.serviceClient().reprotect(resourceName, resourceGroupName, recoveryPlanName);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan reprotect(
        String resourceName, String resourceGroupName, String recoveryPlanName, Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().reprotect(resourceName, resourceGroupName, recoveryPlanName, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan testFailover(
        String resourceName, String resourceGroupName, String recoveryPlanName, RecoveryPlanTestFailoverInput input) {
        RecoveryPlanInner inner =
            this.serviceClient().testFailover(resourceName, resourceGroupName, recoveryPlanName, input);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan testFailover(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanTestFailoverInput input,
        Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().testFailover(resourceName, resourceGroupName, recoveryPlanName, input, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan testFailoverCleanup(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanTestFailoverCleanupInput input) {
        RecoveryPlanInner inner =
            this.serviceClient().testFailoverCleanup(resourceName, resourceGroupName, recoveryPlanName, input);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan testFailoverCleanup(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanTestFailoverCleanupInput input,
        Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().testFailoverCleanup(resourceName, resourceGroupName, recoveryPlanName, input, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan unplannedFailover(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanUnplannedFailoverInput input) {
        RecoveryPlanInner inner =
            this.serviceClient().unplannedFailover(resourceName, resourceGroupName, recoveryPlanName, input);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public RecoveryPlan unplannedFailover(
        String resourceName,
        String resourceGroupName,
        String recoveryPlanName,
        RecoveryPlanUnplannedFailoverInput input,
        Context context) {
        RecoveryPlanInner inner =
            this.serviceClient().unplannedFailover(resourceName, resourceGroupName, recoveryPlanName, input, context);
        if (inner != null) {
            return new RecoveryPlanImpl(inner, this.manager());
        } else {
            return null;
        }
    }

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

    public Response<RecoveryPlan> getByIdWithResponse(String id, Context context) {
        String resourceName = Utils.getValueFromIdByName(id, "vaults");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'vaults'.", 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 recoveryPlanName = Utils.getValueFromIdByName(id, "replicationRecoveryPlans");
        if (recoveryPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationRecoveryPlans'.",
                                id)));
        }
        return this.getWithResponse(resourceName, resourceGroupName, recoveryPlanName, context);
    }

    public void deleteById(String id) {
        String resourceName = Utils.getValueFromIdByName(id, "vaults");
        if (resourceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'vaults'.", 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 recoveryPlanName = Utils.getValueFromIdByName(id, "replicationRecoveryPlans");
        if (recoveryPlanName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'replicationRecoveryPlans'.",
                                id)));
        }
        this.delete(resourceName, resourceGroupName, recoveryPlanName, Context.NONE);
    }

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

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

    private com.azure.resourcemanager.recoveryservicessiterecovery.SiteRecoveryManager manager() {
        return this.serviceManager;
    }

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