package com.arrebol.taxiservicios.beans;

import com.arrebol.taxiservicios.beans.admin.driver.DriverAddBean;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintException;
import javax.print.SimpleDoc;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 *
 * @author Dax
 */
public class TicketSocioEconomicStudy extends TaxiGenericBean {
    
    final Logger logger = LogManager.getLogger(DriverAddBean.class);

    private String contentTicket = "\n" + "::" + (char) 27 + (char) 112 + (char) 0 + (char) 10 + (char) 100
            + "               " + getFechaFormateada(new Date()) + "\n\n"
            + "ESTUDIO SOCIOECONOMICO E INGRESO\n"
            + "=============================\n"
            + "CHOFER:\n"
            + "{{driver}}\n"
            + "ESTUDIO SOCIOECONOMICO: ${{socioEconomicStudy}}\n"
            + "INGRESO: ${{amount}}"
            + "\n"
            + "=============================\n"
            + "       "+ getLoggedUser().getLocation().getBusinessName() +"\n"
            + "AGRADECE SU PREFERENCIA\n\n"
            + getLoggedUser().getLocation().getCompleteAddress()+"\n"
            + getLoggedUser().getLocation().getMunicipality() + ", "+ getLoggedUser().getLocation().getState() +".\n"
            + "RFC: "+ getLoggedUser().getLocation().getRfc() +"\n"
            + "TEL. "+ getLoggedUser().getLocation().getPhone() +"\n"
            + "\n\n"
            + "\n\n";

    ;

    public TicketSocioEconomicStudy(String driver, String socioEconomicStudy, String amount) {

        this.contentTicket = this.contentTicket.replace("{{driver}}", driver);
        this.contentTicket = this.contentTicket.replace("{{socioEconomicStudy}}", socioEconomicStudy);
        this.contentTicket = this.contentTicket.replace("{{amount}}", amount);
    }

    public void printTest() {
        System.out.print(contentTicket);
    }

    public void print() {
        try {

            byte[] bytes = contentTicket.getBytes();

            String printerName = getLoggedUser().getLocation().getPrinter();
            PrintService pintService;
            if (printerName != null) {
                pintService = findPrinter(printerName);
                DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
                DocPrintJob job = pintService.createPrintJob();

                Doc doc = new SimpleDoc(bytes, flavor, null);

                job.print(doc, null);
            } else {
                logger.error("Printer not found");
            }
        } catch (PrintException ex) {
            logger.error(ex);
        }
    }
    
    public PrintService findPrinter(String nombre) {
        logger.info("Find printer");

        PrintService service = null;

        PrintService[] services = PrinterJob.lookupPrintServices();

        for (int i = 0; service == null && i < services.length; i++) {
            if (services[i].getName().contains(nombre)) {
                service = services[i];
            }
        }

        return service;
    }
    
    public static String getFechaFormateada(Date fecha) {
        SimpleDateFormat formato = new SimpleDateFormat("yyyy-MM-dd");
        return formato.format((Date) fecha);
    }
    
}
