#include "builtins/builtins.h"
#include "parse/parse.h"
#include "verify/verify.h"
#include "execute/execute.h"
#include "compile/compiler.h"

using namespace goose;
using namespace goose::compile;

namespace goose::builtins
{
    void SetupApiCompiler( Env& e )
    {
        weak_ptr< Env > pEnv = e.shared_from_this();

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#DiagnosticsEnableTraces"_sid,
            [pEnv]( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "#DiagnosticsEnableTraces: the expression doesn't evaluate to a constant." );
                    return;
                }

                DiagnosticsManager::GetInstance().setTraceMode( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< void ( bool ) >( e, "DiagnosticsForceColors"_sid,
            [pEnv]( bool enable )
            {
                DiagnosticsManager::GetInstance().setForceColors( enable );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationTraceMode"_sid,
            [pEnv]( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "#VerificationTraceMode: the expression doesn't evaluate to a constant." );
                    return;
                }

                verify::Func::SetTraceMode( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationDumpSolverOnFailure"_sid,
            [pEnv]( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "#VerificationDumpSolverOnFailure: the expression doesn't evaluate to a constant." );
                    return;
                }

                verify::Func::SetDumpSolverOnFailure( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( bool ) > >( e, "#VerificationDumpSolverOnSuccess"_sid,
            [pEnv]( auto&& c, const Value& enable )
            {
                if( !enable.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( enable.locationId(),
                        "#VerificationDumpSolverOnSuccess: the expression doesn't evaluate to a constant." );
                    return;
                }

                verify::Func::SetDumpSolverOnSuccess( *FromValue< bool >( enable ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( uint32_t ) > >( e, "#SetExecutionBudget"_sid,
            [pEnv]( auto&& c, const Value& budget )
            {
                static bool used = false;

                if( !budget.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( budget.locationId(),
                        "#SetExecutionBudget: the expression doesn't evaluate to a constant." );
                    return;
                }

                if( used )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( 0,
                        "#SetExecutionBudget can only be used once." );
                    return;
                }

                used = true;
                execute::VM::SetExecutionBudget( *FromValue< uint32_t >( budget ) );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( Value ) > >( e, "#DumpValue"_sid,
            [pEnv]( auto&& c, const Value& val )
            {
                stringstream s;
                s << val;
                DiagnosticsManager::GetInstance().emitTraceMessage( val.locationId(), s.str() );
            } );

        RegisterBuiltinFunc< Eager< Value > ( Value, string ) >( e, "ExternalFunction"_sid,
            [pEnv]( const Value& f, const string& symbol )
            {
                auto ft = FromValue< FuncType >( f );
                if( !ft )
                    return PoisonValue();

                return ToValue( BuildExternalFunc( *ft, symbol ) );
            } );

        RegisterBuiltinFunc< Eager< Value > ( Value, string ) >( e, "ExternalVarArgFunction"_sid,
            [pEnv]( const Value& f, const string& symbol )
            {
                auto ft = FromValue< FuncType >( f );
                if( !ft )
                    return PoisonValue();

                return ToValue( BuildExternalFunc( *ft, symbol, true ) );
            } );

        RegisterBuiltinFunc< void ( string, Value ) >( e, "CreateConstant"_sid,
            [pEnv]( const string& name, const Value& v )
            {
                if( !v.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( v.locationId(),
                        "CreateConstant: the expression doesn't evaluate to a constant." );
                    return;
                }

                pEnv.lock()->storeValue(
                    AppendToVectorTerm( RootIdentity(), StringId( name.c_str() ) ),
                    ANYTERM( _ ), ValueToEIR( v ) );
            } );

        RegisterBuiltinFunc< Intrinsic< uint32_t ( string ) > >( e, "#Include"_sid,
            [pEnv]( auto&& c, const Value& fnameval ) -> Value
            {
                auto filename = *FromValue< string >( fnameval );

                auto identity = TakeVectorTerm( c.identity(), VecSize( c.identity() ) - 1 );
                get< pvec >( identity )->terms().back() = TSID( locvars );

                auto result = Compiler::LoadAndExecuteFile( pEnv.lock(), filename, identity,
                    GetValueType< uint32_t >(), ToValue< uint32_t >( 0 ) );

                if( !result )
                    return ToValue< uint32_t >( 0 );

                return *result;
            } );

        RegisterBuiltinFunc< uint32_t ( string ) >( e, "ExecuteFile"_sid,
            [pEnv]( const string& filename ) -> Value
            {
                // For now generate an unique identity for the file to execute. Later we might want to
                // pass an identity as parameters as a wrapped term.
                auto identity = AppendToVectorTerm( RootIdentity(),
                    TERM( StringId( Env::NewUniqueId() ) ) );
                auto locVarsIdentity = AppendToVectorTerm( identity, TSID( locvars ) );

                auto env = pEnv.lock();
                env->addVisibilityRule( builtins::RootIdentity(), identity );
                env->addVisibilityRule( identity, locVarsIdentity );

                auto result = Compiler::LoadAndExecuteFile( env, filename, locVarsIdentity,
                    GetValueType< uint32_t >(), ToValue< uint32_t >( 0 ) );

                if( !result )
                    return ToValue< uint32_t >( 0 );

                return *result;
            } );

        RegisterBuiltinFunc< Intrinsic< Value ( string, Value, Value, Value ) > >( e, "#CompileFileToFunction"_sid,
            [pEnv]( auto&& c, const Value& filenameVal, const Value& rt, const Value& defReturnValue, const Value& params ) -> Value
            {
                if( !filenameVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( filenameVal.locationId(),
                        "#CompileFileToFunction: the expression doesn't evaluate to a constant." );
                    return PoisonValue();
                }

                auto filename = *FromValue< string >( filenameVal );

                // Validate those generic value inputs
                if( !IsType( c, rt ) )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( rt.locationId(),
                        "#CompileFileToFunction: type expected.", 0 );
                    return PoisonValue();
                }

                if( CheckParamListKind( params ) != ParamListKind::Regular )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( params.locationId(),
                        "#CompileFileToFunction: template parameter lists are not supported.", 0 );
                    return PoisonValue();
                }

                // 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 localC( pEnv.lock(), builtins::RootIdentity(), ValueToEIR( rt ) );
                auto ftype = BuildFuncType( localC, rt, params );

                auto funcIdentity = AppendToVectorTerm( builtins::RootIdentity(),
                    TERM( StringId( Env::NewUniqueId() ) ) );
                c.env()->addVisibilityRule( builtins::RootIdentity(), funcIdentity );

                auto locVarsIdentity = AppendToVectorTerm( funcIdentity, TSID( locvars ) );
                c.env()->addVisibilityRule( funcIdentity, locVarsIdentity );

                auto func = BuildFunc( localC, ftype, funcIdentity, params, nullptr, c );
                const auto& pFuncCIR = func.cir();

                auto cfg = Compiler::LoadAndParseFile( pEnv.lock(), filename, locVarsIdentity,
                    ftype.returnType(), defReturnValue );

                if( !cfg )
                    return PoisonValue();

                if( cfg->isPoisoned() )
                    return PoisonValue();

                pFuncCIR->body() = cfg;
                return ToValue( func );
            } );
    }
}
