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

package com.azure.resourcemanager.deviceprovisioningservices.implementation;

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.deviceprovisioningservices.fluent.DpsCertificatesClient;
import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.CertificateListDescriptionInner;
import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.CertificateResponseInner;
import com.azure.resourcemanager.deviceprovisioningservices.fluent.models.VerificationCodeResponseInner;
import com.azure.resourcemanager.deviceprovisioningservices.models.CertificateListDescription;
import com.azure.resourcemanager.deviceprovisioningservices.models.CertificatePurpose;
import com.azure.resourcemanager.deviceprovisioningservices.models.CertificateResponse;
import com.azure.resourcemanager.deviceprovisioningservices.models.DpsCertificates;
import com.azure.resourcemanager.deviceprovisioningservices.models.VerificationCodeRequest;
import com.azure.resourcemanager.deviceprovisioningservices.models.VerificationCodeResponse;
import java.time.OffsetDateTime;

public final class DpsCertificatesImpl implements DpsCertificates {
    private static final ClientLogger LOGGER = new ClientLogger(DpsCertificatesImpl.class);

    private final DpsCertificatesClient innerClient;

    private final com.azure.resourcemanager.deviceprovisioningservices.IotDpsManager serviceManager;

    public DpsCertificatesImpl(
        DpsCertificatesClient innerClient,
        com.azure.resourcemanager.deviceprovisioningservices.IotDpsManager serviceManager) {
        this.innerClient = innerClient;
        this.serviceManager = serviceManager;
    }

