#pragma once

#ifdef __cplusplus
extern "C"
{
#endif

#include <stdarg.h>
#include <unistd.h>
#include <sys/stat.h>
#include <limits.h>
#include <gtest/gtest.h>

#include "stellar/stellar.h"

#define BUFFER_SIZE 1024
#define MAX_COMPARISON 16

struct packet_inject_case
{
    const char *work_dir;

    const char *pcap_dir;
    const char *input_pcap;

    struct
    {
        const char *expect_pcap;
        const char *inject_pcap;
    } compares[MAX_COMPARISON];

    const char *plugin_config_file;
    const char *diff_skip_pattern;
};

static inline void system_cmd(const char *cmd, ...)
{
    char buf[PATH_MAX] = {0};
    va_list args;

    va_start(args, cmd);
    vsnprintf(buf, sizeof(buf), cmd, args);
    va_end(args);

    system(buf);
}

static inline int replace_file_string(const char *file, const char *old_str, const char *new_str)
{
    FILE *in_fp = fopen(file, "r");
    if (in_fp == NULL)
    {
        printf("Open file %s failed, %s\n", file, strerror(errno));
        return -1;
    }

    FILE *tmp_fp = tmpfile();
    if (tmp_fp == NULL)
    {
        printf("Create temporary file failed, %s\n", strerror(errno));
        fclose(in_fp);
        return -1;
    }

    size_t old_len = strlen(old_str);
    size_t new_len = strlen(new_str);
    char buff[BUFFER_SIZE];

    while (fgets(buff, BUFFER_SIZE, in_fp))
    {
        char *pos = buff;
        if ((pos = strstr(pos, old_str)))
        {
            fwrite(buff, 1, pos - buff, tmp_fp); // Write characters before the old_str
            fwrite(new_str, 1, new_len, tmp_fp); // Write the new_str
            pos += old_len;                      // Move past the old_str
            fwrite(pos, 1, strlen(pos), tmp_fp); // Write characters after the old_str
        }
        else
        {
            fputs(buff, tmp_fp); // Write the remaining part of the line
        }
    }

    fclose(in_fp);
    fseek(tmp_fp, 0, SEEK_SET);

    FILE *out_fp = fopen(file, "w");
    if (out_fp == NULL)
    {
        printf("Open file %s for writing failed, %s\n", file, strerror(errno));
        fclose(tmp_fp);
        return -1;
    }

    while (fgets(buff, BUFFER_SIZE, tmp_fp))
    {
        fputs(buff, out_fp); // Write the contents of the temporary file to the original file
    }

    fclose(tmp_fp);
    fclose(out_fp);

    return 0;
}

static inline void expect_cmp_inject(const char *expect_pcap_file, const char *inject_pcap_file, const char *diff_skip_pattern, int idx)
{
    struct stat s;
    char expect_pcap_json[PATH_MAX] = {0};
    char inject_pcap_json[PATH_MAX] = {0};
    char diff_json_txt[PATH_MAX] = {0};

    snprintf(expect_pcap_json, sizeof(expect_pcap_json), "expect_pcap_%d.json", idx);
    snprintf(inject_pcap_json, sizeof(inject_pcap_json), "inject_pcap_%d.json", idx);
    snprintf(diff_json_txt, sizeof(diff_json_txt), "json_diff_%d.txt", idx);

    stat(expect_pcap_file, &s);
    EXPECT_TRUE(s.st_size > 0);

    stat(inject_pcap_file, &s);
    EXPECT_TRUE(s.st_size > 0);

    printf("\033[32m tcpdump read expect pcap (%s) \033[0m\n", expect_pcap_file);
    system_cmd("tcpdump -r %s", expect_pcap_file);

    printf("\033[32m tcpdump read inject pcap (%s) \033[0m\n", inject_pcap_file);
    system_cmd("tcpdump -r %s", inject_pcap_file);

    system_cmd("tshark -r %s -T json | jq >> %s", expect_pcap_file, expect_pcap_json);
    system_cmd("tshark -r %s -T json | jq >> %s", inject_pcap_file, inject_pcap_json);

    stat(expect_pcap_json, &s);
    EXPECT_TRUE(s.st_size > 0);

    stat(inject_pcap_json, &s);
    EXPECT_TRUE(s.st_size > 0);

    system_cmd("diff %s %s %s >> %s", diff_skip_pattern, expect_pcap_json, inject_pcap_json, diff_json_txt);

    stat(diff_json_txt, &s);
    EXPECT_TRUE(s.st_size == 0);
}

static inline void packet_inject_test(struct packet_inject_case *test)
{
    // create directory
    char dumpfile_path[PATH_MAX] = {0};
    snprintf(dumpfile_path, sizeof(dumpfile_path), "%s/input/%s", test->work_dir, test->input_pcap);
    system_cmd("rm -rf %s", test->work_dir);
    system_cmd("mkdir -p %s/input/", test->work_dir);
    system_cmd("mkdir -p %s/log/", test->work_dir);
    system_cmd("mkdir -p %s/conf/", test->work_dir);
    system_cmd("mkdir -p %s/plugin/", test->work_dir);

    // copy file
    for (int i = 0; i < MAX_COMPARISON; i++)
    {
        if (test->compares[i].expect_pcap)
        {
            system_cmd("cp %s/%s %s", test->pcap_dir, test->compares[i].expect_pcap, test->work_dir);
        }
    }
    system_cmd("cp %s/%s                      %s/input/", test->pcap_dir, test->input_pcap, test->work_dir);
    system_cmd("cp conf/log.toml              %s/conf/", test->work_dir);
    system_cmd("cp conf/stellar.toml          %s/conf/", test->work_dir);
    system_cmd("cp conf/spec.toml             %s/plugin/", test->work_dir);
    system_cmd("cp conf/%s                    %s/plugin/inject.toml", test->plugin_config_file, test->work_dir);
    system_cmd("cp libpacket_inject.so        %s/plugin/", test->work_dir);

    // run
    char cwd[PATH_MAX] = {0};
    char temp[PATH_MAX * 2] = {0};
    getcwd(cwd, sizeof(cwd));
    chdir(test->work_dir);
    snprintf(temp, sizeof(temp), "dumpfile_path = \"%s\"", dumpfile_path);
    EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0);
    EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_path = \"/tmp/dumpfile/dumpfile.pcap\"", temp) == 0);

    const char *stellar_cfg_file = "./conf/stellar.toml";
    const char *plugin_cfg_file = "./plugin/spec.toml";
    const char *log_cfg_file = "./conf/log.toml";
    struct stellar *st = stellar_new(stellar_cfg_file, plugin_cfg_file, log_cfg_file);
    EXPECT_TRUE(st != NULL);
    stellar_run(st);
    stellar_free(st);

    // compare
    for (int i = 0; i < MAX_COMPARISON; i++)
    {
        if (test->compares[i].expect_pcap && test->compares[i].inject_pcap)
        {
            expect_cmp_inject(test->compares[i].expect_pcap, test->compares[i].inject_pcap, test->diff_skip_pattern, i + 1);
        }
    }
    chdir(cwd);
}

#ifdef __cplusplus
}
#endif
