package org.ellectra.gui.managebeans;

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

import javax.faces.application.Application;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIData;
import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;
import javax.faces.event.ActionEvent;

import org.ellectra.gui.utils.JSFUtils;


public class CPVcodesPage {

	String codesReturned; 
	String voccodesReturned;
	String vocRow;
	
	boolean showTable;
	int currow;
	
	private String[] codes = {"", "", "", "", "", "", "", "", "", ""}; 
	private String[] names = {"", "", "", "", "", "", "", "", "", ""};
	private String[] vocList = {"", "", "", "", "", "", "", "", "", ""};
	private boolean[] showCode = {false, false, false, false, false, false, false, false, false, false};
	
	
	public CPVcodesPage() {
		currow = 999;
		codesReturned=(String)org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "CPVCodes");
	}

	public String getCodesReturned() {
		codesReturned = (String)
			org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "CPVCodes");
		String codesTemp = codesReturned;
	
//		if (!codesTemp.length()>0) {
//			setShowTable(true);
//			
//			String[] splitted = codesTemp.split("\\?");
//			String codeOnly;
//			String nameOnly;
//			for (int i=0; i<splitted.length; i++) {
//				String wholeCode = splitted[i];
//				if (!wholeCode.isEmpty()) {
//					int lastOccur = wholeCode.indexOf("-");
//					codeOnly = wholeCode.substring(0, lastOccur+2);
//					nameOnly = wholeCode.substring(lastOccur+5);
//					int j = i-1;
//					codes[j] = codeOnly;
//					names[j] = nameOnly;
//					showCode[j] = true;
//				}
//			}
//		}
//		else 
//			setShowTable(false);
		
		return codesReturned;
	}

	public String getVocRow() {
		vocRow = (String)
		org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "VOCrow");
		
		String rowTemp = vocRow;
		
//		if (!rowTemp.isEmpty()) {
//			String[] splitted = rowTemp.split("voc");
//			String rowString = splitted[1];
//			int rowNumber = Integer.parseInt(rowString);
//			currow = rowNumber;
//		}
		
		return vocRow;
	}
	
	public String getVoccodesReturned() {
		voccodesReturned = (String)
		org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "VOCCodes");
		
		String vocs = voccodesReturned;
		String vocCodesToSet = processVocCodes(vocs);
		if (currow != 999)
			vocList[currow] = vocs;
		
//		if (!vocCodesToSet.isEmpty() && currow != 999) {
//			vocList[currow] = vocCodesToSet;
//		}
//		
		
		return voccodesReturned;
	}
	
	public String processVocCodes (String vocCodes){
		String vocCodesToSet = "";
//		if (!vocCodes.isEmpty()) {
//			String[] splitted = vocCodes.split("\\?");
//			String vocCodeOnly;
//			for (int i=0; i<splitted.length; i++) {
//				String wholeVocCode = splitted[i];
//				//if (!wholeVocCode.isEmpty()) {
//				int lastOccur = wholeVocCode.indexOf("-");
//				vocCodeOnly = wholeVocCode.substring(0, lastOccur+2);
//				
//				String vocCodeFormatted;
//				if (i==0)
//					vocCodeFormatted = vocCodeOnly;
//				else 
//					vocCodeFormatted = ", " + vocCodeOnly;
//				vocCodesToSet = vocCodesToSet + vocCodeFormatted;
//				//}
//			}
//		}
		return vocCodesToSet;
	}
	
	public void setVoccodesReturned(String voccodesReturned) {
		this.voccodesReturned = voccodesReturned;
	}

////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////	
	
	public void setCodesReturned(String codes) {
		this.codesReturned = codes;
	}

	public boolean isShowTable() {
		return showTable;
	}

	public void setShowTable(boolean showTable) {
		this.showTable = showTable;
	}

	public String[] getVocList() {
		return vocList;
	}

	public void setVocList(String[] vocList) {
		this.vocList = vocList;
	}

	public String[] getCodes() {
		return codes;
	}


	public void setCodes(String[] codes) {
		this.codes = codes;
	}


	public String[] getNames() {
		return names;
	}


	public void setNames(String[] names) {
		this.names = names;
	}


	public void setVocRow(String vocRow) {
		this.vocRow = vocRow;
	}


	public boolean[] getShowCode() {
		return showCode;
	}


	public void setShowCode(boolean[] showCode) {
		this.showCode = showCode;
	}

	public int getCurrow() {
		return currow;
	}

	public void setCurrow(int row) {
		this.currow = row;
	}


	
	
	

}