class FindServersMenuGameController extends PreGameSubMenuGameController
{
	private var m_serversListCtrl : weak< ListController >;
	private var NONE_CHOOSEN : Int32;
	default NONE_CHOOSEN = -1;
	private var curentlyChoosenServer : Int32;
	default curentlyChoosenServer = -1;
	private var m_LANStatusLabel : weak< inkText >;
	private var m_WEBStatusLabel : weak< inkText >;
	private var c_onlineColor : Color;
	private var c_offlineColor : Color;
	private var m_token : weak< inkText >;

	protected event OnInitialize()
	{
		var selectorsList : weak< inkVerticalPanel >;
		c_onlineColor = Color( 0, 255, 0, 255 );
		c_offlineColor = Color( 255, 0, 0, 255 );
		super.OnInitialize();
		GetSystemRequestsHandler().RegisterToCallback( 'OnServersSearchResult', this, 'OnServersSearchResult' );
		selectorsList = ( ( inkVerticalPanel )( GetWidget( 'Data/Table/Rows' ) ) );
		m_serversListCtrl = ( ( ListController )( selectorsList.GetController() ) );
		m_LANStatusLabel = ( ( inkText )( GetWidget( 'Data/Table/NetworkStatus/LAN/Status/Status' ) ) );
		m_WEBStatusLabel = ( ( inkText )( GetWidget( 'Data/Table/NetworkStatus/WEB/Status/Status' ) ) );
		m_token = ( ( inkText )( GetWidget( 'txtToken' ) ) );
		UpdateNetworkStatus();
	}

	public override function InitializeMenuName( menuName : weak< inkText > )
	{
		menuName.SetText( "FIND SERVERS" );
	}

	public override function InitializeButtons( buttonsList : weak< inkVerticalPanel > )
	{
		ReInitializeButtons();
	}

	private function UpdateNetworkStatus()
	{
		m_LANStatusLabel.SetText( "ONLINE BY DEFAULT" );
		m_LANStatusLabel.SetTintColor( c_onlineColor );
		if( GetSystemRequestsHandler().IsOnline() )
		{
			m_WEBStatusLabel.SetText( "ONLINE" );
			m_WEBStatusLabel.SetTintColor( c_onlineColor );
		}
		else
		{
			m_WEBStatusLabel.SetText( "OFFLINE" );
			m_WEBStatusLabel.SetTintColor( c_offlineColor );
		}
	}

	protected event OnCloudQuickmatch( e : inkPointerEvent )
	{
		var groupToken : String;
		if( e.IsAction( 'click' ) )
		{
			groupToken = m_token.GetText();
			GetSystemRequestsHandler().CloudQuickmatch( groupToken );
		}
	}

