/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.HashPrintServiceAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.PrintServiceAttributeSet;
import javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.PrinterName;
import static main.connection.TiposImpressao;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;

/**
 *
 * @author IAGO
 */
public class JavaPrinter 
{
    /**
     * @param args the command line arguments
     */
    
    public static void main(String[] args) 
    {
        TiposImpressao();
        
        if(connection.ImprimeSeparada.equals("N")) {
            ImprimeJunto(args[0]);
        }
        else {
            ImprimeSeparado(args[0]);
        }
    }    
    
    private static void ImprimeJunto(String hash)
    {
        try
        {
            System.out.println("Imprimindo Comanda Unica...");
            
            Connection conn1 = connection.GetConnection();
            
            Map map = new HashMap();
            map.put("HASH", hash);
            map.put("CON_ITENS", conn1);
            
            JasperPrint rel = JasperFillManager.fillReport("jasper/printer.jasper", map, connection.GetConnection());  
            int finalPag = rel.getPages().size() - 1;  
            JasperPrintManager.printPages(rel, 0 , finalPag ,false);
            
            System.out.println("Pronto!");
        }
        catch(JRException e)
        {
            System.out.println("Erro: "+e.toString());
            e.printStackTrace();
        }
    }
    
    private static void ImprimeSeparado(String hash) 
    {
        try
        {
            System.out.println("Imprimindo comanda bebidas...");
            ImprimeBebidas(hash);
            System.out.println("Pronto!");
            
            System.out.println("Imprimindo comanda comidas...");
            ImprimeComidas(hash);
            System.out.println("Pronto!");
        }
        catch(Exception e)
        {
            System.out.println("Erro: "+e.toString());
            e.printStackTrace();
        }
    }
    
    private static void ImprimeBebidas(String hash)
    {
        try
        {
            try(Connection conn = connection.GetConnection();
                PreparedStatement Pstmt = conn.prepareStatement("select PRODUTO_ID from PRODUTOS inner join PEDIDOS_BISTRO on PRODUTOS.PRODUTO_ID = PEDIDOS_BISTRO.PED_BIST_PRODUTO_ID "
                                                              + "where PED_BIST_HASH='"+hash+"' and PRODUTO_BEBIDA='S'");
                ResultSet rs = Pstmt.executeQuery())
            {
                if(rs.next())
                {
                    Connection conn1 = connection.GetConnection();
            
                    Map map = new HashMap();
                    map.put("HASH", hash);
                    map.put("CON_ITENS", conn1);

                    JasperPrint jp = JasperFillManager.fillReport("jasper/bebidas/printer.jasper", map, connection.GetConnection());  

                    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();  
                    printRequestAttributeSet.add(MediaSizeName.ISO_A4);  

                    PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();  
                    printServiceAttributeSet.add(new PrinterName(connection.ImpressoraBebidas, null));

                    JRPrintServiceExporter exporter = new JRPrintServiceExporter();  
                    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);  
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);  
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);  
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);  
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);  

                    exporter.exportReport();
                }
            }
        }
        catch(SQLException | JRException e)
        {
            System.out.println("Erro: "+e.toString());
            e.printStackTrace();
        }
    }
    
    private static void ImprimeComidas(String hash)
    {
        try
        {
            try(Connection conn = connection.GetConnection();
                PreparedStatement Pstmt = conn.prepareStatement("select PRODUTO_ID from PRODUTOS inner join PEDIDOS_BISTRO on PRODUTOS.PRODUTO_ID = PEDIDOS_BISTRO.PED_BIST_PRODUTO_ID "
                                                              + "where PED_BIST_HASH='"+hash+"' and PRODUTO_BEBIDA='N'");
                ResultSet rs = Pstmt.executeQuery())
            {
                if(rs.next())
                {
                    Connection conn1 = connection.GetConnection();

                    Map map = new HashMap();
                    map.put("HASH", hash);
                    map.put("CON_ITENS", conn1);

                    JasperPrint jp = JasperFillManager.fillReport("jasper/comidas/printer.jasper", map, connection.GetConnection());  

                    PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();  
                    printRequestAttributeSet.add(MediaSizeName.ISO_A4);  

                    PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();  
                    printServiceAttributeSet.add(new PrinterName(connection.ImpressoraComidas, null));

                    JRPrintServiceExporter exporter = new JRPrintServiceExporter();  
                    exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);  
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);  
                    exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);  
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);  
                    exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);  

                    exporter.exportReport();
                }
            }
        }
        catch(SQLException | JRException e)
        {
            System.out.println("Erro: "+e.toString());
            e.printStackTrace();
        }
    }
}