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

import org.apache.commons.lang.StringUtils;
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.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Button;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

import com.rjconsultores.ventaboletos.constantes.Constantes;
import com.rjconsultores.ventaboletos.entidad.Constante;
import com.rjconsultores.ventaboletos.enums.FuncionSistemaEnum;
import com.rjconsultores.ventaboletos.service.ConstanteService;
import com.rjconsultores.ventaboletos.service.UsuarioPerfilService;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;

/**
 * @author valdevir_rj
 *
 */
@Controller("editarPainelVendaEmbarcadaController")
@Scope("prototype")
public class EditarPainelVendaEmbarcadaController extends MyGenericForwardComposer {
	private static final String HTTPS = "https://";
	private static final String HTTP = "http://";
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String enderecoUrl;
	private Button btnIr;
	private Button btnEditar;
	private Textbox txtEnderecoUrl;
	@Autowired
	private ConstanteService constanteService;
	@Autowired
	private UsuarioPerfilService usuarioPerfilService;
	private Constante constante;
	private boolean isPermiteEditar;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		isPermiteEditar = usuarioPerfilService.validaPermissaoPerfilEdicao(FuncionSistemaEnum.PAINEL_VENDA_EMBARCADA.getValor());
		constante = constanteService.buscarPorNomeConstante(Constantes.URL_PAINEL_EMBARCADA);
		enderecoUrl = constante.getValorconstante();
		if (StringUtils.isEmpty(enderecoUrl)) {
			btnIr.setDisabled(true);
		}
		if(!isPermiteEditar) {
			txtEnderecoUrl.setDisabled(true);
			btnEditar.setDisabled(true);
		}
	}

	public void onClick$btnEditar(Event ev) throws InterruptedException {
		if (isPermiteEditar) {
			if (StringUtils.isNotEmpty(txtEnderecoUrl.getText()) ) {
				int resp = -1;
				resp = Messagebox.show(
						Labels.getLabel("painelVendaEmbarcadaController.msg.confirmacaoaltecaourl"),
						Labels.getLabel("painelVendaEmbarcadaController.window.title"),
						Messagebox.YES | Messagebox.NO, Messagebox.QUESTION);
				if (resp == Messagebox.YES) {
					if (constante != null && validaUrl(txtEnderecoUrl.getText())) {
						constante.setValorconstante(txtEnderecoUrl.getText());
						constanteService.actualizacion(constante);
						Messagebox.show(Labels.getLabel("painelVendaEmbarcadaController.msg.scusso"),
								Labels.getLabel("painelVendaEmbarcadaController.window.title"),
								Messagebox.OK, Messagebox.INFORMATION);
					} else {
						Messagebox.show(Labels.getLabel("painelVendaEmbarcadaController.msg.urlformatoinvalido"),
								Labels.getLabel("painelVendaEmbarcadaController.window.title"),
								Messagebox.OK, Messagebox.INFORMATION);
					}
				}

			}
		} else {
			Messagebox.show(Labels.getLabel("painelVendaEmbarcadaController.msg.usuariosempermisao"),
					Labels.getLabel("painelVendaEmbarcadaController.window.title"),
					Messagebox.OK, Messagebox.INFORMATION);
		}


	}

	
	public void onClick$btnIr(Event ev) {
		Executions.getCurrent().sendRedirect(enderecoUrl, "_new");
	}

	public String getEnderecoUrl() {
		return enderecoUrl;
	}

	public void setEnderecoUrl(String enderecoUrl) {
		this.enderecoUrl = enderecoUrl;
	}

	private boolean validaUrl(String url) {
		if (StringUtils.isNotEmpty(url)) {
			if (url.toLowerCase().startsWith(HTTP) || url.toLowerCase().startsWith(HTTPS)) {
				return true;
			}
		}
		return false;
	}

}
