#include "../test.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include "arc/std/parser.h"
#include "arc/std/parser/helpers.h"
#include "arc/std/lexer.h"
#include "arc/std/vector.h"
#include <stddef.h>
#include <stdlib.h>

const uint32_t TEST_PARSER_CHAR_OR_NUM   = 23;
const uint32_t TEST_PARSER_VARIABLE_NAME = 24;
const uint32_t TEST_PARSER_VARIABLE      = 25;

const uint32_t TEST_PARSER_ALPHA_LOWER_CHAR = 1;
const uint32_t TEST_PARSER_ALPHA_UPPER_CHAR = 2;
const uint32_t TEST_PARSER_ALPHA_CHAR       = 3;

void TEST_ParserData_CreateStringFn(void **data, ARC_ParserTagToken *parsedData, void *userData){
    ARC_String_Create((ARC_String **)data, NULL, 0);

    ARC_ParserData_HelperRecurseStringAdd((ARC_String **)data, parsedData);
}

void TEST_ParserData_DestroyStringFn(void *data, ARC_Bool clear, void *userData){
    if(data == NULL){
        return;
    }

    ARC_String_Destroy((ARC_String *)data);
}

uint32_t *charOrNumTokens[]      = { (uint32_t[]){ 1, ARC_LEXER_TOKEN_ALPHA_LOWER_CHAR }, (uint32_t[]){ 1, ARC_LEXER_TOKEN_NUMBER } };
uint32_t *variableNameTags[]     = { (uint32_t[]){ 2, TEST_PARSER_CHAR_OR_NUM, TEST_PARSER_VARIABLE_NAME }, (uint32_t[]){ 1, ARC_PARSER_TAG_LAMBDA } };
uint32_t *variableTokensOrTags[] = { (uint32_t[]){ 2, ARC_LEXER_TOKEN_ALPHA_LOWER_CHAR, TEST_PARSER_VARIABLE_NAME } };

//TODO: note how language function callbacks work, and how they use the parentData if createDataFn is NULL

ARC_ParserTag testTags[3] = {
    {
        TEST_PARSER_VARIABLE, //tagId
        variableTokensOrTags, //tokensOrTags
        1                     //tokenOrTagsSize
    },
    {
        TEST_PARSER_VARIABLE_NAME, //tagId
        variableNameTags,          //tokensOrTags
        2                          //tokenOrTagsSize
    },
    {
        TEST_PARSER_CHAR_OR_NUM, //tagId
        charOrNumTokens,         //tokensOrTags
        2                        //tokenOrTagsSize
    }
};

ARC_Array languageArray = {
    3,       //size
    testTags //data
};

void TEST_Parser_InitLexerRulesFn(ARC_Lexer *lexer){
    ARC_Lexer_InitBasicTokenRules(lexer);
}

ARC_TEST(Parser_Init){
    ARC_Parser *parser;
    ARC_Parser_Create(&parser, &languageArray, TEST_Parser_InitLexerRulesFn, NULL, NULL, NULL);

    ARC_Parser_Destroy(parser);

    ARC_CHECK(arc_errno == 0);
}

ARC_TEST(Parser_Basic_Parse){
    ARC_Parser *parser;
    ARC_Parser_Create(&parser, &languageArray, TEST_Parser_InitLexerRulesFn, NULL, NULL, NULL);

    ARC_String *tempString;


    /* ~ first test ~ */
    ARC_String_CreateWithStrlen(&tempString, "myvar1");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == 0);


    /* ~ second test ~ */
    ARC_String_CreateWithStrlen(&tempString, "z1xwvq");
    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);


    ARC_CHECK(arc_errno == 0);


    /* ~ third test ~ */
    ARC_String_CreateWithStrlen(&tempString, "z1234");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == 0);


    /* ~ fourth test ~ */
    ARC_String_CreateWithStrlen(&tempString, "aaaaa");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == 0);


    /* ~ cleanup ~ */
    ARC_Parser_Destroy(parser);
}

ARC_TEST(Parser_Basic_ParseError){
    ARC_Parser *parser;
    ARC_Parser_Create(&parser, &languageArray, TEST_Parser_InitLexerRulesFn, NULL, NULL, NULL);

    ARC_String *tempString;


    /* ~ first test ~ */
    ARC_String_CreateWithStrlen(&tempString, "!myVar1");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == ARC_ERRNO_DATA);


    /* ~ second test ~ */
    //check again with moved character
    arc_errno = 0;
    ARC_String_CreateWithStrlen(&tempString, "my!Var1");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == ARC_ERRNO_DATA);


    /* ~ third test ~ */
    //check again with moved character
    arc_errno = 0;
    ARC_String_CreateWithStrlen(&tempString, "myVar1!");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == ARC_ERRNO_DATA);


    /* ~ cleanup ~ */
    ARC_Parser_Destroy(parser);

    //reset for next test
    arc_errno = 0;
}

ARC_TEST(Parser_Basic_GetParsedValue){
    ARC_Parser *parser;

    ARC_ParserData_CreateFn  createStringFn  = TEST_ParserData_CreateStringFn;
    ARC_ParserData_DestroyFn destroyStringFn = TEST_ParserData_DestroyStringFn;

    ARC_Parser_Create(&parser, &languageArray, TEST_Parser_InitLexerRulesFn, &createStringFn, &destroyStringFn, NULL);

    ARC_String *tempString;


    /* ~ first test ~ */
    ARC_String_CreateWithStrlen(&tempString, "myvar1");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    ARC_CHECK(arc_errno == 0);

    ARC_String *checkValue = (ARC_String *)ARC_Parser_GetData(parser);
    ARC_CHECK(ARC_String_EqualsCStringWithStrlen(checkValue, "myvar1"));


    /* ~ cleanup ~ */
    ARC_Parser_Destroy(parser);
}

