package model.root;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import jv.object.PsDebug;
import jv.object.PsPanel;
import jv.object.PsUpdateIf;
import jv.objectGui.PsTabPanel;
import jv.project.PjProject_IP;
/**
 * Classe do Info Panel da RaizSolo
 *  @author Tome Matos
 */
@SuppressWarnings("serial")
public class RaizSolo_IP extends PjProject_IP implements ActionListener, ItemListener {

	protected	PsTabPanel					m_tabPanel;

	protected	RaizSolo		RaizSolo;
	protected	PsPanel				sliderPanelGeral;
	protected	PsPanel				sliderPanelRaiz;
	protected	PsPanel				sliderPanelSolo;
	protected	PsPanel				sliderPanelVisual;
	protected	Label				lArea;
	protected	Button			bRecompute;
	static		Label			nosOcupados = new Label();
	static		Label			nosOcupadosCorte = new Label();
	static		Label			apicesOcupados= new Label();
	static		Label		    aguaPlantaCount= new Label();
	static		Label		    fosforoPlantaCount= new Label();
	static		Label		    azotoPlantaCount= new Label();
	static		Label		    progressoCount= new Label();
	protected static	CheckboxGroup		m_cbg;
	protected static	Checkbox []			m_cOperation;
	protected	String []			m_sOperation = { "Normal", "Água", "Fósforo","Azoto","Raiz Água", "Raiz Fósforo","Raiz Azoto","Raiz Carbono"};
	static protected	CheckboxGroup		m_cbg_soil;
	protected static	Checkbox []			m_cOperation_soil;
	//protected	String []			m_sOperation_soil = { "Normal","Areia","Etc"}; Poderá ser mais aprofundado em futuros trabalhos
	protected	Checkbox			continuous = new Checkbox("Por iterações", false);
	static protected	Checkbox			crescimento = new Checkbox("Só crescem ápices",false);
	static protected	Checkbox			paredes = new Checkbox("Parar nas paredes",true);


	public RaizSolo_IP() {
		super();

		if (getClass() == RaizSolo_IP.class)
			init();
	}
	public void init() {
		super.init();

		m_tabPanel = new PsTabPanel();
		add(m_tabPanel);

		// Adicionar a label do titulo
		addTitle("Opções");

		// escolha para o modo de criação de raíz
		Panel crescPanel = new Panel();
		crescPanel.setLayout(new FlowLayout(FlowLayout.LEADING));
		add(crescPanel);
		{

			crescPanel.add(crescimento);
			crescimento.addItemListener(this);
			crescPanel.add(paredes);
			paredes.addItemListener(this);
			crescPanel.add(continuous);
			continuous.addItemListener(this);

		}

		sliderPanelGeral = new PsPanel();
		sliderPanelGeral.setLayout(new GridLayout(10, 1));
		m_tabPanel.addPanel("Geral",sliderPanelGeral);

		sliderPanelSolo = new PsPanel();
		sliderPanelSolo.setLayout(new GridLayout(11, 1));
		m_tabPanel.addPanel("Solo",sliderPanelSolo);

		sliderPanelRaiz = new PsPanel();
		sliderPanelRaiz.setLayout(new GridLayout(12, 1));
		m_tabPanel.addPanel("Raiz",sliderPanelRaiz);

		sliderPanelVisual = new PsPanel();
		sliderPanelVisual.setLayout(new GridLayout(10, 1));
		m_tabPanel.addPanel("Visualização",sliderPanelVisual);


		//escolhas para a cor
		Panel info = new Panel();
		info.setLayout(new GridLayout(1,1));
		info.add(new Label("Cor do solo:"));
		add(info);

		Panel pStyle = new Panel();
		pStyle.setLayout(new GridLayout(2,4));
		add(pStyle);
		{
			m_cbg = new CheckboxGroup();
			m_cOperation = new Checkbox[m_sOperation.length];
			for (int i=0; i<m_sOperation.length; i++) {
				m_cOperation[i] = new Checkbox(m_sOperation[i], m_cbg, i==0);
				m_cOperation[i].addItemListener(this);
				pStyle.add(m_cOperation[i]);
			}

		}

		/**	
		//escolhas para o solo
		Panel infoSoilType = new Panel();
		infoSoilType.setLayout(new GridLayout(1,1));
		infoSoilType.add(new Label("Tipo de solo:"));
		add(infoSoilType);


		Panel pSoilType = new Panel();
		pSoilType.setLayout(new FlowLayout(FlowLayout.CENTER));
		add(pSoilType);
		{
			m_cbg_soil= new CheckboxGroup();
			m_cOperation_soil = new Checkbox[m_sOperation_soil.length];
			for (int i=0; i<m_sOperation_soil.length; i++) {
				m_cOperation_soil[i] = new Checkbox(m_sOperation_soil[i], m_cbg_soil, i==0);
				m_cOperation_soil[i].addItemListener(this);
				pSoilType.add(m_cOperation_soil[i]);
			}

		}

		addLine(1);
		 **/

		/**
		//Add step button at bottom
		Panel buttonNextStep = new Panel();
		buttonNextStep.setLayout(new FlowLayout(FlowLayout.CENTER));
		bNextStep = new Button("Próxima Iteração");
		bNextStep.addActionListener(this);
		buttonNextStep.add(bNextStep);
		add(buttonNextStep);
		 */
		addLine(1);		 
		// Adicionar o butão "Recriar / Próxima Iteração"
		Panel buttonPanel = new Panel();
		buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
		bRecompute = new Button("Recriar / Próxima Iteração");
		bRecompute.addActionListener(this);
		buttonPanel.add(bRecompute);
		add(buttonPanel);

		// Criar uma label descritiva
		Label label = new Label("Nº de pontos do solo ocupados: ");
		// Criar painel que contem a label e o resultado
		Panel nosPanel = new Panel();

		Label progresso = new Label("Progresso: ");
		nosPanel.add(progresso);
		nosPanel.add(progressoCount);

		nosPanel.setLayout(new GridLayout(7, 2));
		nosPanel.add(label);
		nosPanel.add(nosOcupados);

		Label labelCorte = new Label("Nº de pontos ocupados no corte: ");
		nosPanel.add(labelCorte);
		nosPanel.add(nosOcupadosCorte);

		Label labelApices = new Label("Nº de ápices: ");
		nosPanel.add(labelApices);
		nosPanel.add(apicesOcupados);

		Label aguaPlanta = new Label("Água cedida à planta: ");
		nosPanel.add(aguaPlanta);
		nosPanel.add(aguaPlantaCount);

		Label fosforoPlanta = new Label("Fósforo cedido à planta: ");
		nosPanel.add(fosforoPlanta);
		nosPanel.add(fosforoPlantaCount);

		Label azotoPlanta = new Label("Azoto cedido à planta: ");
		nosPanel.add(azotoPlanta);
		nosPanel.add(azotoPlantaCount);

		add(nosPanel);



	}
	@SuppressWarnings("static-access")
	public void setParent(PsUpdateIf parent) {
		super.setParent(parent);
		RaizSolo = (RaizSolo)parent;
		setTitle(RaizSolo.getName());
		// Adicionar os paines de informação
		sliderPanelGeral.add(RaizSolo.runs.getInfoPanel());
		sliderPanelGeral.add(RaizSolo.linha.getInfoPanel());
		sliderPanelGeral.add(RaizSolo.repDifusao.getInfoPanel());

		sliderPanelSolo.add(RaizSolo.percentagem.getInfoPanel());	
		sliderPanelSolo.add(RaizSolo.densidadeAgua.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.mudancaAgua.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.iniAgua.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.iniAzoto.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.iniFosforo.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.iniRaio.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.fosfIni.getInfoPanel());
		sliderPanelSolo.add(RaizSolo.capSolo.getInfoPanel());	

		sliderPanelRaiz.add(RaizSolo.repUptake.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.percentRaiz.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.consumo.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.carbIntake.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.raio.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.limiteNut.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.maxRam.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.plateauCap.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.capRaise.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.nutSkip.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.rootLvls.getInfoPanel());
		sliderPanelRaiz.add(RaizSolo.internodes.getInfoPanel());

		sliderPanelVisual.add(RaizSolo.verSize.getInfoPanel());
		sliderPanelVisual.add(RaizSolo.rootSize.getInfoPanel());
		sliderPanelVisual.add(RaizSolo.partViewX.getInfoPanel());
		sliderPanelVisual.add(RaizSolo.partViewY.getInfoPanel());
		sliderPanelVisual.add(RaizSolo.partViewZ.getInfoPanel());

	}

