package gov.nih.nci.cananolab.restful.core;

import gov.nih.nci.cananolab.restful.view.SimpleTabsBean;
import gov.nih.nci.cananolab.service.security.UserBean;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class TabGenerationBO {

	public SimpleTabsBean getTabs(HttpServletRequest httpRequest, String homePage) {
		
		List<String[]> tabs = new ArrayList<String[]>();
		HttpSession session = httpRequest.getSession(false);
		UserBean userBean = (session == null)? null : (UserBean)session.getAttribute("user");
		boolean hasResultWaiting = (session == null)? false : (Boolean)session.getAttribute("hasResultsWaiting") ;
		homePage = homePage.trim().toLowerCase();
		
		String urlBase = getUrlBase(httpRequest.getRequestURL().toString());
		
		SimpleTabsBean tabsBean = new SimpleTabsBean();
		tabsBean.setUserLoggedIn(userBean != null);
		
		if (userBean == null) { //not logged in
			String[] tabWithLink = new String[2];
			if (homePage.length() == 0 || !homePage.startsWith("true")) {
				tabWithLink[0] = "HOME";
				tabWithLink[1] = urlBase + "index.html#/";
				tabs.add(tabWithLink.clone());
				
				tabWithLink = new String[2];
				tabWithLink[0] = "PROTOCOLS";
				tabWithLink[1] =  urlBase + "manageProtocol.do";;
				tabs.add(tabWithLink);
				
				tabWithLink = new String[2];
				tabWithLink[0] = "SAMPLES";
				tabWithLink[1] =  urlBase + "#/manageSamples";
				tabs.add(tabWithLink);
				
				tabWithLink = new String[2];
				tabWithLink[0] = "PUBLICATIONS";
				tabWithLink[1] =  urlBase + "#/managePublications";;
				tabs.add(tabWithLink);
				
			}
			tabWithLink = new String[2];
			tabWithLink[0] = "HELP";
			tabWithLink[1] = "https://wiki.nci.nih.gov/display/caNanoLab/caNanoLab+User%27s+Guide";
			tabs.add(tabWithLink);
			tabWithLink = new String[2];
			tabWithLink[0] = "GLOSSARY";
			tabWithLink[1] = "https://wiki.nci.nih.gov/display/caNanoLab/caNanoLab+Glossary";
			tabs.add(tabWithLink);
			
			if (homePage.length() == 0 || !homePage.startsWith("true")) {
				tabWithLink = new String[2];
				tabWithLink[0] = "LOGIN";
				tabWithLink[1] =  urlBase + "index.html#/login";;
				tabs.add(tabWithLink);
			}
		} else {
			
			String[] tabWithLink = new String[2];
			
			tabWithLink[0] = "HOME";
			tabWithLink[1] = urlBase + "#/home";
			tabs.add(tabWithLink.clone());

			tabWithLink = new String[2];
			tabWithLink[0] = "PROTOCOLS";
			tabWithLink[1] =  urlBase + "manageProtocol.do";;
			tabs.add(tabWithLink);

			tabWithLink = new String[2];
			tabWithLink[0] = "SAMPLES";
			tabWithLink[1] =  urlBase + "#/manageSamples";;
			tabs.add(tabWithLink);

			tabWithLink = new String[2];
			tabWithLink[0] = "PUBLICATIONS";
			tabWithLink[1] =  urlBase + "#/managePublications";;
			tabs.add(tabWithLink);
			
			if (userBean.isCurator()) {
				tabWithLink = new String[2];
				tabWithLink[0] = "CURATION";
				tabWithLink[1] =  urlBase + "manageCuration.do";
				tabs.add(tabWithLink);
			}
			
			
			if (userBean.isCurator() && hasResultWaiting) {
				tabWithLink = new String[2];
				tabWithLink[0] = "RESULTS";
				tabWithLink[1] =  urlBase + "TBD";
				tabs.add(tabWithLink);
			}
			
			tabWithLink = new String[2];
			tabWithLink[0] = "HELP";
			tabWithLink[1] = "https://wiki.nci.nih.gov/display/caNanoLab/caNanoLab+User%27s+Guide";
			tabs.add(tabWithLink);
			
			tabWithLink = new String[2];
			tabWithLink[0] = "GLOSSARY";
			tabWithLink[1] = "https://wiki.nci.nih.gov/display/caNanoLab/caNanoLab+Glossary";
			tabs.add(tabWithLink);
			
			tabWithLink = new String[2];
			tabWithLink[0] = "LOGOUT";
			tabWithLink[1] =  urlBase + "index.html#/logout";;
			tabs.add(tabWithLink);
		}
		
		tabsBean.setTabs(tabs);

		return tabsBean;
		
	}
	
	protected String getUrlBase(String fullUrl) {
		if (fullUrl == null || fullUrl.length() == 0)
			return "";
		String token = "/caNanoLab/";
		int end = fullUrl.indexOf(token) + token.length();
		return fullUrl.substring(0, end);
	}
}

@@ -1287,32 +1287,34 @@
 %5D =  urlBase + %22
+#/
 manageProtocol.d
@@ -1307,27 +1307,25 @@
 nageProtocol
-.do
+s
 %22;;%0A%09%09%09%09tabs
@@ -2486,24 +2486,26 @@
  urlBase + %22
+#/
 manageProtoc
@@ -2506,19 +2506,17 @@
 Protocol
-.do
+s
 %22;;%0A%09%09%09t

Reset Protocol tab link