#include "builtins/builtins.h"
#include "parse/parse.h"
#include "execute/execute.h"

using namespace empathy;

namespace empathy::builtins
{
    void SetupApiCompiler( Env& e )
    {
        auto pEnv = e.shared_from_this();

        RegisterBuiltinFunc< uint64_t ( string ) >( e, "ExecuteFile"_sid,
            [pEnv]( const string& filename ) -> uint64_t
            {
                auto pFN = make_shared< string >( filename );

                ifstream sourcefile( pFN->c_str() );
                if( !sourcefile.good() )
                {
                    cout << "can't open '" << *pFN << "'\n";
                    return 0;
                }

                sema::Context c( pEnv, RootIdentity(), GetValueType< uint64_t >() );

                auto r = make_shared< parse::Resolver >( make_shared< lex::Lexer >( sourcefile, pFN ), c );
                parse::Parser p( r );

                auto cfgBuilder = make_shared< sema::CFGBuilder >();
                p.setCFGBuilder( cfgBuilder );
                p.setCurrentBB( cfgBuilder->entryBB() );

                p.parseSequence();

                if( !r->eos() )
                {
                    cout << "syntax error.\n";
                    return 0;
                }

                execute::VM vm;
                auto result = vm.execute( cfgBuilder->entryBB() );
                if( !result )
                    return 1;

                return *FromValue< uint64_t >( *result );
            } );

        RegisterBuiltinFunc< Value ( string, Value, Value ) >( e, "CompileFileToFunction"_sid,
            [pEnv]( const string& filename, const Value& rt, const Value& params ) -> Value
            {
                // Validate those generic value inputs
                if( !rt.isType() )
                {
                    // We don't have builtin optional types so return something completely unexpected instead.
                    // TODO: figure out something more elegant that doens't involve making the optional type builtin.
                    // Perhaps a simpler builtin version, just for the purpose of the builtin api.
                    return ToValue( "error"s );
                }

                if( CheckParamListKind( params ) != ParamListKind::Regular )
                {
                    // We only support compiling files into non-template functions for now.
                    return ToValue( "error"s );
                }

                auto ftype = BuildFuncType( sema::DomainAny(), rt, params );

                // TODO at some point we'll want to pass the base identity to use as a param but
                // let's wait and see how the module and namespace stuff pans out first
                sema::Context c( pEnv, RootIdentity(), ftype.returnType() );

                auto identity = AppendToVectorTerm( RootIdentity(), TERM( StringId( pEnv->GetUniqueId() ) ) );
                auto func = BuildFunc( c, ftype, identity, params, make_shared< Vector >(), c );
                const auto& pFuncLLR = func.llr();

                auto pFN = make_shared< string >( filename );

                ifstream sourcefile( pFN->c_str() );
                if( !sourcefile.good() )
                {
                    pFuncLLR->setInvalid();
                    cout << "can't open '" << *pFN << "'\n";
                    return ToValue( func );
                }

                auto r = make_shared< parse::Resolver >( make_shared< lex::Lexer >( sourcefile, pFN ), c );
                parse::Parser p( r );

                auto cfgBuilder = make_shared< sema::CFGBuilder >();
                p.setCFGBuilder( cfgBuilder );
                p.setCurrentBB( cfgBuilder->entryBB() );

                p.parseSequence();

                if( !r->eos() )
                {
                    pFuncLLR->setInvalid();
                    cout << "syntax error.\n";
                    return ToValue( func );
                }

                if( c.returnType() != GetValueType< void >()
                    && !cfgBuilder->areAllBBTerminated() )
                {
                    pFuncLLR->setInvalid();

                    // TODO error management etc
                    cout << "missing return statement in a function with non-void return type.\n";
                    return ToValue( func );
                }

                pFuncLLR->body() = cfgBuilder->entryBB();




                return ToValue( func );
            } );
    }
}
