#ifndef AST_H
#define AST_H

#include <vector>
#include <stdlib.h>
#include <string>
#include "llvmlayer.h"
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <climits>
#include "attachments.h"

namespace xreate {

struct String_t{};
struct Identifier_t {};
struct Number_t {};
struct Type_t {};

template<typename A>
class Atom {};

template<> class Atom<Identifier_t>
{
public:
    Atom(const std::wstring& value)
    {
        char buffer[32];
        wcstombs(buffer, value.c_str(), 32);

        __value = buffer;
    }
    Atom(std::string && name): __value(name) {}

    const std::string&  get() const{return __value; }
private:
    std::string __value;
};

template<> class Atom<Number_t>
{
public:
    Atom(wchar_t* value)
    {
        __value = wcstol(value, 0, 10);
    }

    Atom(int value)
        : __value(value)
    {}
    double get()const {return __value; }
private:
   double __value;
};

template<> class Atom<String_t>
{
public:
    Atom(const std::wstring& value)
        : __value(++value.begin(), --value.end())
    {}

    const std::string& get() const {return __value; }

private:
    std::string __value;
};

enum class TypePrimitive {Bool, Int, Float, Num, String, i32};

template<> class Atom<Type_t>
{
public:
    Atom(wchar_t* value)
    {
        char buffer_[32];
        wcstombs(buffer_, value, 32);
        std::string buffer(buffer_);

        if (buffer=="bool"){
            __value = TypePrimitive ::Bool;
        } else if (buffer=="int") {
            __value = TypePrimitive::Int;
        } else if (buffer=="float") {
            __value = TypePrimitive::Float;
        } else if (buffer=="num") {
            __value = TypePrimitive::Num;
        } else if (buffer=="string") {
            __value = TypePrimitive::String;
        }
    }

    Atom()
    {
    }

    TypePrimitive get() const
    {
        return __value;
    }

private:
    TypePrimitive __value;
};

typedef Atom<Type_t> TypeAtom;


enum class TypeOperator{NONE, LIST, STRUCT};
struct llvm_array_tag {}; struct struct_tag{};
    const llvm_array_tag tag_array = llvm_array_tag();
    const struct_tag tag_struct = struct_tag();
class TypeAnnotation
{
public:
    TypeAnnotation();
    TypeAnnotation (const Atom<Type_t>& typ);
    TypeAnnotation (TypePrimitive typ);
    TypeAnnotation (llvm_array_tag, TypePrimitive typ, int size);

    TypeAnnotation (TypeOperator op, std::initializer_list<TypeAnnotation> operands);
 //   TypeAnnotation (struct_tag, std::initializer_list<TypePrimitive>);

    llvm::Type* toLLVMType();

    TypePrimitive __value;
    TypeOperator __operator = TypeOperator::NONE;
    std::vector<TypeAnnotation> __operands;
    int __size = 0;
private:

};

enum class Operator
{
ADD, SUB, MUL, DIV, EQU, LSS, GTR, NEG, LIST, LIST_RANGE, CALL, NONE, IMPL/* implication */, MAP, FOLD, INDEX, IF
};

class Function;
class AST;
class CodeScope;
class MetaRuleAbstract;

template<class Target>
struct ManagedPtr
{
    ManagedPtr(){};

    ManagedPtr(unsigned int id, const std::vector<Target*>* storage)
            : __id(id), __storage(storage)
    {}

    Target&
    operator*() const
    {
        return *(*__storage).at(__id);
    }

    void operator=(const ManagedPtr<Target>& other)
    {
        __id = other.__id;
        __storage = other.__storage;
    }

    bool
    operator == (const ManagedPtr<Target>& other)
    {
        return isValid() && (__id == other.__id);
    }

    Target*
    operator->() const noexcept
    {
        assert(isValid());
        return __storage->at(__id);
    }

    inline bool isValid() const
    {
        return  (__storage) && (0 <= __id) && (__id < __storage->size());
    }

    ManagedPtr<Target>& operator++()
    {
        ++__id;
        return *this;
    }

    inline unsigned int id()
    {
        return __id;
    }

private:
    unsigned int __id =0;
    const std::vector<Target*> * __storage=0;
};

typedef ManagedPtr<Function> ManagedFnPtr;
typedef ManagedPtr<CodeScope> ManagedScpPtr;
typedef ManagedPtr<MetaRuleAbstract> ManagedRulePtr;
const ManagedScpPtr NO_SCOPE = ManagedScpPtr(UINT_MAX, 0);

struct Expression
{
    friend class CodeScope;
    friend class ClaspLayer;
    friend class CFGPass;

    Expression(const Operator &oprt, std::initializer_list<Expression> params);
    Expression(const Atom<Identifier_t>& ident);
    Expression(const Atom<Number_t>& number);
    Expression();

    void setOp(Operator oprt);
    void addArg(Expression&& arg);
    void addBindings(std::initializer_list<Atom<Identifier_t>> params);
    void addBlock(ManagedScpPtr scope);

    const std::vector<Expression>& getOperands() const;
    double getValueDouble() const;
    const std::string& getValueString() const;

