/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.web.gui.controladores.pricing;

import java.util.Calendar;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.util.resource.Labels;
import org.zkoss.zhtml.Messagebox;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.Clients;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Timebox;

import com.rjconsultores.ventaboletos.entidad.Pricing;
import com.rjconsultores.ventaboletos.entidad.PricingDia;
import com.rjconsultores.ventaboletos.service.PricingDiaService;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
import com.rjconsultores.ventaboletos.utilerias.UsuarioLogado;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;
import com.rjconsultores.ventaboletos.web.utilerias.MyListbox;

/**
 * 
 * @author Rafius
 */
@Controller("editarPricingDiaController")
@Scope("prototype")
public class EditarPricingDiaController extends MyGenericForwardComposer {

	private static final long serialVersionUID = 1L;
	private static Logger log = Logger.getLogger(EditarPricingDiaController.class);

	@Autowired
	private PricingDiaService pricingDiaService;
	private PricingDia pricingDia;
	private MyListbox pricingDiaList;

	private Checkbox rdLun;
	private Checkbox rdMar;
	private Checkbox rdMie;
	private Checkbox rdJue;
	private Checkbox rdVie;
	private Checkbox rdSab;
	private Checkbox rdDom;
	private Timebox horarioinicio;
	private Timebox horariofin;

	private Radio rdFecViaje;
	private Radio rdFecVenta;

	@Override
	public void doAfterCompose(Component comp) throws Exception {

		pricingDia = (PricingDia) Executions.getCurrent().getArg().get("pricingDia");
		pricingDiaList = (MyListbox) Executions.getCurrent().getArg().get("pricingDiaList");

		super.doAfterCompose(comp);

		if (pricingDia.getPricingdiaId() != null) {
			if (pricingDia.getIndlunes() == Boolean.TRUE) {
				rdLun.setChecked(true);
			}
			if (pricingDia.getIndmartes() == Boolean.TRUE) {
				rdMar.setChecked(true);
			}
			if (pricingDia.getIndmiercoles() == Boolean.TRUE) {
				rdMie.setChecked(true);
			}
			if (pricingDia.getIndjueves() == Boolean.TRUE) {
				rdJue.setChecked(true);
			}
			if (pricingDia.getIndviernes() == Boolean.TRUE) {
				rdVie.setChecked(true);
			}
			if (pricingDia.getIndsabado() == Boolean.TRUE) {
				rdSab.setChecked(true);
			}
			if (pricingDia.getInddomingo() == Boolean.TRUE) {
				rdDom.setChecked(true);
			}
			if (pricingDia.getIndfecventa() == Boolean.TRUE) {
				rdFecVenta.setChecked(true);
			}
			if (pricingDia.getIndfecviaje() == Boolean.TRUE) {
				rdFecViaje.setChecked(true);
			}
		}
	}

