package com.evolute.siprp.client.panels.utils.navigation;

import java.util.HashMap;
import java.util.Map;

import com.evolute.siprp.client.panels.apps.MainPanel;
import com.evolute.siprp.client.panels.apps.AnaliseAcidentesTrabalho.AnaliseAcidentesTrabalhoPanel;
import com.evolute.siprp.client.panels.index.login.LoginPanel;
import com.evolute.siprp.client.panels.index.recover_pwd.RecoverPwdPanel;
import com.evolute.siprp.client.panels.utils.AppConstants;
import com.evolute.siprp.client.vo.Utilizador;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.RootPanel;

public class PageNavigation 
{
	private static PageNavigation instance = null;
	
	private RootPanel mainPanel;
	private String containerID;
	
	private Map< Integer, Object > panelsCache;
	
	private Utilizador userLogged = null;
	
	
	private PageNavigation()
	{
		this.containerID = AppConstants.MAIN_DIV;
		
		mainPanel = RootPanel.get( this.containerID );
		panelsCache = new HashMap< Integer, Object >();
	}

	
	public static PageNavigation getProvider()
	{
		if ( instance == null )
		{
			instance = new PageNavigation();
		}
		return instance;
	}
	
	
	public static void goPage( Integer pageID )
	{
		PageNavigation.getProvider().gotoPage( pageID );
	}
	
	
	/**
	 * usado quando se faz logout ...
	 * manter login panel e recover se existir ...
	 */
	public void clearCache()
	{
		Map< Integer, Object > newCache = new HashMap< Integer, Object >();
		
		if ( panelsCache.containsKey( NavigationConstants.LOGIN_PANEL ) )
		{
			newCache.put( NavigationConstants.LOGIN_PANEL, panelsCache.get( NavigationConstants.LOGIN_PANEL ) );
		}
//		if ( panelsCache.containsKey( NavigationConstants.RECOVER_PWD_PANEL ) )
//		{
//			newCache.put( NavigationConstants.RECOVER_PWD_PANEL, panelsCache.get( NavigationConstants.RECOVER_PWD_PANEL ) );
//		}
		
		panelsCache = newCache;
	}
	
	
	public void gotoPage( Composite newWindow )
	{
		if ( newWindow != null )
		{
			mainPanel.clear();
			mainPanel.add( newWindow );
		}
	}
	
	
	public void gotoPage( Integer pageID )
	{
		switch ( pageID.intValue() )
		{
			case NavigationConstants.LOGIN_PANEL: 
			{
				if ( ! panelsCache.containsKey( NavigationConstants.LOGIN_PANEL ) )
				{
					panelsCache.put( NavigationConstants.LOGIN_PANEL, new LoginPanel() );
				}				
				gotoPage( ( LoginPanel ) panelsCache.get( NavigationConstants.LOGIN_PANEL ) );
				History.newItem( String.valueOf( NavigationConstants.LOGIN_PANEL ) );
				break;
			}
			case NavigationConstants.RECOVER_PWD_PANEL: 
			{
				if ( ! panelsCache.containsKey( NavigationConstants.RECOVER_PWD_PANEL ) )
				{
					panelsCache.put( NavigationConstants.RECOVER_PWD_PANEL, new RecoverPwdPanel() );
				}				
				gotoPage( ( RecoverPwdPanel ) panelsCache.get( NavigationConstants.RECOVER_PWD_PANEL ) );
				History.newItem( String.valueOf( NavigationConstants.RECOVER_PWD_PANEL ) );
				break;
			}
			case NavigationConstants.APP_MAIN_PANEL: 
			{
				if ( ! isLoggedIn() )
				{
					gotoPage( NavigationConstants.LOGIN_PANEL );
					break;
				}
				
				if ( ! panelsCache.containsKey( NavigationConstants.APP_MAIN_PANEL ) )
				{
					panelsCache.put( NavigationConstants.APP_MAIN_PANEL, new MainPanel() );
				}
				gotoPage( ( MainPanel ) panelsCache.get( NavigationConstants.APP_MAIN_PANEL) );
				History.newItem( String.valueOf( NavigationConstants.APP_MAIN_PANEL ) );
				break;
			}
			case NavigationConstants.ANALISE_ACIDENTES_TRABALHO_PANEL: 
			{
				if ( ! isLoggedIn() )
				{
					gotoPage( NavigationConstants.LOGIN_PANEL );
					break;
				}
				
				if ( ! panelsCache.containsKey( NavigationConstants.ANALISE_ACIDENTES_TRABALHO_PANEL ) )
				{
					panelsCache.put( NavigationConstants.ANALISE_ACIDENTES_TRABALHO_PANEL, new AnaliseAcidentesTrabalhoPanel() );
				}				
				gotoPage( ( AnaliseAcidentesTrabalhoPanel ) panelsCache.get( NavigationConstants.ANALISE_ACIDENTES_TRABALHO_PANEL ) );
				History.newItem( String.valueOf( NavigationConstants.ANALISE_ACIDENTES_TRABALHO_PANEL ) );
				break;
			}
			default: 
			{
				break;
			}
		}
	}

	
	//TODO: find a better place for this stuff ...
	public boolean isLoggedIn()
	{
		return getUserLogged() != null;
	}
	
	public Utilizador getUserLogged()
	{
		return this.userLogged;
	}
	
	public void setUserLogged( Utilizador user )
	{
		this.userLogged = user;
	}
	
}