    Operator op;
    enum {INVALID, COMPOUND, IDENT, NUMBER, STRING} __state;
    std::vector<std::string> bindings;
    std::list<ManagedScpPtr> blocks;
    std::vector<Expression> operands;
private:
    std::string __valueS;
    double __valueD;
};

typedef std::list<Expression> ExpressionList;
enum class TagModifier
{NONE, ASSERT, REQUIRE};

enum class DomainAnnotation
{FUNCTION, VARIABLE};

class RuleArguments: public std::vector<std::pair<std::string, DomainAnnotation>>
{
public:
    void add(const Atom<Identifier_t>& name, DomainAnnotation typ);
};

class RuleGuards: public std::vector<Expression>
{
public:
    void add(Expression&& e);
};


class ClaspLayer;
class LLVMLayer;


class MetaRuleAbstract
{
public:
    MetaRuleAbstract(RuleArguments&& args, RuleGuards&& guards);
    virtual ~MetaRuleAbstract();
    virtual void compile(ClaspLayer& layer) =0;
protected:
    RuleArguments __args;
    RuleGuards __guards;
};

class RuleWarning: public MetaRuleAbstract
{
    friend class ClaspLayer;
public:
    RuleWarning(RuleArguments&& args, RuleGuards&& guards, Expression&& condition, Atom<String_t>&& message);
    virtual void compile(ClaspLayer& layer);
    ~RuleWarning();

private:
    std::string __message;
    Expression __condition;
};

typedef unsigned int VID;



/*
class Expression: ExpressionAbstract
{
    friend class CFGPass;

public:
    llvm::Value* compile(LLVMLayer& l, Function* f,  std::string* hintRetVar=0) const;
};
*/



typedef std::pair<VID, TypeAnnotation> VariableDefinition;
typedef std::pair<VID, Expression> VariableDeclaration;

typedef std::pair<Expression, TagModifier> Tag;


struct Symbol
{
    VID identifier;
    CodeScope * scope;
};

bool operator< (const Symbol& s1, const Symbol& s2);
bool operator== (const Symbol& s1, const Symbol& s2);

class CodeScope
{
    friend class Function;
    friend class PassManager;

public:
    CodeScope(CodeScope* parent=0);
    void setBody(const Expression& body);
    void addDeclaration(const Atom <Identifier_t> &&name, TypeAnnotation &&typ, Expression&& body);

    void addArg(Atom <Identifier_t>&& name, TypeAnnotation&& typ);
    void bindArg(llvm::Value* var, std::string&& name);

        //TODO exclude forceCompile partz
    Symbol findSymbol(const std::string &name, LLVMLayer &l, bool forceCompile=false);
    static const Expression& findDeclaration(const Symbol& symbol);
    static TypeAnnotation& findDefinition(const Symbol& symbol);
    static bool hasDeclaration(const Symbol& symbol);
    static llvm::Value* compileExpression(const Symbol& s, LLVMLayer& l, std::string hintRetVar="");
    llvm::Value* compile(LLVMLayer &l, const std::string & hintBlockName="");


    ~CodeScope();

    std::vector<std::string> __args;
    Expression __body;  //TODO move __body to __declarations[0]
    std::map<VID,llvm::Value*> __rawVars;
    virtual llvm::Value* compileExpression(const Expression& expr, LLVMLayer& l, std::string hintRetVar="");
    SymbolAttachments attachments;
protected:
    /**
    * definition of return type have variable index Zero(0)
    */
    //TODO move __definitions to SymbolsAttachments data
    std::unordered_map<VariableDefinition::first_type, VariableDefinition::second_type> __definitions;
    std::unordered_map<VariableDeclaration::first_type, VariableDeclaration::second_type> __declarations;
    std::map<std::string, VID> __vartable;

    VID __vCounter=1;
    CodeScope* __parent;
    std::list<CodeScope*> __storage;
    VID registerVar(std::string&& name, TypeAnnotation &&typ);

};

class Function
{
    friend class Expression;
    friend class CodeScope;
    friend class AST;

public:
    Function(const Atom<Identifier_t>& name);

    void addArg(Atom <Identifier_t>&& name, TypeAnnotation&& typ);
    void addTag(Expression&& tag, const TagModifier mod);

    void setReturnType(const TypeAnnotation& rtyp);

    const std::string& getName() const;

    llvm::Function* compile(LLVMLayer& l);
    const std::vector<Tag>& getAnnotations() const;
    CodeScope* getEntryScope() const;
private:

    CodeScope* __entry;
    std::string __name;
    std::vector<Tag>  __tags;

    llvm::Function* __raw;

};



class AST
{
public:
    AST();
    void add(Function* f);
    void add(MetaRuleAbstract* r);
    void addDFAData(Expression&& data);
    ManagedScpPtr add(CodeScope* scope);

    void compile(LLVMLayer& l);
    void run(LLVMLayer& l);

    std::string getModuleName();

    ManagedPtr<Function> findFunction(const std::string& name);

    template<class Target>
    ManagedPtr<Target> begin();

    std::list<Expression> __dfadata; //TODO move to more appropriate place
    std::list<std::string> __rawImports; //TODO move to more appropriate place
private:
    std::vector<MetaRuleAbstract*> __rules;
    std::vector<Function*> __functions;
    std::vector<CodeScope*> __scopes;


    std::map<std::string, unsigned int> __indexFunctions;
};

    template<>
    ManagedPtr<Function>
    AST::begin<Function>();

    template<>
    ManagedPtr<CodeScope>
    AST::begin<CodeScope>();

    template<>
    ManagedPtr<MetaRuleAbstract>
    AST::begin<MetaRuleAbstract>();
}
#endif // AST_H
