/*
 * Copyright 2021 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.linecorp.line.auth.fido.fido2.server.error;

import java.util.Arrays;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
public enum InternalErrorCode {
    // TODO: Need to refine
    SUCCESS(0),
    ATTESTATION_CERTIFICATE_ERROR(1),
    U2F_ATTESTATION_KEY_NOT_ECC_TYPE(2),
    U2F_ATTESTATION_USER_KEY_INVALID(3),
    U2F_ATTESTATION_AAGUID_INVALID(4),
    SIGNATURE_VERIFICATION_ERROR(4),
    PACKED_ATTESTATION_ALGORITHM_NOT_MATCHED(5),
    USER_PUBLIC_KEY_INVALID_KEY_SPEC(6),

    ANDROID_KEY_ATTESTATION_PUBLIC_KEY_NOT_MATCHED(7),
    ANDROID_KEY_ATTESTATION_DATA_NOT_FOUND(8),
    ANDROID_KEY_ATTESTATION_DATA_DECODING_FAIL(9),
    ANDROID_KEY_ATTESTATION_CERTIFICATE_INVALID(10),
    ANDROID_KEY_ATTESTATION_ATTESTATION_CHALLENGE_NOT_MATCH_TO_CLIENT_DATA_HASH(120),
    ANDROID_KEY_ATTESTATION_ATTESTATION_ALL_APPLICATION_SET(121),
    ANDROID_KEY_ATTESTATION_CERTIFICATE_REVOKED_CHECK_FAILED(122),

    ANDROID_SAFETYNET_ATTESTATION_CERTIFICATE_INVALID(11),
    ANDROID_SAFETYNET_ATTESTATION_NOT_SUPPORTED_SIGNATURE_ALGORITHM(12),
    ANDROID_SAFETYNET_ATTESTATION_CERTIFICATE_ISSUER_NAME_INVALID(13),
    ANDROID_SAFETYNET_ATTESTATION_CTS_PROFILE_MATCH_NOT_SET(14),
    ANDROID_SAFETYNET_ATTESTATION_DATA_INVALID(15),
    ANDROID_SAFETYNET_ATTESTATION_VERSION_EMPTY(16),
    ANDROID_SAFETYNET_ATTESTATION_NONCE_NOT_MATCHED(200),
    ANDROID_SAFETYNET_ATTESTATION_TIMESTAMP_INVALID(201),
    ANDROID_SAFETYNET_ATTESTATION_CERTIFICATE_NOT_INCLUDED(202),

    TPM_ATTESTATION_DATA_INVALID(202),
    TPM_ATTESTATION_INVALID_HASH_ALGORITHM(17),
    TPM_ATTESTATION_CERTIFICATE_VERSION_INVALID(18),
    TPM_ATTESTATION_CERTIFICATE_SUBJECT_FIELD_INVALID(19),
    TPM_ATTESTATION_CERTIFICATE_SAN_INVALID(20),
    TPM_ATTESTATION_CERTIFICATE_EXTENDED_KEY_USAGE_INVALID(21),
    TPM_ATTESTATION_CERTIFICATE_BASIC_CONSTRAINTS_INVALID(22),
    TPM_ATTESTATION_CERTIFICATE_INVALID(23),
    TPM_ATTESTATION_CERTINFO_TYPE_NOT_MATCHED(24),
    TPM_ATTESTATION_CERTINFO_MAGIC_NOT_MATCHED(25),
    TPM_ATTESTATION_CERTINFO_EXTRADATA_NOT_MATCHED(26),
    TPM_ATTESTATION_CERTINFO_PUBAREA_HASH_NOT_MATCHED(27),
    TPM_ATTESTATION_CERTINFO_PUBAREA_UNIQUE_NOT_MATCH_TO_CREDENTIAL_PUBKEY(100),

    APPLE_ANONYMOUS_ATTESTATION_PUBLIC_KEY_NOT_MATCHED(300),
    APPLE_ANONYMOUS_ATTESTATION_DATA_NOT_FOUND(301),
    APPLE_ANONYMOUS_ATTESTATION_DATA_DECODING_FAIL(302),

    PACKED_ATTESTATION_AAGUID_NOT_MATCHED(303),
    PACKED_ATTESTATION_DATA_DECODING_FAIL(304),

    METADATA_JSON_DESERIALIZE_FAIL(28),
    CRYPTO_OPERATION_EXCEPTION(29),
    SESSION_NOT_FOUND(30),
    SESSION_ALREADY_REVOKED(31),
    DUPLICATED_CREDENTIAL_ID(32),
    INVALID_CREDENTIAL_INSTANCE(33),
    CERTIFICATE_PATH_VALIDATION_FAIL(34),

    INVALID_REQUEST_BODY(34),

    INVALID_FORMAT_CLIENT_DATA_JSON(35),
    INVALID_OPERATION_TYPE(36),
    CHALLENGE_NOT_MATCHED(37),
    ORIGIN_NOT_MATCHED(38),
    TOKEN_BINDING_INFO_NOT_MATCHED(39),
    TOKEN_BINDING_STATUS_MISSING(40),
    INVALID_FORMAT_ATTESTATION_OBJECT(40),
    RPID_HASH_NOT_MATCHED(41),
    USER_PRESENCE_FLAG_NOT_SET(42),
    USER_VERIFICATION_FLAG_NOT_SET(43),
    ATTESTATION_SIGNATURE_VERIFICATION_FAIL(44),
    ATTESTATION_TRUSTWORTHINEES_CHECK_FAIL(45),
    RPID_NOT_FOUND(46),
    CREDENTIAL_ID_NOT_FOUND(47),
    CREDENTIAL_COUNT_INVALID(48),
    USER_HANDLE_NOT_MATCHED(49),
    METADATA_NOT_FOUND(50),
    INVALID_COSE_ALGORITHM(51),
    INVALID_ATTESTATION_FORMAT(52),
    INVALID_AUTHENTICATOR_DATA(53),
    CREDENTIAL_NOT_INCLUDED(54),
    CREDENTIAL_NOT_FOUND(55),
    ASSERTION_SIGNATURE_VERIFICATION_FAIL(56),
    METADATA_JSON_PARSING_FAIL(57),
    INVALID_ORIGIN(80),

    ECDAA_ALGORITHM_NOT_SUPPORTED(81),

    //U2F
    APPID_NOT_FOUND(100),
    KEYHANDLE_NOT_EXPECTED_ONE(101),
    INVALID_REGISTRATION_DATA(102),
    INVALID_SIGNATURE_DATA(103),
    INVALID_ORIGIN_OR_APPID_FORMAT(104),
    UNTRUSTED_FACETID(105),

    INTERNAL_SERVER_ERROR(999);



   @Getter private final int code;

    public static InternalErrorCode fromCode(int code) {
        return Arrays
                .stream(InternalErrorCode.values()).filter(e -> e.code == code)
                .findFirst()
                .get();
    }
}
