// Copyright 2023 Google LLC
//
// Licensed 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
//
//      http://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.

#ifndef NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
#define NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_

#include "nearby_protocol.h"

// Plaintext advertisement bytes
constexpr std::array<uint8_t, 2> V0AdvEmptyBytes{0x00, 0x03};
constexpr std::array<uint8_t, 2> V1AdvEmptyBytes{0x00, 0x00};
constexpr std::array<uint8_t, 3> V0AdvPlaintextBytes{0x00, 0x15, 0x03};
constexpr std::array<uint8_t, 6> V0AdvPlaintextMultiDeBytes{
    0x00, 0x15, 0x05, 0x26, 0x40, 0x40,
};
constexpr std::array<uint8_t, 5> V1AdvPlaintextBytes{0x20,  // Version header
                                                     0x00,  // format
                                                     0x02,  // section len
                                                     0x15,  // Tx power value 3
                                                     0x03};

// V0 encrypted advertisement data - ripped out of np_adv/tests/examples_v0.rs
constexpr std::array<uint8_t, 19> V0AdvEncryptedBytes{
    0x04, 0x22, 0x22, 0xD8, 0x22, 0x12, 0xEF, 0x16, 0xDB, 0xF8,
    0x72, 0xF2, 0xA3, 0xA7, 0xC0, 0xFA, 0x52, 0x48, 0xEC};
inline std::vector<uint8_t> V0AdvEncryptedMetadata = {
    0x8D, 0xD4, 0x7A, 0x58, 0x47, 0x30, 0x65, 0x89, 0x06, 0x16, 0x04, 0xCA,
    0x68, 0xCE, 0xB4, 0x18, 0xA7, 0x40, 0xB8, 0x36, 0xB0, 0x94, 0xB4, 0xC0,
    0x6C, 0xAF, 0x7C, 0x4A, 0x3B, 0x68, 0x28, 0x54, 0x8F, 0x36, 0xD5, 0x68,
    0x12, 0x6C, 0xC2, 0x63, 0x31, 0x4A, 0x48, 0xE4, 0x3B, 0xB3, 0xCF, 0xD4,
    0x10, 0x49, 0x59, 0xE7, 0x2B, 0xDE, 0xEB, 0x78, 0x44, 0x68,
};
inline std::string ExpectedV0DecryptedMetadata(
    R"({"name":"Alice","email":"alice@gmail.com"})");
constexpr std::array<uint8_t, 32> V0AdvKeySeed = {
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
    0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
};
constexpr std::array<uint8_t, 32> V0AdvLegacyIdentityTokenHmac = {
    0x09, 0xFE, 0x9E, 0x81, 0xB7, 0x3E, 0x5E, 0xCC, 0x76, 0x59, 0x57,
    0x71, 0xE0, 0x1F, 0xFB, 0x34, 0x38, 0xE7, 0x5F, 0x24, 0xA7, 0x69,
    0x56, 0xA0, 0xB8, 0xEA, 0x67, 0xD1, 0x1C, 0x3E, 0x36, 0xFD};

// V1 encrypted advertisement data - ripped out of np_adv/tests/examples_v1.rs
constexpr std::array<uint8_t, 101> V1AdvEncryptedBytes{
    0x20, 0x03, 0x9C, 0xED, 0x67, 0x93, 0x04, 0x6A, 0xBB, 0x1B, 0x1C, 0x40,
    0x45, 0x1F, 0x34, 0x6C, 0x03, 0xF3, 0xAA, 0x4F, 0x01, 0x8B, 0x7A, 0x5F,
    0x8A, 0x7B, 0x06, 0x29, 0xA4, 0x12, 0x27, 0x12, 0x63, 0xB4, 0x42, 0x76,
    0xDE, 0xE9, 0x01, 0x6D, 0xF5, 0x70, 0x1C, 0x09, 0xDF, 0xBE, 0x2E, 0x32,
    0xE5, 0x64, 0x8A, 0xDF, 0xB0, 0xB5, 0xE0, 0xC1, 0xD9, 0x76, 0x99, 0x4C,
    0x71, 0xFB, 0xB2, 0xBF, 0xA2, 0xB9, 0xC2, 0xEA, 0x95, 0xE6, 0x6B, 0xFD,
    0xD7, 0x93, 0x4D, 0xB5, 0x2D, 0xC2, 0x8D, 0x1E, 0x31, 0x32, 0x00, 0x88,
    0x38, 0xBA, 0x73, 0x39, 0x5E, 0x23, 0xC3, 0x6C, 0x2D, 0x50, 0x22, 0x5B,
    0xB5, 0xF4, 0x9C, 0x96, 0x37};
constexpr std::array<uint8_t, 32> V1AdvKeySeed = {
    0xF1, 0xEF, 0x9E, 0x34, 0xDC, 0x28, 0xBC, 0x37, 0x5B, 0x6E, 0x4A,
    0xC4, 0x52, 0xE6, 0x9C, 0xD3, 0x6D, 0xA9, 0xAB, 0x21, 0x5B, 0x02,
    0xB9, 0x10, 0x27, 0xC3, 0xA9, 0x53, 0xB5, 0x29, 0x4C, 0x36};
constexpr std::array<uint8_t, 32>
    V1AdvExpectedMicExtendedSaltIdentityTokenHmac = {0};
