/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.arrebol.taxiservicios.beans;

import com.arrebol.taxiservicios.beans.admin.driver.DriverAddBean;
import java.awt.print.PrinterJob;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.SimpleDoc;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author pc one
 */
public class TicketSale 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"
            + "VENTA\n"
            + "=============================\n"
            + "CHOFER:\n"
            + "{{driver}}\n"
            + "PAGO DIA: {{pagoDia}}\n"
            + "AHORRO: {{ahorro}}\n"
            + "MUTUAL: {{mutual}}"
            + "\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 TicketSale(String nameDriver, String pagoDia, String ahorro, String mutual) {
        this.contentTicket = this.contentTicket.replace("{{driver}}", nameDriver);
        this.contentTicket = this.contentTicket.replace("{{pagoDia}}", pagoDia);
        this.contentTicket = this.contentTicket.replace("{{ahorro}}", ahorro);
        this.contentTicket = this.contentTicket.replace("{{mutual}}", mutual);
    }

    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);
    }
}