	public boolean noTraslapa() {
		List<PricingDia> lsVT1 = pricingDiaService.buscarTraslapa(pricingDia);

		// Nao Traslapen
		Boolean podeSalvar = false;
		Boolean[] podeTodos = new Boolean[lsVT1.isEmpty() ? 1 : lsVT1.size()];
		boolean podeMesmo = true;

		// Se a lista for nula / Salva
		if (lsVT1.isEmpty()) {
			podeMesmo = true;
			podeTodos[0] = true;
			// Se a lista não for nula e for algo novo... não pode salvar
		} else {
			int i = 0;
			for (PricingDia p : lsVT1) {
				if (p.getPricingdiaId().equals(pricingDia.getPricingdiaId())) {
					podeTodos[i] = true;
					continue;
				}
				podeSalvar = true;
				if (pricingDia.getIndlunes()) {
					if (p.getIndlunes() == pricingDia.getIndlunes()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndmartes()) {
					if (p.getIndmartes() == pricingDia.getIndmartes()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndmiercoles()) {
					if (p.getIndmiercoles() == pricingDia.getIndmiercoles()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndjueves()) {
					if (p.getIndjueves() == pricingDia.getIndjueves()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndviernes()) {
					if (p.getIndviernes() == pricingDia.getIndviernes()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndsabado()) {
					if (p.getIndsabado() == pricingDia.getIndsabado()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getInddomingo()) {
					if (p.getInddomingo() == pricingDia.getInddomingo()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndfecventa()) {
					if (p.getIndfecventa() == pricingDia.getIndfecventa()) {
						podeSalvar = false;
					}
				}
				if (pricingDia.getIndfecviaje()) {
					if (p.getIndfecviaje() == pricingDia.getIndfecviaje()) {
						podeSalvar = false;
					}
				}
				podeTodos[i] = podeSalvar;
				i++;
			}
		}

		for (int i = 0; i < podeTodos.length; i++) {
			if (podeTodos[i] != null) {
				if (!podeTodos[i]) {
					podeMesmo = false;
				}
			}
		}
		return podeMesmo;
	}

	public void onClick$btnAdicionarDia(Event ev) throws Exception {

		horarioinicio.getValue();
		horariofin.getValue();

		horarioinicio.setValue(DateUtil.getFecInicio(horarioinicio.getValue()).getTime());
		horariofin.setValue(DateUtil.getFecInicio(horariofin.getValue()).getTime());

		if (horarioinicio.getValue().after(horariofin.getValue())) {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.datas"), Labels.getLabel("editarPricingController.windowDia.title"), Messagebox.OK, Messagebox.EXCLAMATION);
			return;
		}

		if (!rdFecViaje.isChecked() && !rdFecVenta.isChecked()) {
			Clients.alert(Labels.getLabel("EditarPricingDiaController.MSG.obligatorioData"),
					Labels.getLabel("editarPricingController.windowDia.title"),
					Messagebox.EXCLAMATION);
			return;
		}

		if (!rdLun.isChecked() && !rdMar.isChecked() && !rdMie.isChecked() && !rdJue.isChecked() && !rdVie.isChecked() && !rdSab.isChecked() && !rdDom.isChecked()) {
			Clients.alert(Labels.getLabel("EditarPricingDiaController.MSG.obligatorioDia"), Labels.getLabel("editarPricingController.windowDia.title"), Messagebox.EXCLAMATION);
			return;
		}
		try {
			pricingDia.setIndlunes(rdLun.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setIndmartes(rdMar.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setIndmiercoles(rdMie.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setIndjueves(rdJue.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setIndviernes(rdVie.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setIndsabado(rdSab.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setInddomingo(rdDom.isChecked() ? Boolean.TRUE : Boolean.FALSE);

			pricingDia.setIndfecventa(rdFecVenta == null ? Boolean.FALSE :
					rdFecVenta.isChecked() ? Boolean.TRUE : Boolean.FALSE);
			pricingDia.setIndfecviaje(rdFecViaje == null ? Boolean.FALSE :
					rdFecViaje.isChecked() ? Boolean.TRUE : Boolean.FALSE);

			pricingDia.setHorarioinicio(DateUtil.getFecInicio(horarioinicio.getValue()).getTime());
			pricingDia.setHorariofin(DateUtil.getFecInicio(horariofin.getValue()).getTime());

			if (noTraslapa()) {
				if (pricingDia.getPricingdiaId() != null) {
					pricingDiaService.actualizacion(pricingDia);
					pricingDiaList.updateItem(pricingDia);
				} else {
					pricingDiaService.suscribir(pricingDia);
					pricingDiaList.addItem(pricingDia);
				}
				closeWindow();
			} else {
				Messagebox.show(Labels.getLabel("editarPricingController.MSG.traslapa"),
						Labels.getLabel("editarPricingController.windowDia.title"),
						Messagebox.OK, Messagebox.EXCLAMATION);
			}
		} catch (Exception ex) {
			log.error("",ex);
			Clients.alert(Labels.getLabel("MSG.Error"),
					Labels.getLabel("editarPricingController.windowDia.title"), Messagebox.ERROR);
		}

	}

	public void onClick$btnRemoverDia(Event ev) throws InterruptedException {
		PricingDia pDia = (PricingDia) pricingDiaList.getSelected();
		if (pDia != null) {
			int resp = Messagebox.show(Labels.getLabel("editarPricingController.MSG.borrarPerguntaDia"), Labels.getLabel("editarPricingController.windowDia.title"),
					Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);

			if (resp == Messagebox.YES) {
				pricingDia.setUsuarioId(UsuarioLogado.getUsuarioLogado().getUsuarioId());
				pricingDia.setFecmodif(Calendar.getInstance().getTime());
				pricingDia.setActivo(Pricing.INATIVO);

				pricingDiaList.removeItem(pricingDia);
				pricingDiaService.borrar(pricingDia);
				closeWindow();
			}
		} else {
			Messagebox.show(Labels.getLabel("editarPricingController.MSG.selectItem"), Labels.getLabel("editarPricingController.windowDia.title"), Messagebox.OK,
					Messagebox.EXCLAMATION);
		}
	}

	public PricingDia getPricingDia() {
		return pricingDia;
	}

	public void setPricingDia(PricingDia pricingDia) {
		this.pricingDia = pricingDia;
	}

	public MyListbox getPricingDiaList() {
		return pricingDiaList;
	}

	public void setPricingDiaList(MyListbox pricingDiaList) {
		this.pricingDiaList = pricingDiaList;
	}

	public PricingDiaService getPricingDiaService() {
		return pricingDiaService;
	}

	public void setPricingDiaService(PricingDiaService pricingDiaService) {
		this.pricingDiaService = pricingDiaService;
	}

	public Timebox getHorariofin() {
		return horariofin;
	}

	public void setHorariofin(Timebox horariofin) {
		this.horariofin = horariofin;
	}

	public Timebox getHorarioinicio() {
		return horarioinicio;
	}

	public void setHorarioinicio(Timebox horarioinicio) {
		this.horarioinicio = horarioinicio;
	}
}