constexpr std::array<uint8_t, 32> V1AdvExpectedSignatureIdentityTokenHmac = {
    0x10, 0x05, 0x45, 0x27, 0x8F, 0xE6, 0x86, 0x5B, 0x51, 0xB0, 0x39,
    0xEA, 0xB8, 0xD7, 0x5F, 0x61, 0x41, 0xDD, 0x92, 0x62, 0xDD, 0x1A,
    0x9D, 0xDA, 0x98, 0xDA, 0xF2, 0xDC, 0xA9, 0xD2, 0x45, 0x50};
constexpr std::array<uint8_t, 32> V1AdvPublicKey = {
    0x39, 0xA0, 0x74, 0x81, 0x70, 0x46, 0xFF, 0x72, 0x59, 0xC7, 0x78,
    0x6C, 0x30, 0x7B, 0xC8, 0x71, 0x26, 0x34, 0xFF, 0x13, 0x61, 0x8C,
    0xE6, 0x22, 0x46, 0x62, 0x56, 0xFD, 0xC1, 0x7A, 0x01, 0xAE};
inline std::vector<uint8_t> V1AdvEncryptedMetadata = {
    0x7B, 0xE9, 0x66, 0x00, 0x9E, 0x14, 0x50, 0xDE, 0x96, 0xCB, 0x79,
    0x38, 0x93, 0xC5, 0x15, 0xE9, 0xC2, 0x6B, 0xE8, 0x03, 0x9F, 0x6C,
    0xCA, 0x94, 0xAD, 0x24, 0x13, 0x27, 0xC1, 0xDE, 0xBC, 0xC2, 0x29,
    0x88, 0xC0, 0xA5, 0x3B, 0xCA, 0x98, 0x4A, 0x42, 0xC3, 0xDE, 0xF5,
    0x1C, 0xAE, 0xFF, 0xC0, 0x02, 0xD2, 0xD2, 0x97, 0x8B, 0x52, 0x93,
    0x71, 0x07, 0x39, 0x87, 0x89, 0xCB, 0xCD, 0x79, 0x0A, 0x19, 0x0E,
    0x73, 0xEB, 0x24, 0xFA, 0x8F, 0x4C, 0xA7, 0xF3, 0x95, 0x0B, 0xED,
    0xEF, 0x27, 0x49, 0x8D, 0xE8, 0x7B, 0x88, 0x33, 0x16, 0x83, 0xF6,
    0xF1, 0xE6, 0x43, 0x22, 0x70, 0x70, 0xFD, 0x9B, 0xA6, 0x52, 0x35,
    0x0B, 0xBF, 0xBA, 0x0C, 0x20, 0xA3, 0x0C, 0xE7, 0xC7, 0xD0, 0x70};
inline std::string ExpectedV1DecryptedMetadata(
    "{\"uuid\":\"378845e1-2616-420d-86f5-674177a7504d\","
    "\"display_name\":\"Alice\",\"location\":\"Wonderland\"}");

// Data suitable for use directly through the C API (bypassing the C++ wrapper)
constexpr np_ffi::internal::RawAdvertisementPayload V0AdvMultiDeInternals{
    {7, {0x00, 0x03, 0x15, 0x05, 0x26, 0x00, 0x46}}};

// Wrapper types directly usable by C++ API
constexpr nearby_protocol::ByteBuffer<255> V0AdvEmptyBuffer(V0AdvEmptyBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvEmpty(V0AdvEmptyBuffer);

constexpr nearby_protocol::ByteBuffer<255> V0AdvPlaintextBuffer(
    V0AdvPlaintextBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvPlaintext(
    V0AdvPlaintextBuffer);

constexpr nearby_protocol::ByteBuffer<255> V0AdvPlaintextMultiDeBuffer(
    V0AdvPlaintextMultiDeBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvPlaintextMultiDe(
    V0AdvPlaintextMultiDeBuffer);

constexpr nearby_protocol::ByteBuffer<255> V1AdvPlaintextBuffer(
    V1AdvPlaintextBytes);
constexpr nearby_protocol::RawAdvertisementPayload V1AdvPlaintext(
    V1AdvPlaintextBuffer);

constexpr nearby_protocol::ByteBuffer<255> V0AdvEncryptedBuffer(
    V0AdvEncryptedBytes);
constexpr nearby_protocol::RawAdvertisementPayload V0AdvEncryptedPayload(
    V0AdvEncryptedBuffer);

constexpr nearby_protocol::ByteBuffer<255> V1AdvEncryptedBuffer(
    V1AdvEncryptedBytes);
constexpr nearby_protocol::RawAdvertisementPayload V1AdvEncrypted(
    V1AdvEncryptedBuffer);

void test_panic_handler(nearby_protocol::PanicReason reason);

nearby_protocol::V0MatchableCredential GenerateRandomCredentialV0();

nearby_protocol::V1MatchableCredential GenerateRandomCredentialV1();

template <uint32_t N>
std::array<uint8_t, N> create_random_array() {
  std::array<uint8_t, N> array{};
  std::generate(array.begin(), array.end(), []() { return rand(); });
  return array;
}

std::string generate_hex_string(size_t length);

#endif  // NEARBY_PRESENCE_NP_CPP_FFI_TESTS_SHARED_TEST_UTILS_H_
