#ifndef __IFPARSER_h__
#define __IFPARSER_h__

#include <iostream>
#include <string>
#include <vector>

#include <stdexcept>



enum class NodeType { 
    Variable, Number, Not, And, Or,
    Equal, NotEqual, Less, Greater, 
    True, False, StringLiteral 
};

class IFNode {
public:
    NodeType type;
    std::string name;
    IFNode *left;
    IFNode *right;
};

class IFParser {
public:
    enum class TokenType {
        Symbol, Number, True, False,
        And, Or, Not,
        Equal, NotEqual, Less, Greater,
        LParen, RParen, EndOfFile,
        StringLiteral, nullpt
    };
    
    struct Token {
        TokenType type;
        std::string value;
    };
private:
    std::vector<Token> tokens_;
    std::vector<int> varpositions;
    std::vector<std::string> varnames;
    size_t pos_;


public:

    const std::vector<int> getVarPositions(){
        return varpositions;
    }
    
    const std::vector<std::string> getVarNames(){
        return varnames;
    }
    
    std::vector<Token> getTokens(){
        return tokens_;
    }
    
    void setToken(int index, std::string str){
        tokens_[index].value = str;
    }
    
    std::string getLogicalExpression(){
        std::string ret;
        for(Token t: tokens_){
            ret += t.value;
        }
        return ret;
    }
    

    void tokenize(const std::string& input) {
        size_t i = 0;
        while (i < input.size()) {
            if (std::isspace(static_cast<unsigned char>(input[i]))) {
                i++;
                continue;
            }
    
            if (input[i] == '=' && i + 1 < input.size() && input[i + 1] == '=') {
                tokens_.push_back({TokenType::Equal, "=="});
                pos_++;
                i += 2;
            } else if (input[i] == '!' && i + 1 < input.size() && input[i + 1] == '=') {
                tokens_.push_back({TokenType::NotEqual, "!="});
                pos_++;
                i += 2;
            } else if (input[i] == '&' && i + 1 < input.size() && input[i + 1] == '&') {
                tokens_.push_back({TokenType::And, "&&"});
                pos_++;
                i += 2;
            } else if (input[i] == '|' && i + 1 < input.size() && input[i + 1] == '|') {
                tokens_.push_back({TokenType::Or, "||"});
                pos_++;
                i += 2;
            }else if (input[i] == '<') {
                tokens_.push_back({TokenType::Less, "<"});
                pos_++;
                i++;
            } else if (input[i] == '>') {
                tokens_.push_back({TokenType::Greater, ">"});
                pos_++;
                i++;
            } else if (input[i] == '!') {
                tokens_.push_back({TokenType::Not, "!"});
                pos_++;
                i++;
            } else if (input[i] == '(') {
                tokens_.push_back({TokenType::LParen, "("});
                pos_++;
                i++;
            } else if (input[i] == ')') {
                tokens_.push_back({TokenType::RParen, ")"});
                pos_++;
                i++;
            } else if (input[i] == '"') {
                i++; 
                
                std::string strVal;
                strVal += "\"";
                while (i < input.size() && input[i] != '"') {
                    if (input[i] == '\\' && i + 1 < input.size()) {
                        i++;
                    }
                    strVal += input[i++];
                }
                
                if (i >= input.size()) {
                    throw std::runtime_error("Ungeschlossener String (Fehlendes \")");
                }
                strVal += "\"";
                i++; 
                tokens_.push_back({TokenType::StringLiteral, strVal});
                pos_++;
            } else if (input[i] == '\'') {
                i++; 
                std::string strVal;
                strVal += "\'";
                while (i < input.size() && input[i] != '"') {
                    if (input[i] == '\\' && i + 1 < input.size()) {
                        i++;
                    }
                    strVal += input[i++];
                }
                
                if (i >= input.size()) {
                    throw std::runtime_error("Ungeschlossener String (Fehlendes \")");
                }
                strVal += "\'";
                i++; 
                tokens_.push_back({TokenType::StringLiteral, strVal});
                pos_++;
            } else if (std::isdigit(static_cast<unsigned char>(input[i]))) {
                std::string numStr;
                while (i < input.size() && (std::isdigit(static_cast<unsigned char>(input[i])) || input[i] == '.')) {
                    numStr += input[i++];
                }
                tokens_.push_back({TokenType::Number, numStr});
                pos_++;
            } else if (std::isalpha(static_cast<unsigned char>(input[i])) || input[i] == '_') {
                std::string sym;
                while (i < input.size() && (std::isalnum(static_cast<unsigned char>(input[i])) || input[i] == '_')) {
                    sym += input[i++];
                }
    
                if (sym == "true") {
                    tokens_.push_back({TokenType::True, sym});
                    pos_++;
                } else if (sym == "false") {
                    tokens_.push_back({TokenType::False, sym});
                    pos_++;
                } else if (sym == "nullptr") {
                    tokens_.push_back({TokenType::nullpt, sym});
                    pos_++;
                } else {
                    tokens_.push_back({TokenType::Symbol, sym});
                    varnames.push_back(sym);
                    varpositions.push_back(pos_);
                    pos_++;
                }
            } 
            else {
                throw std::runtime_error(std::string("Unerwartetes Zeichen: ") + input[i]);
            }
        }
        tokens_.push_back({TokenType::EndOfFile, ""});
    }


    IFParser() : pos_(0) {}
};


#endif