	protected event OnJoin( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			GetSystemRequestsHandler().JoinServer( curentlyChoosenServer );
		}
	}

	protected event OnLANServers( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			m_serversListCtrl.Clear();
			m_serversListCtrl.Refresh();
			GetSystemRequestsHandler().RequestLANServers();
			ClearButtons();
		}
	}

	protected event OnInternetServers( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			m_serversListCtrl.Clear();
			m_serversListCtrl.Refresh();
			GetSystemRequestsHandler().RequestInternetServers();
			ClearButtons();
		}
	}

	protected event OnBack( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) )
		{
			m_menuEventDispatcher.SpawnEvent( 'OnBack' );
		}
	}

	protected event OnServersSearchResult( servers : array< ServerInfo > )
	{
		var _servers : array< IScriptable >;
		var i : Int32;
		if( servers.Size() > 0 )
		{
			for( i = 0; i < servers.Size(); i += 1 )
			{
				_servers.PushBack( ( ( IScriptable )( servers[ i ] ) ) );
			}
			m_serversListCtrl.PushDataList( _servers, true );
			for( i = 0; i < servers.Size(); i += 1 )
			{
				GetServerInfoController( i ).RegisterToCallback( 'OnRelease', this, 'OnServerChoosen' );
			}
		}
		curentlyChoosenServer = NONE_CHOOSEN;
		ReInitializeButtons();
	}

	protected event OnServerChoosen( e : inkPointerEvent )
	{
		var newChoosenServerId : Int32;
		newChoosenServerId = GetChoosenServerId( curentlyChoosenServer );
		if( ( newChoosenServerId != NONE_CHOOSEN ) && ( curentlyChoosenServer != newChoosenServerId ) )
		{
			if( curentlyChoosenServer != NONE_CHOOSEN )
			{
				GetServerInfoController( curentlyChoosenServer ).SetMarked( false );
			}
			curentlyChoosenServer = newChoosenServerId;
			GetServerInfoController( curentlyChoosenServer ).SetMarked( true );
			ClearButtons();
			ReInitializeButtons();
		}
	}

	private function GetServerInfoController( i : Int32 ) : ServerInfoController
	{
		return ( ( ServerInfoController )( m_serversListCtrl.GetItemAt( i ).GetController() ) );
	}

	public function GetChoosenServerId( omitItem : Int32 ) : Int32
	{
		var i : Int32;
		for( i = 0; i < m_serversListCtrl.Size(); i += 1 )
		{
			if( i == omitItem )
			{
				continue;
			}
			if( GetServerInfoController( i ).IsMarked() )
			{
				return i;
			}
		}
		return NONE_CHOOSEN;
	}

	public function ClearButtons()
	{
		var buttonsList : weak< inkVerticalPanel >;
		buttonsList = ( ( inkVerticalPanel )( GetWidget( 'MainColumn\Container\ButtonsList' ) ) );
		buttonsList.RemoveAllChildren();
	}

	public function ReInitializeButtons()
	{
		var buttonsList : weak< inkVerticalPanel >;
		buttonsList = ( ( inkVerticalPanel )( GetWidget( 'MainColumn\Container\ButtonsList' ) ) );
		AddButtons( buttonsList );
	}

	public function AddButtons( buttonsList : weak< inkVerticalPanel > )
	{
		ClearButtons();
		AddButton( buttonsList, "CLOUD QUICKMATCH", 'OnCloudQuickmatch' );
		if( curentlyChoosenServer == NONE_CHOOSEN )
		{
			AddButton( buttonsList, "JOIN SERVER ", 'OnJoin' );
			Deactivate( GetButton( buttonsList, "JOIN SERVER" ) );
		}
		else
		{
			AddButton( buttonsList, "JOIN SERVER " + ( ( String )( ( curentlyChoosenServer + 1 ) ) ), 'OnJoin' );
		}
		AddButton( buttonsList, "FIND LAN SERVERS", 'OnLANServers' );
		if( GetSystemRequestsHandler().IsOnline() )
		{
			AddButton( buttonsList, "FIND INTERNET SERVERS", 'OnInternetServers' );
		}
		else
		{
			AddButton( buttonsList, "INTERNET SERVERS UNAVAILABLE", 'OnInternetServers' );
			Deactivate( GetButton( buttonsList, "INTERNET SERVERS UNAVAILABLE" ) );
		}
		AddButton( buttonsList, "BACK", 'OnBack' );
	}

	private function Deactivate( widget : weak< inkWidget > )
	{
		widget.SetInteractive( false );
		widget.SetOpacity( 0.30000001 );
	}

	private function GetButton( buttonsList : weak< inkVerticalPanel >, const name : ref< String > ) : weak< inkWidget >
	{
		var i : Int32;
		var button : weak< inkWidget >;
		var logicController : weak< inkButtonDpadSupportedController >;
		for( i = 0; i < buttonsList.GetNumChildren(); i += 1 )
		{
			button = buttonsList.GetWidget( i );
			logicController = ( ( inkButtonDpadSupportedController )( button.GetController() ) );
			if( logicController.GetButtonText() == name )
			{
				return button;
			}
		}
		return NULL;
	}

}

