package com.rjconsultores.ventaboletos.web.gui.controladores.seguridad.contingencia;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;

import com.rjconsultores.ventaboletos.enums.TypeEventListener;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;

@Controller("agreementContigenciaController")
@Scope("prototype")
public class AgreementContigenciaController extends MyGenericForwardComposer {

	public static final String TITULO_CONTIGENCIA_CONTROLLER = "editarContigencia.window.title";
	
	private static final long serialVersionUID = 1L;
	
	private Checkbox chkAgree;
	private Button btnAceitar;
	private EditarContigenciaController contigenciaController;
	private EventListenerOnClickContingenciaBotaoContingencia eventoPai;
	
	@Override
	public void doAfterCompose(Component comp) throws Exception {
		contigenciaController = (EditarContigenciaController) Executions.getCurrent().getArg().get("controller");
		eventoPai = (EventListenerOnClickContingenciaBotaoContingencia) Executions.getCurrent().getArg().get("eventoPai");
		
		super.doAfterCompose(comp);
		chkAgree.setLabel(String.format("Deseja realmente ativar a contingência para a empresa %s na UF %s?", 
				contigenciaController.getEmpresaSelecionada().getNombempresa(), 
				contigenciaController.getEstadoSelecionado().getCveestado()));
		chkAgree.addEventListener(TypeEventListener.ON_CHECK.getEvent(), new EventListenerChkAgree());
		btnAceitar.addEventListener(TypeEventListener.ON_CLICK.getEvent(), new EventListenerBtnAceite());
	}
	
	private final class EventListenerBtnAceite implements EventListener {
		@Override
		public void onEvent(Event event) throws Exception {
			eventoPai.gravaLogHistoricoContingencia();
		}
	}

	private final class EventListenerChkAgree implements EventListener {
		@Override
		public void onEvent(Event event) throws Exception {
			btnAceitar.setDisabled(!chkAgree.isChecked());
		}
	}
	
}