	/**
	 * Caso o projecto mude algo e faça update ao panel, esse update é feito por a superclass do PjProject
	 */
	public boolean update(Object event) {
		if (RaizSolo==null) {
			if (PsDebug.WARNING) PsDebug.warning("missing parent, setParent not called");
			return false;
		}
		return super.update(event);
	}


	/**
	 * Trata dos events causados por butões
	 */
	public void actionPerformed(ActionEvent event) {
		if (RaizSolo==null)
			return;
		Object source = event.getSource();
		if (source == bRecompute) {
			if (RaizSolo.step == false)  
				RaizSolo.recompute();
			else
				RaizSolo.computeRootStep();

			RaizSolo.update(this);
		}

	}


	/**
	 * Trata dos item events
	 */
	public void itemStateChanged(ItemEvent event) {
		if (PsDebug.NOTIFY) PsDebug.notify("entered");
		Object source = event.getSource();
		if (source == continuous) {
			if (continuous.getState() == true) {
				RaizSolo.step = true;
				RaizSolo.recompute();
			} else {
				RaizSolo.step = false;
				//RaizSolo.recompute();
			}
		}
		if (source == crescimento) {
			//	RaizSolo.recompute();
		}
		if (m_cbg == null && m_cbg_soil == null)
			return;
		for (int i=0; i<m_cOperation.length; i++) {
			if (source == m_cOperation[i]) {
				RaizSolo.paintSoil(i);
				RaizSolo.partViewX.setValue(0);
				RaizSolo.partViewY.setValue(0);
				RaizSolo.partViewZ.setValue(0);
			}
		}
		/**for (int i=0; i<m_cOperation_soil.length; i++) {
			if (source == m_cOperation_soil[i]) {
				RaizSolo.changeSoil(i);
			}
		}*/
	}

	/**
	 * Método para mudar o solo seleccionado
	 * @param solo valor do solo a seleccionar
	 */
	static public void changeSoilCheck(int solo) {
		m_cbg_soil.setSelectedCheckbox(m_cOperation_soil[solo]);
	}

	/**
	 * Método para obter o solo seleccionado
	 * @return int valor do solo seleccionado
	 */
	static int getSoilVisualCheck() {
		for (int i=0; i<m_cOperation.length; i++) {
			if (m_cbg.getSelectedCheckbox() == m_cOperation[i]) {
				return i;
			}
		}
		return 0;
	}
}

