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_old {

	String codesReturned; 
	String voccodesReturned;
	
	Integer cpvNum;
	List cpvCodes = new ArrayList(); 
	boolean showTable;
	
	private UIData data = null;
	
	public CPVcodesPage_old() {
		//cpvCodes = new ArrayList();
		codesReturned=(String)org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "CPVCodes");
	}

	
//	public void eventSelectCode(ValueChangeEvent vce) {
//		cpvCodesSelected = (String[]) vce.getNewValue();
//		
//		//getFinalCodes().clear();
//		for (int i=0; i<cpvCodesSelected.length; i++) {
//			String value = cpvCodesSelected[i];
//			String label = String.valueOf(i);
//			SelectItem result = new SelectItem(label, value);
//
//			getFinalCodes().add(result);
//		}
//	}
//
//	public String moveSelected() {
//		String[] values = getSelectedCpvCodes();
//
//		int length = values.length;
//		for (int i=0; i<length; i++) {
//			String value = values[i];
//			getSelectedCodes().add(removeItem(value, getInvisibleItems()));
//		}
//	}
//
//	private SelectItem removeItem(String value, List items) {
//		SelectItem result = null;
//
//		int size = items.size();
//		for (int i=0; i<size; i++) {
//			SelectItem item = (SelectItem) items.get(i);
//			if (value.equals(item.getValue())) {
//				result = (SelectItem) items.remove(i);
//				break;
//			}
//		}
//
//		return result;
//	}
	
	public Integer getCpvNum() {
		return cpvNum;
	}

	public void setCpvNum(Integer cpvNum) {
		this.cpvNum = cpvNum;
	}

	public List getCpvCodes() {
		return cpvCodes;
	}

	public void setCpvCodes(List cpvCodes) {
		this.cpvCodes = cpvCodes;
	}

	public void addCpvCode(CpvCode code) {
		cpvCodes.add(code);
	}
	
	public String getCodesReturned() {
		codesReturned = (String)
			org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "CPVCodes");
		String codesTemp = codesReturned;
	
//		if (!codesTemp.isEmpty()) {
//			setShowTable(true);
//			cpvCodes.clear();
//			
//			String[] splitted = codesTemp.split("\\?");
//			String codeOnly;
//			String nameOnly;
//			for (int i=0; i<splitted.length; i++) {
//				CpvCode code = new CpvCode();
//				
//				String wholeCode = splitted[i];
//				if (!wholeCode.isEmpty()) {
//					int lastOccur = wholeCode.indexOf("-");
//					codeOnly = wholeCode.substring(0, lastOccur+2);
//					nameOnly = wholeCode.substring(lastOccur+5);
//					code.setId(i);
//					code.setCodeNumber(codeOnly);
//					code.setCodeName(nameOnly);
//					//code.setVocCodes("");
//					
//					////////////TODO: enhance
//					//String vocs = getVoccodesReturned();
//					//code.setVocCodes( processVocCodes(vocs) );
//					///////////////
//					
//					addCpvCode(code);
//					
//					JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "CpvCode", code);
//				}
//			}
//			cpvNum = getCpvCodes().size();
//			//setCpvCodes(cpvCodesTemp);
//			//setCpvNum(cpvCodesTemp.size());
//		}
//		else 
//			setShowTable(false);
		
		return codesReturned;
	}

	public String processVocCodes (String vocCodes){
		String vocCodesToSet = "";
//		if (!vocCodes.isEmpty()) {
//			char rowNumber = vocCodes.charAt(0);
//			
//			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 String getVoccodesReturned() {
		voccodesReturned = (String)
		org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "VOCCodes");
		
		
		return voccodesReturned;
	}
	
	public void setVoccodesReturned(String voccodesReturned) {
		this.voccodesReturned = voccodesReturned;
	}
	
//	public String getVoccodesReturned() {}


	
//	public void editEvent(ActionEvent event){
	
	public String editEvent () {
		CpvCode selectedCode = (CpvCode) data.getRowData();//this.getData().getRowData();
		String vocs = voccodesReturned;
		selectedCode.setVocCodes( processVocCodes(vocs) );
		return "editVoc";
		//////////////
//		CpvCode selectedCode = null;

//		UIComponent tmpComponent = event.getComponent();
//
//		while (null != tmpComponent && !(tmpComponent instanceof UIData))
//		      tmpComponent = tmpComponent.getParent(); //parent is the dataTable
//
//		if (tmpComponent != null && (tmpComponent instanceof UIData)) {
//			Object tmpRowData = ((UIData) tmpComponent).getRowData();
//			if (tmpRowData instanceof CpvCode) {
//				selectedCode = (CpvCode) tmpRowData;
//				String vocs = voccodesReturned;
//				selectedCode.setVocCodes( processVocCodes(vocs) );
//		      }
//		    }

	}

	//new solution
	public void edit(ActionEvent event) {
		UIData datatable = findParentHtmlDataTable(event.getComponent());
		CpvCode selectedCode = getCpvCode(datatable.getRowIndex() + datatable.getFirst());
		String vocs = voccodesReturned;
		selectedCode.setVocCodes( processVocCodes(vocs) );
	}
		
	private CpvCode getCpvCode(int index) {
        CPVcodesPage_old managedCpvBean = (CPVcodesPage_old)JSFUtils.getManagedObject("CPVcodesPage");
		
		return (CpvCode) managedCpvBean.getCpvCodes().get(index);
	}
	
	private HtmlDataTable findParentHtmlDataTable(UIComponent component) {

		if (component == null) {
			return null;
		}
		if (component instanceof HtmlDataTable) {
			return (HtmlDataTable) component;
		}
		return findParentHtmlDataTable(component.getParent());
	}
	

////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////	
	public String editVocAction() {
		CpvCode selectedCode = (CpvCode) this.getData().getRowData();
		
		FacesContext ctx = FacesContext.getCurrentInstance();
		ctx.getExternalContext().getSessionMap().put("EditVoc", selectedCode);
		
		//selectedCode.setVocCodes("");
		return "editVoc";
	}
	
	public String saveVocAction() {
		FacesContext ctx = FacesContext.getCurrentInstance();
		Application app = ctx.getApplication();
		CpvCode toBeUpdated = (CpvCode)JSFUtils.getManagedObject("EditVoc");//(CpvCode)ctx.getExternalContext().getSessionMap().get("EditVoc");
		toBeUpdated.setVocCodes( "test" );
		
		updateVocEvent(toBeUpdated);
		return "back";
	}
	
	public void updateVocEvent(CpvCode toBeUpdated) {
		for (int i=0; i<cpvCodes.size(); i++) {
			CpvCode temp = (CpvCode)cpvCodes.get(i);
			if (temp.getId() == toBeUpdated.getId()) {
				String vocs = (String)
				org.ellectra.gui.utils.JSFUtils.retrieveOnSession(javax.faces.context.FacesContext.getCurrentInstance(), "VOCCodes");
				toBeUpdated.setVocCodes( "test" );	//processVocCodes(vocs)
				
				cpvCodes.set(i, toBeUpdated);
				return;
			}
		}
		
	}
	
	public void setCodesReturned(String codes) {
		this.codesReturned = codes;
	}

	public boolean isShowTable() {
		return showTable;
	}

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





	public UIData getData() {
		return data;
	}

	public void setData(UIData data) {
		this.data = data;
	}
	

}