package com.rjconsultores.ventaboletos.web.gui.controladores.impressaofiscal;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.List;

import javax.sql.DataSource;

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.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Button;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Filedownload;

import com.rjconsultores.ventaboletos.entidad.Empresa;
import com.rjconsultores.ventaboletos.service.EmpresaService;
import com.rjconsultores.ventaboletos.service.FiscalService;
import com.rjconsultores.ventaboletos.utilerias.DateUtil;
import com.rjconsultores.ventaboletos.web.utilerias.MyComboboxEstandar;
import com.rjconsultores.ventaboletos.web.utilerias.MyGenericForwardComposer;

@Controller("busquedaImportacionFiscalController")
@Scope("prototype")
public class BusquedaImportacionFiscalController extends MyGenericForwardComposer {

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

	@Autowired
	private DataSource dataSourceRead;
	@Autowired
	private FiscalService fiscalService;
	@Autowired
	private EmpresaService empresaService;
	private List<Empresa> lsEmpresa;
	private Datebox datInicial;
	private Datebox datFinal;
	private MyComboboxEstandar cmbEmpresa;

	private Button btnExeImportacionEcf;
	private Button btnExeImportacionEcfCancelados;
	private Button btnExeImportacionReducaoZ;
	private Button btnExeImportacionManual;
	private Button btnExeImportacionNaoFiscal;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		lsEmpresa = empresaService.obtenerTodos();
		super.doAfterCompose(comp);

		defineImportacao();
	}

	private void defineImportacao() {

		boolean isEcf = (Boolean) Executions.getCurrent().getArg().get("ECF");
		if (isEcf) {
			btnExeImportacionEcf.setVisible(true);
		} else {
			btnExeImportacionEcf.setVisible(false);
		}

		boolean isEcfCancel = (Boolean) Executions.getCurrent().getArg().get("ECF_CANCELADOS");
		if (isEcfCancel) {
			btnExeImportacionEcfCancelados.setVisible(true);
		} else {
			btnExeImportacionEcfCancelados.setVisible(false);
		}

		boolean isManual = (Boolean) Executions.getCurrent().getArg().get("MANUAL");
		if (isManual) {
			btnExeImportacionManual.setVisible(true);
		} else {
			btnExeImportacionManual.setVisible(false);
		}

		boolean isReducaoZ = (Boolean) Executions.getCurrent().getArg().get("REDUCAO_Z");
		if (isReducaoZ) {
			btnExeImportacionReducaoZ.setVisible(true);
		} else {
			btnExeImportacionReducaoZ.setVisible(false);
		}

		boolean isNaoFiscal = (Boolean) Executions.getCurrent().getArg().get("NAO_FISCAL");
		if (isNaoFiscal) {
			btnExeImportacionNaoFiscal.setVisible(true);
		} else {
			btnExeImportacionNaoFiscal.setVisible(false);
		}
	}

	public void onClick$btnExeImportacionManual(Event ev) throws InterruptedException {

		Empresa empresa = null;
		Comboitem itemEmpresa = cmbEmpresa.getSelectedItem();
		if (itemEmpresa != null) {
			empresa = (Empresa) itemEmpresa.getValue();
		}

		try {
			File arquivo = fiscalService.importacionFiscalECFManual(datInicial.getValue(), datFinal.getValue(), empresa);
			InputStream is = new FileInputStream(arquivo);
			Filedownload.save(is, "application/notepad", "fiscal_manual_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmm") + ".txt");
			arquivo.delete();
		} catch (FileNotFoundException e) {
			log.error("", e);
		}
	}
	
	public void onClick$btnExeImportacionEcfCancelados(Event ev) throws InterruptedException {

		Empresa empresa = null;
		Comboitem itemEmpresa = cmbEmpresa.getSelectedItem();
		if (itemEmpresa != null) {
			empresa = (Empresa) itemEmpresa.getValue();
		}

		try {
			File arquivo = fiscalService.importacionFiscalECFCancelados(datInicial.getValue(), datFinal.getValue(), empresa, dataSourceRead.getConnection());
			InputStream is = new FileInputStream(arquivo);
			Filedownload.save(is, "application/notepad", "fiscal_cancel_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmm") + ".txt");
			arquivo.delete();
		} catch (FileNotFoundException e) {
			log.error("", e);
		} catch (WrongValueException e) {
			log.error("", e);
		} catch (SQLException e) {
			log.error("", e);
		}
	}


	public void onClick$btnExeImportacionEcf(Event ev) throws InterruptedException {

		Empresa empresa = null;
		Comboitem itemEmpresa = cmbEmpresa.getSelectedItem();
		if (itemEmpresa != null) {
			empresa = (Empresa) itemEmpresa.getValue();
		}

		try {
			File arquivo = fiscalService.importacionFiscalECF(datInicial.getValue(), datFinal.getValue(), empresa, dataSourceRead.getConnection());
			InputStream is = new FileInputStream(arquivo);
			Filedownload.save(is, "application/notepad", "fiscal_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmm") + ".txt");
			arquivo.delete();
		} catch (FileNotFoundException e) {
			log.error("", e);
		} catch (WrongValueException e) {
			log.error("", e);
		} catch (SQLException e) {
			log.error("", e);
		}
	}

	public void onClick$btnExeImportacionReducaoZ(Event ev) throws InterruptedException {

		Empresa empresa = null;
		Comboitem itemEmpresa = cmbEmpresa.getSelectedItem();
		if (itemEmpresa != null) {
			empresa = (Empresa) itemEmpresa.getValue();
		}

		try {
			File arquivo = fiscalService.importacionFiscalReducaoZ(datInicial.getValue(), datFinal.getValue(), empresa);
			InputStream is = new FileInputStream(arquivo);
			Filedownload.save(is, "application/notepad", "fiscal_reducao_z" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmm") + ".txt");
			arquivo.delete();
		} catch (FileNotFoundException e) {
			log.error("", e);
		}

	}

	public void onClick$btnExeImportacionNaoFiscal(Event ev) throws InterruptedException {
		try {
			Empresa empresa = null;
			Comboitem itemEmpresa = cmbEmpresa.getSelectedItem();
			if (itemEmpresa != null) {
				empresa = (Empresa) itemEmpresa.getValue();
			}

			File arquivo = fiscalService.importacionNaoFiscal(datInicial.getValue(), datFinal.getValue(), empresa);
			InputStream is = new FileInputStream(arquivo);
			Filedownload.save(is, "application/notepad", "nao_fiscal_" + DateUtil.getStringDate(Calendar.getInstance().getTime(), "yyyyMMddhhmm") + ".txt");
			arquivo.delete();
		} catch (FileNotFoundException e) {
			log.error(e.getMessage(), e);
		}
	}

	public List<Empresa> getLsEmpresa() {
		return lsEmpresa;
	}

}
