#using scripts\codescripts\struct;

#using scripts\shared\array_shared;
#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\flagsys_shared;
#using scripts\shared\scene_shared;
#using scripts\shared\spawner_shared;
#using scripts\shared\system_shared;
#using scripts\shared\util_shared;
#using scripts\shared\visionset_mgr_shared;

#using scripts\cp\gametypes\_save;

    	   	                                                                                                                                         	                                	                                                                                                                                                                                                                                                                                                                                                                    	        	     	              	    	   	               	                                              	                                                           	                               	                     	                                                                                                           	                                                                 	                                                              	                                                                                                                                             	                            	                                     	                                       	                                                                                                              	   	                  	       	                                                    	                   	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            	                                                                                                 	                                    	                                                                                        	           	                        	                                            	                                             	                                                   	                                                             	                                                         	                                                                    	                                                                                                                                                                                                                                	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  	    	         	                                                              	                                                          	                                   	                                   	                                                    	                                       	                                           	     	                                                                                                                                                                                                                                                      	                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	                                                               	        	                                                                                                                              	                                                                                                                     	                               	                                                      	                          
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         	   	                                                                                                                                                                                                                                  	                                                                                                                                                                                                                                                                                                                                                                                                                                                   
                                                                          	                                        	   	     	    	     	   		   	   	                                                                       	                                                                          	           	        	                                                                          	     	          	           		       	                                                                                                                                                                                                                                                                                                                                                    	                                                                                                                                                   	   	                  	   	                                    	          	   	   	    	    		                                                                     	                          	              	                        	                        	                    	                                                                                                         	                                                                                           	                                                                     	  	                                                                                              	    	                                                                                    	                                                                                      	                                                                                	  	                                                                     	   	  	  	
    	                               	                    	                                 	                                        	                            	                                                                  	                                               

#namespace oed;

function autoexec __init__sytem__() {     system::register("oed",&__init__,&__main__,undefined);    }
	
//TODO - add in support for the animated portion of the hud/lui when it comes online

function __init__()
{
	clientfield::register( "toplayer",		"ev_toggle",		1, 1, "int" );
	clientfield::register( "toplayer",		"sitrep_toggle",	1, 1, "int" );
	clientfield::register( "toplayer",		"tmode_toggle",		1, 3, "int" );
	clientfield::register( "toplayer", 		"active_dni_fx",	1, 1, "counter" );
	clientfield::register( "toplayer", 		"hack_dni_fx",		1, 1, "counter" );
		
	clientfield::register( "actor",			"thermal_active",	1, 1, "int" );
	clientfield::register( "actor",			"sitrep_material",	1, 1, "int" );
	clientfield::register( "actor",			"force_tmode",		1, 1, "int" );
	clientfield::register( "actor",			"tagged",			1, 1, "int" );

	clientfield::register( "vehicle",		"thermal_active",	1, 1, "int" );
	clientfield::register( "vehicle",		"sitrep_material",	1, 1, "int" );

	clientfield::register( "scriptmover",	"thermal_active",	1, 1, "int" );
	clientfield::register( "scriptmover",	"sitrep_material",	1, 1, "int" );
	
	clientfield::register( "item",			"sitrep_material",	1, 1, "int" );

	if ( !IsDefined( level.vsmgr_prio_visionset_tmode ) )
	{
		level.vsmgr_prio_visionset_tmode = 50;
	}
	visionset_mgr::register_info( "visionset", "tac_mode", 1, level.vsmgr_prio_visionset_tmode, 15, true, &visionset_mgr::ramp_in_out_thread_per_player, false );

	callback::on_spawned( &on_player_spawned );

	// Have all enemies show up on thermal
	spawner::add_global_spawn_function( "axis", &enable_thermal_on_spawned );
	spawner::add_global_spawn_function( "allies", &enable_thermal_on_spawned );
	
	level.b_enhanced_vision_enabled = true;
	level.b_tactical_mode_enabled = true;
	level.b_player_scene_active = false;
	
	level.enable_thermal = &enable_thermal;
	level.disable_thermal = &disable_thermal;
}

function __main__()
{
	keyline_weapons();
}

function keyline_weapons()
{
	waittillframeend; // clientfields can't be set before a wait
	if ( level.b_tactical_mode_enabled )
	{
		array::thread_all(
			util::query_ents(
				AssociativeArray( "classname", "weapon_" ), true,
				[],
				true,
				true
			 ), &enable_keyline );
	}
}

function on_player_spawned()
{
	// ev init
	self.b_enhanced_vision_enabled = level.b_enhanced_vision_enabled;
	self.ev_state = false;
	self ev_activate_on_player( self.ev_state );
     
	// tmode init
  	self.b_tactical_mode_enabled = level.b_tactical_mode_enabled;
	self.tmode_state = false;
	
	b_playsound = false;
	
   	if ( !( SessionModeIsCampaignZombiesGame() ) )
   	if ( ( isdefined( GetLocalProfileInt( "tacticalModeAutoOn" ) ) && GetLocalProfileInt( "tacticalModeAutoOn" ) ) )
   	{
    	self.tmode_state = true;
		b_playsound = false;	   
   	}

	self tmode_activate_on_player( self.tmode_state, b_playsound );
	
	// sitrep   
	self clientfield::set_to_player( "sitrep_toggle", 1 );

	self thread check_keys_for_ev_and_tmode();
	self thread init_heroes();
}

