#include "g0api/g0api.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::g0api
{
    void SetupApiCompiler( Env& e )
    {
        DefineConstant( e, "TargetOSName"_sid, ValueToEIR( ToValue( string( TARGET_OS_NAME ) ) ) );

        wptr< Env > pEnv = e.shared_from_this();

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

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

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

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

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

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

        RegisterBuiltinFunc< Intrinsic< void ( CustomPattern< Value, FuncPattern >, string ) > >( e, "#DumpFunctionCFG"_sid,
            []( auto&& c, const Value& funcVal, const Value& filename )
            {
                if( !funcVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( funcVal.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                if( !filename.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( filename.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                auto name = *FromValue< string >( filename );
                auto func = CompileFunc( c, funcVal );
                if( func.isPoison() || DiagnosticsManager::GetInstance().errorsWereEmitted() )
                    return;

                auto f = *FromValue< builtins::Func >( func );
                if( !f.cir() )
                    return;

                CfgViz( name.c_str(), *f.cir() );
            } );

        RegisterBuiltinFunc< Intrinsic< void ( CustomPattern< Value, FuncPattern >, string ) > >( e, "#DumpFunctionCFGText"_sid,
            []( auto&& c, const Value& funcVal, const Value& filename )
            {
                if( !funcVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( funcVal.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                if( !filename.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( filename.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

                auto name = *FromValue< string >( filename );
                auto func = CompileFunc( c, funcVal );
                if( func.isPoison() || DiagnosticsManager::GetInstance().errorsWereEmitted() )
                    return;

                auto f = *FromValue< builtins::Func >( func );
                if( !f.cir() )
                    return;

                ofstream out( name.c_str() );
                out << *f.cir();
            } );

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

                if( !budget.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( budget.locationId(),
                        "this 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< Eager< Value > ( Value, string ) >( e, "ExternalFunction"_sid,
            []( 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,
            []( const Value& f, const string& symbol )
            {
                auto ft = FromValue< FuncType >( f );
                if( !ft )
                    return PoisonValue();

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

        // TODO: Clean: this is no longer needed now that we have an api to access the Env
        RegisterBuiltinFunc< void ( string, Value ) >( e, "CreateConstant"_sid,
            [pEnv]( const string& name, const Value& v )
            {
                if( !v.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( v.locationId(),
                        "this doesn't evaluate to a constant." );
                    return;
                }

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

        RegisterBuiltinFunc< Intrinsic< uint32_t ( string ) > >( e, "#Include"_sid,
            [pEnv]( auto&& c, const Value& fnameval ) -> Value
            {
                execute::VM vm;
                auto fname = Evaluate( fnameval, vm );
                auto filename = FromValue< string >( fname );
                if( !filename )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( fnameval.locationId(),
                        "can't evaluate this to a string." );
                    return PoisonValue();
                }

                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 ) -> uint32_t
            {
                auto identity = AppendToVectorTerm( RootG0Identity(),
                    TERM( StringId( Env::NewUniqueId() ) ) );
                auto locVarsIdentity = AppendToVectorTerm( identity, TSID( locvars ) );

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

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

                if( !result || result->isPoison() )
                    return 0;

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

        RegisterBuiltinFunc< Intrinsic< Value ( string, TypeWrapper< Term >, Value, Value, Value ) > >( e, "#CompileFileToFunction"_sid,
            [pEnv]( auto&& c, const Value& filenameVal, const Value& identityVal, const Value& rt, const Value& defReturnValue, const Value& params ) -> Value
            {
                ProfileZoneScopedN( "#CompileFileToFunction" );

                if( !filenameVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( filenameVal.locationId(),
                        "this doesn't evaluate to a constant." );
                    return PoisonValue();
                }

                auto filename = *FromValue< string >( filenameVal );
                ProfileZoneName( filename.c_str(), filename.size() );

                if( !identityVal.isConstant() )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( identityVal.locationId(),
                        "this doesn't evaluate to a constant." );
                    return PoisonValue();
                }

                auto identity = *FromValue< TypeWrapper< Term > >( identityVal );

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

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

                sema::Context localC( pEnv.lock(), identity, ValueToEIR( rt ) );
                auto ftype = BuildFuncType( localC, rt, params );
                if( !ftype )
                    return PoisonValue();

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

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

                auto func = BuildFunc( localC, *ftype, identity, 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 );
            } );
    }
}
