/** klasa przechowujaca dane team-deathmatch'owej rozgrywki
 */

public class NetTeamInfo extends NetCompetitorInfo
{
    final static int NONE       = 0,  
                     _FIRST     = 1,
                     BLUE       = 1,
                     RED        = 2,
                     TEAMS_CNT  = 3;
    
    final static String []_TEAM_NAMES = 
                    {"&TEAM_NONE_NAME&",
                     "&TEAM_BLUE_NAME&",
                     "&TEAM_RED_NAME&"};   
    
    final static String []_TEAM_NAMES_NL = 
                    {"NONE",
                     "BLUE",
                     "RED"};   
    
    static String GetName(int nTeam)
    {
        if ( nTeam >=0 && nTeam < _TEAM_NAMES.length )
            return _TEAM_NAMES[ nTeam ]; 
        return "team "+nTeam+" not found";
    }
    
    static String GetName_NL(int nTeam)
    {
        if ( nTeam >=0 && nTeam < _TEAM_NAMES_NL.length )
            return _TEAM_NAMES_NL[ nTeam ]; 
        return "team "+nTeam+" not found";
    }
    
    
    static String GetLocName(int nTeam)
    {
        return Text.Get( GetName( nTeam ) );
    }
    
    final static String []_TEAM_SKINS = 
                    {"",
                     "@Team_Blue",
                     "@Team_Red"};    
    
    static int GetEnemyTeamId( NetTeamInfo cTeamInfo )
    {
        if ( cTeamInfo != null )
            return GetEnemyTeamId( cTeamInfo.GetTeamId() );
        return NONE;
    }
    
    static int GetEnemyTeamId( int nTeamId )
    {
        if ( nTeamId == BLUE )
            return RED;
        if ( nTeamId == RED )
            return BLUE;
        return NONE;
    }
    
        
    String  GetTeamName(){ return GetName(); }
    
    // ilosc graczy w teamie
    private int m_nNumPlayers = 0;
    
    int     GetNumPlayers()   { return m_nNumPlayers; }
    void    IncNumPlayers()   { m_nNumPlayers++;      }
    void    DecNumPlayers()   { m_nNumPlayers--;      }
    
    ///////////////////////////////////////////////////////
    // inicjalizacja obiektu tak aby replikowal atrybuty //
    ///////////////////////////////////////////////////////
    
    int METHOD_TryToAddToGameInfo = 0;

    private CTFFlag m_cFlag        = null;
    private CTFFlag m_cEnemyFlag   = null;
    
    void SetCTFFlag( int nFlag, CTFFlag cFlag )      
    {   
        if ( nFlag == CTFFlag.OWN )
            m_cFlag = cFlag;
        else
            m_cEnemyFlag = cFlag;
    }
    CTFFlag GetCTFFlag(int nFlag)         
    {   
        return ( nFlag==CTFFlag.OWN ) ? 
               m_cFlag : m_cEnemyFlag ; 
    }
        
    void CleanResults()
    {
        super.CleanResults();
        
        //TODO: dopisywac to wszystkie wyniki do skasowania
    }
    
    void OnCreate()
    {
        METHOD_TryToAddToGameInfo = RegisterCallMethod( "TryToAddToGameInfo" );
        
        super.OnCreate();
        
        TryToAddToGameInfo();
    }
    
    
    void OnDestroy()
    {
        // obiekt musi sie skasowac z listy
        if ( m_cGameInfo != null)
        {
            m_cGameInfo.RemoveTeamInfo( this );
        }
    }   
    
	void TryToAddToGameInfo()
	{
        if (m_cModule != null && m_cModule.GetGameInfo() != null)
        {
            m_cModule.GetGameInfo().AddTeamInfo( this );
            return;
        }
                    
		CallMethodIn( METHOD_TryToAddToGameInfo, 0.1f );
	}
    
    static int sm_nSetData;
    
    public void ReplicationInit()
    {
        //TODO: zastanowic sie cz te dane sa potrzebne 
        //klientowi, bo jak nie to po co ten obiekt ma byc replikowalny
		sm_nSetData = NewReplicationSet("m_nNumPlayers");
        SetNetPriority(2.0f);
    }
    
    public void OnReplicateServer()
    {
        super.OnReplicateServer();
        
        Replicate( sm_nSetData );
    }

    void SetTeamId( int nId )
    {
        super.SetTeamId( nId );
        
        if ( Role == ERole._AUTHORITY )
        {
            if ( nId >=0 && nId < _TEAM_NAMES.length ) 
                SetName( _TEAM_NAMES[ nId ] );
            else
                SetName( "noname" );
        }
    }
        
    void SetExcluded(boolean bExcluded)
    {
        boolean bLastExcluded = IsExcluded();
        
        super.SetExcluded( bExcluded );
        
        if ( !bLastExcluded && IsExcluded() )
        {
            if ( m_cGameInfo instanceof NetGameInfo )
                ((NetGameInfo)m_cGameInfo).EventTeamExcluded( this );    
            else
			    CrashLog( "SetExcluded " + this + " cModule == null || m_cGameInfo "+m_cGameInfo+"\n");
        }
    }
}
