#include "parse.h"

using namespace goose;
using namespace goose::parse;

void Resolver::init()
{
    m_context.onContextRestoredSignal().subscribe( [&]
        {
            clearLookAheadCache();
        } );
}

bool Resolver::eos() const
{
    clearCacheIfNeeded();
    return m_tokProvider->eos() && m_lookAheadCache.empty();
}

optional< TermLoc > Resolver::consume()
{
    clearCacheIfNeeded();

    auto tok = m_tokProvider->consume();

    if( m_lookAheadCache.empty() )
    {
        if( !tok )
            return nullopt;
        return resolve( *tok );
    }

    auto t = move( m_lookAheadCache.front() );
    m_lookAheadCache.pop_front();
    return t;
}

optional< TermLoc > Resolver::consumeUnresolved()
{
    clearCacheIfNeeded();

    if( !m_lookAheadCache.empty() )
        m_lookAheadCache.pop_front();

    return m_tokProvider->consume();
}

optional< TermLoc > Resolver::lookAhead( size_t distance )
{
    clearCacheIfNeeded();
    size_t i = 0;

    while( m_lookAheadCache.size() < ( distance + 1 ) )
    {
        auto tok = m_tokProvider->lookAhead( i++ );
        if( !tok )
            return nullopt;

        m_lookAheadCache.emplace_back( resolve( *tok ) );
    }

    return m_lookAheadCache[distance];
}

optional< TermLoc > Resolver::lookAheadUnresolved( size_t distance )
{
    return m_tokProvider->lookAhead( distance );
}

void Resolver::clearLookAheadCache() const
{
    m_lookAheadCache.clear();
    m_valueStoreVersion = m_context.env()->valueStoreVersion();
}

TermLoc Resolver::resolve( const TermLoc& t ) const
{
    auto identifier = Decompose( t.first, Val< StringId >() );
    if( !identifier )
        return t;

    Term result;

    DiagnosticsContext dc( t.second, "Referenced here." );

    auto valId = AppendToVectorTerm( m_context.identity(), t.first );
    switch( m_context.env()->retrieveValue( valId, m_context.identity(), result ) )
    {
        case sema::Env::Status::NoMatch:
            return t;

        case sema::Env::Status::AmbiguousMatch:
            DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                t.second, "ambiguous match for symbol.", 0 );
            return t;

        default:
            break;
    }

    return TermLoc( result, t.second );
}

Generator< TermLoc > Resolver::consumeUnit()
{
    auto tok = consumeUnresolved();
    if( !tok )
        co_return;

    const auto* delim = get_if< Delimiter >( &tok->first );
    if( !delim )
    {
        co_yield move( *tok );
        co_return;
    }

    switch( *delim )
    {
        case Delimiter::OpenParen:
            co_yield move( *tok );
            co_yield consumeBlock( Delimiter::CloseParen );
            break;

        case Delimiter::OpenBrace:
            co_yield move( *tok );
            co_yield consumeBlock( Delimiter::CloseBrace );
            break;

        case Delimiter::OpenBracket:
            co_yield move( *tok );
            co_yield consumeBlock( Delimiter::CloseBracket );
            break;

        case Delimiter::CloseParen:
            DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                tok->second, "mismatched ')'.", 0 );
            break;

        case Delimiter::CloseBrace:
            DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                tok->second, "mismatched '}'.", 0 );
            break;

        case Delimiter::CloseBracket:
            DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                tok->second, "mismatched ']'.", 0 );
            break;

        default:
            co_yield move( *tok );
            break;
    }
}

Generator< TermLoc > Resolver::consumeBlock( Delimiter end )
{
    while( const auto& tok = lookAheadUnresolved() )
    {
        const auto* delim = get_if< Delimiter >( &tok->first );
        if( delim )
        {
            if( *delim == end )
            {
                co_yield *consumeUnresolved();
                co_return;
            }
            else
            {
                switch( *delim )
                {
                    case Delimiter::CloseParen:
                        DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                            tok->second, "mismatched ')'.", 0 );
                        co_return;

                    case Delimiter::CloseBrace:
                        DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                            tok->second, "mismatched '}'.", 0 );
                        co_return;

                    case Delimiter::CloseBracket:
                        DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
                            tok->second, "mismatched ']'.", 0 );
                        co_return;

                    default:
                        break;
                }
            }
        }

        co_yield consumeUnit();
    }

    switch( end )
    {
        case Delimiter::CloseParen:
            DiagnosticsManager::GetInstance().emitLexerErrorMessage( currentLocation(), "missing ')'." );
            break;

        case Delimiter::CloseBrace:
            DiagnosticsManager::GetInstance().emitLexerErrorMessage( currentLocation(), "missing '}'." );
            break;

        case Delimiter::CloseBracket:
            DiagnosticsManager::GetInstance().emitLexerErrorMessage( currentLocation(), "missing ']'." );
            break;

        default:
            break;
    }
}

lex::TokenProvider::pos_type Resolver::position() const
{
    return m_tokProvider->position();
}

void Resolver::setPosition( lex::TokenProvider::pos_type position )
{
    clearLookAheadCache();
    m_tokProvider->setPosition( position );
}