ARC_TEST(Parser_ParserLang_BasicVector){
    ARC_Vector *testLanguage;
    ARC_Vector_Create(&testLanguage, NULL, NULL);

    ARC_Vector_Add(testLanguage, testTags + 0);
    ARC_Vector_Add(testLanguage, testTags + 1);
    ARC_Vector_Add(testLanguage, testTags + 2);

    ARC_Parser *parser;
    ARC_Parser_CreateFromVector(&parser, testLanguage, TEST_Parser_InitLexerRulesFn, NULL, NULL, NULL);

    ARC_String *tempString;
    ARC_String_CreateWithStrlen(&tempString, "variablename");

    //this destroys string, so no need for cleanup
    ARC_Parser_Parse(parser, &tempString);

    //cleanup
    ARC_Parser_Destroy(parser);
    ARC_Vector_Destroy(testLanguage);

    ARC_CHECK(arc_errno == 0);
}

/* ~ parser tests ~ */
void TEST_Parser_InitBasicLexerTokenRules(ARC_Lexer *lexer){
    //null
    ARC_Lexer_RegisterTokenRule(lexer, ARC_LexerTokenRule_CreateAndReturnMatchCharRule(ARC_PARSER_TAG_LAMBDA, 0));

    //alpha char
    ARC_Lexer_RegisterTokenRule(lexer, ARC_LexerTokenRule_CreateAndReturnMatchCharOrBetween(TEST_PARSER_ALPHA_LOWER_CHAR, 'a', 'z'));
    ARC_Lexer_RegisterTokenRule(lexer, ARC_LexerTokenRule_CreateAndReturnMatchCharOrBetween(TEST_PARSER_ALPHA_UPPER_CHAR, 'A', 'Z'));
}

uint32_t TEST_Parser_GetStringIdFn(ARC_String *string){
    if(ARC_String_EqualsCStringWithStrlen(string, "<alphaChar>")){
        return TEST_PARSER_ALPHA_CHAR;
    }

    if(ARC_String_EqualsCStringWithStrlen(string, "ALPHA_LOWER_CHAR")){
        return TEST_PARSER_ALPHA_LOWER_CHAR;
    }

    if(ARC_String_EqualsCStringWithStrlen(string, "ALPHA_UPPER_CHAR")){
        return TEST_PARSER_ALPHA_UPPER_CHAR;
    }

    return ~(uint32_t)0;
}

void TEST_ParserData_CreateCharFn(void **data, ARC_ParserTagToken *parsedData, void *userData){
    if(parsedData == NULL){
        *data = NULL;
        return;
    }

    char *alphaChar = (char *)malloc(sizeof(char));

    ARC_ParserTagToken *tagToken = (ARC_ParserTagToken *)ARC_Vector_Get(parsedData->tagTokens, 0);
    *alphaChar = tagToken->token->data->data[0];

    *data = (void *)alphaChar;
}

void TEST_ParserData_DestroyCharFn(void *data, ARC_Bool clear, void *userData){
    if(data == NULL){
        return;
    }

    free((char *)data);
}

ARC_TEST(Parser_Parser_BasicCreateWithStringTest){
    ARC_Parser *parser;

    /* ~ create the language ~ */
    ARC_String *languageString;
    ARC_String_CreateWithStrlen(&languageString, "<alphaChar> -> ALPHA_LOWER_CHAR | ALPHA_UPPER_CHAR\n");

    ARC_ParserData_CreateFn  createCharFn  = TEST_ParserData_CreateCharFn;
    ARC_ParserData_DestroyFn destroyCharFn = TEST_ParserData_DestroyCharFn;
    ARC_Parser_CreateFromString(&parser, languageString, TEST_Parser_InitBasicLexerTokenRules, TEST_Parser_GetStringIdFn, &createCharFn, &destroyCharFn, NULL);

    ARC_String_Destroy(languageString);

    ARC_CHECK(arc_errno == 0);


    /* ~ check if a can be parsed ~ */
    ARC_String *tempString;
    ARC_String_CreateWithStrlen(&tempString, "a");

    ARC_Parser_Parse(parser, &tempString);

    char *data = (char *)ARC_Parser_GetData(parser);

    ARC_CHECK(*data == 'a');

    ARC_Parser_ClearData(parser);


    /* ~ check if Z can be parsed ~ */
    ARC_String_CreateWithStrlen(&tempString, "Z");

    ARC_Parser_Parse(parser, &tempString);

    data = (char *)ARC_Parser_GetData(parser);

    ARC_CHECK(*data == 'Z');

    ARC_Parser_ClearData(parser);


    /* ~ check if 8 errors ~ */
    ARC_String_CreateWithStrlen(&tempString, "8");

    ARC_Parser_Parse(parser, &tempString);

    data = (char *)ARC_Parser_GetData(parser);

    ARC_CHECK(arc_errno == ARC_ERRNO_DATA);
    arc_errno = 0;

    ARC_Parser_ClearData(parser);


    //cleanup
    ARC_Parser_Destroy(parser);
}
