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

#include "packet_inject_test.h"
#include "packet_inject_main.h"

static int args_len(const char **args)
{
    int i = 0;
    while (args[i] != NULL)
    {
        i++;
    }
    return i;
}

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

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

    system(buf);
}

static int replace_file_string(const char *file, const char *old_str, const char *new_str)
{
#define BUFFER_SIZE 1024

    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 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[1024] = {0};
    char inject_pcap_json[1024] = {0};
    char diff_json_txt[1024] = {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);
}

void packet_inject_test(struct packet_inject_case *test)
{
    printf("\033[32m ============================================= \033[0m\n");
    printf("\033[32mTest: %s\033[0m\n", test->descriptor);
    printf("\033[32m ============================================= \033[0m\n");

    // create directory
    char dumpfile_dir[1024] = {0};
    snprintf(dumpfile_dir, sizeof(dumpfile_dir), "%s/input/", test->work_dir);
    system_cmd("rm -rf %s", test->work_dir);
    system_cmd("mkdir -p %s", dumpfile_dir);
    system_cmd("mkdir -p %s/log/", test->work_dir);

    // copy file to work directory
    for (int i = 0; i < MAX_COMPARISON; i++)
    {
        if (test->compares[i].expect_pcap)
        {
            system_cmd("cp %s/%s %s", test->input_prefix, test->compares[i].expect_pcap, test->work_dir);
        }
    }
    system_cmd("cp %s/%s %s", test->input_prefix, test->input_pcap, dumpfile_dir);
    system_cmd("cp -r conf %s/", test->work_dir);
    system_cmd("cp -r plugin %s/", test->work_dir);
    system_cmd("cp -r libpacket_inject_plugin.so %s/", test->work_dir);

    // run packet injector
    char cwd[2048] = {0};
    char temp[2048] = {0};
    getcwd(cwd, sizeof(cwd));
    chdir(test->work_dir);
    snprintf(temp, sizeof(temp), "dumpfile_dir = \"%s\"", dumpfile_dir);
    EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "mode = marsio", "mode = dumpfile") == 0);
    EXPECT_TRUE(replace_file_string("./conf/stellar.toml", "dumpfile_dir = \"/tmp/dumpfile/\"", temp) == 0);
    packet_inject_main(args_len(test->packet_injector_cmd), (char **)test->packet_injector_cmd);

    // compare pcap
    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);
        }
    }

    // clean work directory
    if (test->finish_clean_work_dir)
    {
        system_cmd("rm -rf %s", test->work_dir);
    }
    chdir(cwd);
}