    public CertificateResponse get(String certificateName, String resourceGroupName, String provisioningServiceName) {
        CertificateResponseInner inner =
            this.serviceClient().get(certificateName, resourceGroupName, provisioningServiceName);
        if (inner != null) {
            return new CertificateResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<CertificateResponse> getWithResponse(
        String certificateName,
        String resourceGroupName,
        String provisioningServiceName,
        String ifMatch,
        Context context) {
        Response<CertificateResponseInner> inner =
            this
                .serviceClient()
                .getWithResponse(certificateName, resourceGroupName, provisioningServiceName, ifMatch, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CertificateResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public void delete(
        String resourceGroupName, String ifMatch, String provisioningServiceName, String certificateName) {
        this.serviceClient().delete(resourceGroupName, ifMatch, provisioningServiceName, certificateName);
    }

    public Response<Void> deleteWithResponse(
        String resourceGroupName,
        String ifMatch,
        String provisioningServiceName,
        String certificateName,
        String certificateName1,
        byte[] certificateRawBytes,
        Boolean certificateIsVerified,
        CertificatePurpose certificatePurpose,
        OffsetDateTime certificateCreated,
        OffsetDateTime certificateLastUpdated,
        Boolean certificateHasPrivateKey,
        String certificateNonce,
        Context context) {
        return this
            .serviceClient()
            .deleteWithResponse(
                resourceGroupName,
                ifMatch,
                provisioningServiceName,
                certificateName,
                certificateName1,
                certificateRawBytes,
                certificateIsVerified,
                certificatePurpose,
                certificateCreated,
                certificateLastUpdated,
                certificateHasPrivateKey,
                certificateNonce,
                context);
    }

    public CertificateListDescription list(String resourceGroupName, String provisioningServiceName) {
        CertificateListDescriptionInner inner = this.serviceClient().list(resourceGroupName, provisioningServiceName);
        if (inner != null) {
            return new CertificateListDescriptionImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<CertificateListDescription> listWithResponse(
        String resourceGroupName, String provisioningServiceName, Context context) {
        Response<CertificateListDescriptionInner> inner =
            this.serviceClient().listWithResponse(resourceGroupName, provisioningServiceName, context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CertificateListDescriptionImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public VerificationCodeResponse generateVerificationCode(
        String certificateName, String ifMatch, String resourceGroupName, String provisioningServiceName) {
        VerificationCodeResponseInner inner =
            this
                .serviceClient()
                .generateVerificationCode(certificateName, ifMatch, resourceGroupName, provisioningServiceName);
        if (inner != null) {
            return new VerificationCodeResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<VerificationCodeResponse> generateVerificationCodeWithResponse(
        String certificateName,
        String ifMatch,
        String resourceGroupName,
        String provisioningServiceName,
        String certificateName1,
        byte[] certificateRawBytes,
        Boolean certificateIsVerified,
        CertificatePurpose certificatePurpose,
        OffsetDateTime certificateCreated,
        OffsetDateTime certificateLastUpdated,
        Boolean certificateHasPrivateKey,
        String certificateNonce,
        Context context) {
        Response<VerificationCodeResponseInner> inner =
            this
                .serviceClient()
                .generateVerificationCodeWithResponse(
                    certificateName,
                    ifMatch,
                    resourceGroupName,
                    provisioningServiceName,
                    certificateName1,
                    certificateRawBytes,
                    certificateIsVerified,
                    certificatePurpose,
                    certificateCreated,
                    certificateLastUpdated,
                    certificateHasPrivateKey,
                    certificateNonce,
                    context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new VerificationCodeResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

    public CertificateResponse verifyCertificate(
        String certificateName,
        String ifMatch,
        String resourceGroupName,
        String provisioningServiceName,
        VerificationCodeRequest request) {
        CertificateResponseInner inner =
            this
                .serviceClient()
                .verifyCertificate(certificateName, ifMatch, resourceGroupName, provisioningServiceName, request);
        if (inner != null) {
            return new CertificateResponseImpl(inner, this.manager());
        } else {
            return null;
        }
    }

    public Response<CertificateResponse> verifyCertificateWithResponse(
        String certificateName,
        String ifMatch,
        String resourceGroupName,
        String provisioningServiceName,
        VerificationCodeRequest request,
        String certificateName1,
        byte[] certificateRawBytes,
        Boolean certificateIsVerified,
        CertificatePurpose certificatePurpose,
        OffsetDateTime certificateCreated,
        OffsetDateTime certificateLastUpdated,
        Boolean certificateHasPrivateKey,
        String certificateNonce,
        Context context) {
        Response<CertificateResponseInner> inner =
            this
                .serviceClient()
                .verifyCertificateWithResponse(
                    certificateName,
                    ifMatch,
                    resourceGroupName,
                    provisioningServiceName,
                    request,
                    certificateName1,
                    certificateRawBytes,
                    certificateIsVerified,
                    certificatePurpose,
                    certificateCreated,
                    certificateLastUpdated,
                    certificateHasPrivateKey,
                    certificateNonce,
                    context);
        if (inner != null) {
            return new SimpleResponse<>(
                inner.getRequest(),
                inner.getStatusCode(),
                inner.getHeaders(),
                new CertificateResponseImpl(inner.getValue(), this.manager()));
        } else {
            return null;
        }
    }

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

    public Response<CertificateResponse> getByIdWithResponse(String id, String ifMatch, Context context) {
        String certificateName = Utils.getValueFromIdByName(id, "certificates");
        if (certificateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'certificates'.", 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 provisioningServiceName = Utils.getValueFromIdByName(id, "provisioningServices");
        if (provisioningServiceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisioningServices'.",
                                id)));
        }
        return this.getWithResponse(certificateName, resourceGroupName, provisioningServiceName, ifMatch, 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 provisioningServiceName = Utils.getValueFromIdByName(id, "provisioningServices");
        if (provisioningServiceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisioningServices'.",
                                id)));
        }
        String certificateName = Utils.getValueFromIdByName(id, "certificates");
        if (certificateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'certificates'.", id)));
        }
        String localIfMatch = null;
        String localCertificateName1 = null;
        byte[] localCertificateRawBytes = null;
        Boolean localCertificateIsVerified = null;
        CertificatePurpose localCertificatePurpose = null;
        OffsetDateTime localCertificateCreated = null;
        OffsetDateTime localCertificateLastUpdated = null;
        Boolean localCertificateHasPrivateKey = null;
        String localCertificateNonce = null;
        this
            .deleteWithResponse(
                resourceGroupName,
                localIfMatch,
                provisioningServiceName,
                certificateName,
                localCertificateName1,
                localCertificateRawBytes,
                localCertificateIsVerified,
                localCertificatePurpose,
                localCertificateCreated,
                localCertificateLastUpdated,
                localCertificateHasPrivateKey,
                localCertificateNonce,
                Context.NONE);
    }

    public Response<Void> deleteByIdWithResponse(
        String id,
        String ifMatch,
        String certificateName1,
        byte[] certificateRawBytes,
        Boolean certificateIsVerified,
        CertificatePurpose certificatePurpose,
        OffsetDateTime certificateCreated,
        OffsetDateTime certificateLastUpdated,
        Boolean certificateHasPrivateKey,
        String certificateNonce,
        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 provisioningServiceName = Utils.getValueFromIdByName(id, "provisioningServices");
        if (provisioningServiceName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String
                            .format(
                                "The resource ID '%s' is not valid. Missing path segment 'provisioningServices'.",
                                id)));
        }
        String certificateName = Utils.getValueFromIdByName(id, "certificates");
        if (certificateName == null) {
            throw LOGGER
                .logExceptionAsError(
                    new IllegalArgumentException(
                        String.format("The resource ID '%s' is not valid. Missing path segment 'certificates'.", id)));
        }
        return this
            .deleteWithResponse(
                resourceGroupName,
                ifMatch,
                provisioningServiceName,
                certificateName,
                certificateName1,
                certificateRawBytes,
                certificateIsVerified,
                certificatePurpose,
                certificateCreated,
                certificateLastUpdated,
                certificateHasPrivateKey,
                certificateNonce,
                context);
    }

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

    private com.azure.resourcemanager.deviceprovisioningservices.IotDpsManager manager() {
        return this.serviceManager;
    }

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