//////////////////////////////////////////////////////////////////////////////////
//	EV and TMODE
//////////////////////////////////////////////////////////////////////////////////
function check_keys_for_ev_and_tmode() 
{
	self endon( "death" );
	self endon("killOEDMonitor");
	
	while( 1 )
	{
		/# level flagsys::wait_till_clear( "menu_open" ); #/
		
		// EV on up dpad
		if ( level.b_enhanced_vision_enabled && self.b_enhanced_vision_enabled && self ActionSlotOneButtonPressed() )
		{
			if ( !scene::is_igc_active() )
			{
			   	
 				self.ev_state = !( isdefined( self.ev_state ) && self.ev_state );
				self ev_activate_on_player( self.ev_state );
				
				// wait until the key is released
				while( self ActionSlotOneButtonPressed() )
				{
					{wait(.05);};	
				}
			}
		}
		
		// TMode on right dpad
		if ( !( SessionModeIsCampaignZombiesGame() ) && level.b_tactical_mode_enabled && self.b_tactical_mode_enabled && self ActionSlotFourButtonPressed() )
		{
			if ( !scene::is_igc_active() )
			{
				self.tmode_state = !( isdefined( self.tmode_state ) && self.tmode_state );
				self tmode_activate_on_player( self.tmode_state );
								
				visionset_mgr::activate( "visionset", "tac_mode", self, 0.05, 0.0, 0.8 );
				wait 0.05 + 0.0 + 0.8;
	
				while ( self ActionSlotFourButtonPressed() )
				{
					{wait(.05);};
				}
			}
		}	

		{wait(.05);};
	}
}

//////////////////////////////////////////////////////////////////////
///ENHANCED VISION - THERMAL
//////////////////////////////////////////////////////////////////////

//	Spawn function to automatically enable thermal on enemies
function enable_thermal_on_spawned()
{
	if ( self.team == "axis" )
	{
		self enable_thermal();
	}
	else if( self.team == "allies" )//keep seperate for now in case we need extra logic here
	{
		self enable_thermal();
	}
}

//TODO - will likely need to add in support for hud/lui changes
//	Called externally to enable an entity to have a thermal shader when EV is activated
//	self is the entity to have a thermal signature
//	str_disable - a notify that can be used to deactivate the thermal signature
function enable_thermal( str_disable )
{
	self endon( "death" );
	
	self clientfield::set( "thermal_active", 1 );
	self thread disable_thermal_on_death();
	
	if( isdefined( str_disable ) )
	{
		level waittill( str_disable );
		
		self disable_thermal();
	}
}

//	Remove the thermal signature on death
//	self is the entity that has a thermal signature
function disable_thermal_on_death()
{
	self endon( "disable_thermal" );
	
	self waittill( "death" );

	if( isdefined( self ) )
	{
		self disable_thermal();
	}
}

//	Remove the thermal signature from the entity
//	self is the entity that has a thermal signature
function disable_thermal()
{
	self clientfield::set( "thermal_active", 0  );
	
	self notify( "disable_thermal" );
}

//Toggles the vars used to enable/disable player access to thermal mode, also called EV
//This should be used when toggling during the course of the level
//If you just want to disable thermal access by default at level start, just use level.b_enhanced_vision_enabled = false;
function toggle_thermal_mode_for_players( b_enabled = true )
{
	level.b_enhanced_vision_enabled = b_enabled;
	foreach( e_player in level.players )
	{
		e_player.b_enhanced_vision_enabled = b_enabled;	
	}
}

//
//	 TMODE and EV are disabled & turned off or enabled for the player from a level script here.
//
function enable_ev( b_enabled = true )
{
	self.b_enhanced_vision_enabled = b_enabled;
	
	if ( !b_enabled )
		self ev_activate_on_player( b_enabled );
}

function enable_tac_mode( b_enabled = true )
{
	self.b_tactical_mode_enabled = b_enabled;
  	
    if ( b_enabled )	
	{
		if ( !( SessionModeIsCampaignZombiesGame() ) && ( isdefined( GetLocalProfileInt( "tacticalModeAutoOn" ) ) && GetLocalProfileInt( "tacticalModeAutoOn" ) ) )
	    {
			self tmode_activate_on_player( true, false );
		}
	}
	else
	{
    	self tmode_activate_on_player( false, false );
	}
}


//
//	external call from other scripts to turn ev on and off on a player
//
function set_player_ev( b_enabled = true )
{
	ev_activate_on_player( b_enabled );
}
	
