/*
 *  author:yangwei
 *  create time:2021-8-21
 *
 */

#pragma GCC diagnostic ignored "-Wunused-parameter"

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <assert.h>

#include <gtest/gtest.h>

#include "stellar/stellar.h"
#include "stellar/session.h"
#include "stellar/stellar_mq.h"

#include "cjson/cJSON.h"


struct gtest_json_result
{
    cJSON *test_json_root;
    cJSON *expect_json_root;
    int result_count;
};

static struct gtest_json_result *gtest_result_new(const char *expect_json_path)
{
	struct gtest_json_result *para = (struct gtest_json_result *)calloc(1, sizeof(struct gtest_json_result));
	para->test_json_root = cJSON_CreateArray();

	FILE *file = fopen(expect_json_path, "rb");
    if(file)
	{
        fseek(file, 0, SEEK_END);
        long filesize = ftell(file);
        rewind(file);
        char *buffer = (char *)calloc(filesize + 1, 1);
		fread(buffer, 1, filesize, file);

		para->expect_json_root=cJSON_Parse(buffer);
		
		free(buffer);
		fclose(file);
    }
	para->result_count=1;//count start from 1
    return para;
}

static int gtest_result_compare(struct gtest_json_result *para)
{
	if(cJSON_GetArraySize(para->test_json_root)!=cJSON_GetArraySize(para->expect_json_root))
	{
		char *load_json_str = cJSON_Print(para->expect_json_root);
		printf("LOAD Raw:\n%s\n", load_json_str);
		free(load_json_str);
		char *result_json_str = cJSON_Print(para->test_json_root);
		printf("TEST Raw:\n%s\n", result_json_str);
		free(result_json_str);		
		return -1;
	}
	int compare_ret = cJSON_Compare(para->expect_json_root, para->test_json_root, 0);
	if (compare_ret != 1)
	{
		char *load_json_str = cJSON_Print(para->expect_json_root);
		printf("LOAD Raw:\n%s\n", load_json_str);
		free(load_json_str);
		char *result_json_str = cJSON_Print(para->test_json_root);
		printf("TEST Raw:\n%s\n", result_json_str);
		free(result_json_str);

		cJSON *t_load = para->expect_json_root->child, *t_test = para->test_json_root->child;
		while (t_load != NULL)
		{
			// print first diff item, then return;
			if(1 != cJSON_Compare(t_load, t_test, 0))
			{
				load_json_str = cJSON_Print(t_load);
				printf("LOAD Diff:\n%s\n", load_json_str);
				free(load_json_str);
				result_json_str = cJSON_Print(t_test);
				printf("TEST Diff:\n%s\n", result_json_str);
				free(result_json_str);
				return -1;
			}
			t_load = t_load->next;
			t_test = t_test->next;

		}
	}
	return compare_ret;
}

static void gtest_result_free(struct gtest_json_result *para)
{
	if(para)
	{
		if(para->test_json_root)cJSON_Delete(para->test_json_root);
		if(para->expect_json_root)cJSON_Delete(para->expect_json_root);	
		free(para);
	}	
	return;
}

void gtest_on_session_msg_expect_json(struct session *sess, int topic_id, const void *msg, void *per_session_ctx,
                                      void *plugin_env)
{
	struct gtest_json_result *g_test_para = (struct gtest_json_result *)plugin_env;

    cJSON *per_session_json = cJSON_Parse((const char *)msg);

    if (g_test_para->test_json_root)
    {
        char result_name[128] = "";
        sprintf(result_name, "APP_PROTO_IDENTIFY_RESULT_%d", g_test_para->result_count);
        cJSON_AddStringToObject(per_session_json, "name", result_name);
        cJSON_AddItemToArray(g_test_para->test_json_root, per_session_json);
    }
    g_test_para->result_count += 1;
}

/**********************************************
 * GTEST MAIN *
 **********************************************/

int main(int argc, char ** argv)
{
	::testing::InitGoogleTest(&argc, argv);

	EXPECT_EQ(argc, 2);

	if(argc != 2)
	{
		printf("Invalid Argument!!!\n Usage: ./[gtest_main] [/path/to/expect_json]\n");
		return -1;
	}

	char *expect_json_path=argv[1];
	struct gtest_json_result *g_test_para = gtest_result_new(expect_json_path);
	
	struct stellar *st=stellar_new("./conf/stellar.toml", "./plugin/spec.toml", "./conf/log.toml");
	EXPECT_TRUE(st!=NULL);
	
	int plugin_id=stellar_session_plugin_register(st, NULL, NULL, g_test_para);
	EXPECT_TRUE(plugin_id>=0);
	
	int expect_json_topic_id = stellar_mq_get_topic_id(st, "EXPECT_JSON");
	EXPECT_TRUE(expect_json_topic_id>=0);

	stellar_session_mq_subscribe(st, expect_json_topic_id, gtest_on_session_msg_expect_json, plugin_id);

	stellar_run(st);
	stellar_free(st);

    EXPECT_TRUE(g_test_para->expect_json_root != NULL && g_test_para->test_json_root != NULL);
	EXPECT_EQ(gtest_result_compare(g_test_para), 1);    

	gtest_result_free(g_test_para);

	return ::testing::Test::HasFailure() ? 1 : 0;
}