#include "sp_portal/entities/xen_plantlight_switch"

CCVar cvarLobbyDigitRotatery( "lobby_digit_rotation", 0.0f, "Lobby number rotation", ConCommandFlag::AdminOnly );
CCVar cvarMusic( "lobby_music", 0.0f, "Lobby music", ConCommandFlag::AdminOnly );

/* string strWelcomeInfo1 = "Welcome to Half-Life Nexus Lobby!";
string strWelcomeInfo2 = "Stand on a platform to vote for your favorite map.";
string strWelcomeInfo3 = "Use the green teleports to move to other vote areas."; */

const array<string> SPAWNFX_SNDS = { "beamstart1.wav","beamstart2.wav","beamstart7.wav","beamstart8.wav" };
//array<bool> BL_WELCOMEINFO_NOTRECEIVED( 33, true );

void MapInit()
{
    plantlight_switch::Register();
    g_EngineFuncs.CVarSetFloat( "mp_forcerespawn", 1 );

    g_Game.PrecacheModel( "sprites/b-tele1.spr" );
    for( uint i = 0; i < SPAWNFX_SNDS.length(); i++ )
        g_SoundSystem.PrecacheSound( "debris/" + SPAWNFX_SNDS[i] );

    g_Hooks.RegisterHook( Hooks::Player::PlayerSpawn, @SpawneryFx );
}

void MapStart()
{
    if( cvarLobbyDigitRotatery.GetInt() > 0 )
        g_Scheduler.SetInterval( "DigitRotateryFuncs", 0.025f, -1 );
    
    RemoveryFuncs();
}

void DigitRotateryFuncs()
{
    CBaseEntity@ pDigits;
    while( ( @pDigits = g_EntityFuncs.FindEntityByTargetname( pDigits, "digits" ) ) !is null )
        pDigits.pev.angles.z = ( pDigits.pev.angles.z + 1.0f ) % 360.0f;
}

void RemoveryFuncs()
{
    if( cvarMusic.GetInt() < 1 )
    {
        CBaseEntity@ pMusic;
        while( ( @pMusic = g_EntityFuncs.FindEntityByTargetname( pMusic, "lobbymusic" ) ) !is null )
            g_EntityFuncs.Remove( pMusic );
    }
}

HookReturnCode SpawneryFx(CBasePlayer@ pPlayer)
{
    if( pPlayer is null )
        return HOOK_CONTINUE;

    CSprite@ pSpr = g_EntityFuncs.CreateSprite( "sprites/b-tele1.spr", pPlayer.GetOrigin(), true, 0.0f );
    pSpr.SetScale( 1 );
    pSpr.SetTransparency( kRenderTransAdd, 0, 0, 0, 255, kRenderFxNoDissipation );
    pSpr.AnimateAndDie( 18 );

    g_SoundSystem.EmitSound( pPlayer.edict(), CHAN_ITEM, "debris/" + SPAWNFX_SNDS[Math.RandomLong( 0, SPAWNFX_SNDS.length()-1 )], 1.0f, ATTN_NORM );

    //g_Scheduler.SetTimeout( "ShowWelcomeInfo", 3.0f, EHandle( pPlayer ) );

    return HOOK_CONTINUE;
}

/* void ShowWelcomeInfo(EHandle hPlayer)
{
    if( !hPlayer )
        return;

    CBasePlayer@ pPlayer = cast<CBasePlayer@>( hPlayer.GetEntity() );

    if( BL_WELCOMEINFO_NOTRECEIVED[pPlayer.entindex()] )
    {
        g_PlayerFuncs.SayText( pPlayer, strWelcomeInfo1 );
        g_PlayerFuncs.SayText( pPlayer, "\n" );
        g_PlayerFuncs.SayText( pPlayer, strWelcomeInfo2 );
        g_PlayerFuncs.SayText( pPlayer, strWelcomeInfo3 );

        BL_WELCOMEINFO_NOTRECEIVED[pPlayer.entindex()] = false;
    }
} */