#include "dvd_drive.h"

#include <stdio.h>
#include <string.h>

typedef struct {
    const char *label;
    const char *vendor;
    const char *product;
    const char *revision;
    bool exact;
    u_int32_t type;
    u_int32_t cdb;
    u_int32_t gate;
    u_int32_t blocks;
    int method;
    const char *record_id;
    const char *tokens;
} profile_case;

static int check_case(const profile_case *test) {
    dvd_hlds_e7_profile_info info;
    bool exact = dvd_lookup_hlds_e7_profile(
        test->vendor,
        test->product,
        test->revision,
        &info);
    u_int32_t detected = dvd_detect_hlds_e7_type_for_identity(
        test->vendor,
        test->product,
        test->revision);

    if (exact != test->exact) {
        fprintf(stderr,
            "FAIL: %s exact=%d expected=%d\n",
            test->label, (int) exact, (int) test->exact);
        return 0;
    }

    if (detected != test->type) {
        fprintf(stderr,
            "FAIL: %s type=%u expected=%u\n",
            test->label, detected, test->type);
        return 0;
    }

    if (!exact) {
        printf("PASS: %s legacy type=%u\n", test->label, detected);
        return 1;
    }

    if (
        info.type != test->type ||
        info.static_cdb_base != test->cdb ||
        info.static_gate != test->gate ||
        info.mem_blocks != test->blocks ||
        info.preferred_method != test->method ||
        !info.tokens ||
        strcmp(info.tokens, test->tokens) != 0 ||
        !info.record_id ||
        strcmp(info.record_id, test->record_id) != 0
    ) {
        fprintf(stderr,
            "FAIL: %s type=%u cdb=0x%03x gate=0x%08x "
            "blocks=%u method=%d tokens=%s record=%s\n",
            test->label,
            info.type,
            info.static_cdb_base,
            info.static_gate,
            info.mem_blocks,
            info.preferred_method,
            info.tokens ? info.tokens : "(null)",
            info.record_id ? info.record_id : "(null)");
        return 0;
    }

    printf("PASS: %s exact cdb=0x%03x gate=0x%08x\n",
        test->label, info.static_cdb_base, info.static_gate);
    return 1;
}

int main(void) {
    static const profile_case cases[] = {
        {
            "A102 live inquiry alias",
            "HL-DT-ST", "CDRW/DVD GCC4243", "A102",
            true, 3, 0x880U, 0x9003731dU, 5, 8,
            "hybrid_identity_v3:stage5b_0439",
            "HL;IT;RPC;RPC_JCS3;RPC_SUFFIX"
        },
        {
            "A102 canonical alias",
            "HL-DT-ST", "GCC-4243N", "a102",
            true, 3, 0x880U, 0x9003731dU, 5, 8,
            "hybrid_identity_v3:stage5b_0439",
            "HL;IT;RPC;RPC_JCS3;RPC_SUFFIX"
        },
        {
            "B101 live inquiry alias",
            "HL-DT-ST", "CDRW/DVD GCC4244", "B101",
            true, 3, 0x894U, 0x900386d2U, 5, 8,
            "hybrid_identity_v3:stage5b_0426",
            "HL;IT;RPC;RPC_JCS3;RPC_SUFFIX"
        },
        {
            "B101 canonical alias",
            "HL-DT-ST", "GCC-4244N", "b101",
            true, 3, 0x894U, 0x900386d2U, 5, 8,
            "hybrid_identity_v3:stage5b_0426",
            "HL;IT;RPC;RPC_JCS3;RPC_SUFFIX"
        },
        {
            "A101 existing canonical profile",
            "HL-DT-ST", "GCC-4241N", "A101",
            true, 21, 0x85cU, 0x900356b7U, 1, 8,
            "stage5b_0064",
            "HL;IT;RPC;RPC_JCS3;RPC_SUFFIX"
        },
        {
            "GDR-8050L 0012 hybrid operating identity",
            "HL-DT-ST", "DVD-ROM GDR8050L", "0012",
            true, 44, 0x5d0U, 0x90026160U, 1, 8,
            "hybrid_identity_v3:gdr8050l_0012",
            "HL;IT;RPC;RPC_JD4_SPACE;RPC_SUFFIX"
        },
        {
            "GDR-8050L 0L23 has no false exact metadata",
            "HL-DT-ST", "DVD-ROM GDR8050L", "0L23",
            false, 44, 0, 0, 0, 0, NULL, NULL
        },
        {
            "A103 conservative Type3 fallback",
            "HL-DT-ST", "CDRW/DVD GCC4243", "A103",
            false, 3, 0, 0, 0, 0, NULL, NULL
        },
        {
            "B102 conservative Type3 fallback",
            "HL-DT-ST", "CDRW/DVD GCC4244", "B102",
            false, 3, 0, 0, 0, 0, NULL, NULL
        },
        {
            "non-HL vendor rejected",
            "NOT-HL", "CDRW/DVD GCC4243", "A102",
            false, 0, 0, 0, 0, 0, NULL, NULL
        }
    };
    size_t i;
    int ok = 1;

    for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++)
        ok &= check_case(&cases[i]);

    if (!ok)
        return 1;

    printf("HLDS E7 PROFILE SELECTION TESTS: PASS\n");
    return 0;
}
