package com.mx.dla.dda.reportes.generaxml;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.mx.dla.dda.reporte.general.dtos.ExcelEstilosConfDTO;
import com.mx.dla.global.bos.BaseBO;

@Service
public class ExcelXmlConfiguracionWriter extends BaseBO {

	@Value("${excel.process.source.path}")
	private String fileRoute;

	public void writeToXml(String fileName, List<LinkedHashMap<String, Object>> header, List<LinkedHashMap<String, Object>> tabla, List<ExcelEstilosConfDTO> estilos, String tituloReporte)
	        throws IOException, XMLStreamException {

		Path path = Paths.get(fileRoute + "/" + fileName);

		logger.info(fileRoute + "/" + fileName);

		try (OutputStream os = Files.newOutputStream(path)) {
			XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
			XMLStreamWriter writer = null;
			try {
				writer = outputFactory.createXMLStreamWriter(os, "utf-8");
				createXml(writer, header, tabla, estilos, tituloReporte);
			}
			finally {
				if (writer != null)
					writer.close();
			}
		}
	}

	private void createXml(XMLStreamWriter writer, List<LinkedHashMap<String, Object>> header, List<LinkedHashMap<String, Object>> tabla, List<ExcelEstilosConfDTO> estilos, String tituloReporte)
	        throws XMLStreamException {

		writer.writeStartDocument("utf-8", "1.0");
		writer.writeComment("Se crea configuracion para archivo xls");
		writer.writeStartElement("Archivo");
		writer.writeStartElement("Tabla");
		writer.writeAttribute("id", "Hoja 1");

		tiuloRow(writer, tituloReporte);

		logger.info("Empieza creacion");
		logger.info("{}", estilos);

		List<LinkedHashMap<String, Object>> reporte = new ArrayList<LinkedHashMap<String, Object>>(header);
		reporte.addAll(tabla);

		long x = 0;
		for (LinkedHashMap<String, Object> row : reporte) {

			writer.writeStartElement("Fila");
			writer.writeAttribute("id", "");
			writer.writeAttribute("visible", "true");

			long y = 0;

			for (Map.Entry<String, Object> column : row.entrySet()) {

				ExcelEstilosConfDTO aplica = null;

				logger.info("{}", column);
				logger.info("{} {}", x, y);

				for (ExcelEstilosConfDTO estilo : estilos) {
					if (estilo.find(x, y)) {
						aplica = estilo;
						break;
					}
				}

				logger.info("{}", aplica);

				aplica.setText(column.getValue() == null ? null : column.getValue().toString());

				logger.info("{}", aplica.getText());

				writer.writeStartElement("Celda");

				writer.writeStartElement("width");
				writer.writeCharacters(aplica.getWidth());
				writer.writeEndElement();

				writer.writeStartElement("height");
				writer.writeCharacters(aplica.getHeight());
				writer.writeEndElement();

				writer.writeStartElement("colspan");
				writer.writeCharacters(aplica.getColspan());
				writer.writeEndElement();

				writer.writeStartElement("rowspan");
				writer.writeCharacters(aplica.getRowspan());
				writer.writeEndElement();

				writer.writeStartElement("align");
				writer.writeCharacters(aplica.getAlign());
				writer.writeEndElement();

				writer.writeStartElement("valign");
				writer.writeCharacters(aplica.getValign());
				writer.writeEndElement();

				writer.writeStartElement("cellClass");
				writer.writeCharacters(aplica.getCellClass());
				writer.writeEndElement();

				writer.writeStartElement("value");
				writer.writeCharacters(aplica.getValue());
				writer.writeEndElement();

				writer.writeStartElement("text");
				writer.writeCharacters(aplica.getText());
				writer.writeEndElement();

				writer.writeStartElement("datatype");
				writer.writeCharacters(aplica.getDatatype());
				writer.writeEndElement();

				writer.writeEndElement();

				y++;

			}

			writer.writeEndElement();
			x++;

		}

		writer.writeEndElement();
		writer.writeEndElement();

		writer.writeEndDocument();

		logger.info("Acaba creacion");

	}

	private void tiuloRow(XMLStreamWriter writer, String titulo) throws XMLStreamException {

		writer.writeStartElement("Fila");
		writer.writeAttribute("id", "");
		writer.writeAttribute("visible", "true");
		writer.writeStartElement("Celda");

		writer.writeStartElement("width");
		writer.writeCharacters(null);
		writer.writeEndElement();

		writer.writeStartElement("height");
		writer.writeCharacters(String.valueOf(0));
		writer.writeEndElement();

		writer.writeStartElement("colspan");
		writer.writeCharacters(String.valueOf(13));
		writer.writeEndElement();

		writer.writeStartElement("rowspan");
		writer.writeCharacters(String.valueOf(1));
		writer.writeEndElement();

		writer.writeStartElement("align");
		writer.writeCharacters("center");
		writer.writeEndElement();

		writer.writeStartElement("valign");
		writer.writeCharacters("middle");
		writer.writeEndElement();

		writer.writeStartElement("cellClass");
		writer.writeCharacters("titulo_modulo");
		writer.writeEndElement();

		writer.writeStartElement("value");
		writer.writeCharacters(titulo);
		writer.writeEndElement();

		writer.writeStartElement("text");
		writer.writeCharacters(titulo);
		writer.writeEndElement();

		writer.writeStartElement("datatype");
		writer.writeCharacters("string");
		writer.writeEndElement();
		
	
		writer.writeEndElement();
		writer.writeEndElement();


	}

}