function ev_activate_on_player( b_enabled = true )
{
	self.ev_state = b_enabled;

	// turn on ev - so turn off tmode 
	if ( self.ev_state )
	{
		if ( ( isdefined( self.tmode_state ) && self.tmode_state ) )
			self.tmode_state_before_ev = true;
	   	else
    		self.tmode_state_before_ev = false;
	   
	    self tmode_activate_on_player( false, false, false );
	}

	//the tutorial moment in New World is listening for this	
	if ( self.ev_state )
		self notify( "enhanced_vision_activated" ); 
 	else
		self notify( "enhanced_vision_deactivated" );	  

	self clientfield::set_to_player( "ev_toggle", self.ev_state );		 

	// turn off ev - maybe turn tmode back on
 	if ( !self.ev_state )
	{
    	if ( ( isdefined( self.tmode_state_before_ev ) && self.tmode_state_before_ev ) )
		{
			if ( !( SessionModeIsCampaignZombiesGame() ) && ( isdefined( GetLocalProfileInt( "tacticalModeAutoOn" ) ) && GetLocalProfileInt( "tacticalModeAutoOn" ) ) )	
	    	{
				self tmode_activate_on_player( true, false, false );
			}
		}
	}
}

function tmode_activate_on_player( b_enabled = true, b_playsound = true, b_turnoffev = true )
{
	self.tmode_state = b_enabled;


	if ( b_turnoffev && self.tmode_state )
   		self ev_activate_on_player( false );

  	if ( self.tmode_state )
		self notify( "tactical_mode_activated" ); 
	else
		self notify( "tactical_mode_deactivated" );

  	self TmodeSetServerUser( self.tmode_state );

	code = 0;

 	if( !isdefined( self.tmode_count ) )
		self.tmode_count = 0;

	self.tmode_count++;
    self.tmode_count &= 1;
	
	code = self.tmode_count;

	if ( b_playsound )
		code |= 0x2;

	if ( self.tmode_state )
		code |= 0x4;

	self clientfield::set_to_player( "tmode_toggle", code );
	
	self savegame::set_player_data("tmode", self.tmode_state );
}

function init_heroes()
{
	a_e_heroes = GetEntArray();
	foreach( e_hero in a_e_heroes )
	{
		if( ( isdefined( e_hero.is_hero ) && e_hero.is_hero ) )
		{
			e_hero thread enable_thermal();
		}
	}
}

///////////////////////////////////////////////////////////////////////////
/// TACTICAL MODE
///////////////////////////////////////////////////////////////////////////

//Toggles the vars used to enable/disable player access to tactical mode
//This should be used when toggling during the course of the level
//If you just want to disable tac mode access by default at level start, just use level.b_tactical_mode_enabled = false;


function toggle_tac_mode_for_players( b_enabled = true )
{
	level.b_tactical_mode_enabled = b_enabled;
	foreach( e_player in level.players )
	{
		e_player.b_tactical_mode_enabled = b_enabled;	
	}
}


//self = AI enemy actor that will draw in tmode for all players no matter what
function set_force_tmode( b_enabled = true )
{
	self.b_force_tmode = b_enabled;
	self clientfield::set( "force_tmode", b_enabled );
}


//////////////////////////////////////////////////////////////////////////
/// SITREP - KEYLINE
//////////////////////////////////////////////////////////////////////////

//Set b_interact to true if this is an interactive entity//TODO we may need support now for non-interactable, for now we assume this is an interact object
function enable_keyline( b_interact = false, str_disable )
{
	self endon( "death" );
	
	self clientfield::set( "sitrep_material", 1  );
	
	self thread disable_on_death();
	
	if( isdefined( str_disable ) )
	{
		level waittill( str_disable );
		self disable_keyline();
	}
}

function disable_on_death()
{
	self waittill( "death" );
	if( isdefined( self ) )
	{
		self disable_keyline();
	}
}

function disable_keyline()
{
	self clientfield::set( "sitrep_material", 0  );
}

function toggle_sitrep_for_players( b_active = true )
{
	foreach( player in level.players )
	{
		player.sitrep_active = !( isdefined( player.sitrep_active ) && player.sitrep_active );	
		player clientfield::set_to_player( "sitrep_toggle", player.sitrep_active );
	}
}

function init_sitrep_model()
{
	if( !IsDefined(self.angles) )
	{	
		self.angles = (0, 0 ,0 );
	}
		
	s_sitrep_bundle = level.scriptbundles[ "sitrep" ][ self.scriptbundlename ];
	
	e_sitrep = util::spawn_model( s_sitrep_bundle.model, self.origin, self.angles );
	
	if( isdefined( s_sitrep_bundle.sitrep_interact ) )
	{
		e_sitrep.script_sitrep_id =	s_sitrep_bundle.sitrep_interact;
	}
	else
	{
		e_sitrep.script_sitrep_id = 0;
	}
	
	return e_sitrep;
}
