/*
 * Decompiled with CFR 0_123.
 */
package com.zakscode.FormulaManager;

import com.zakscode.FormulaManager.Component;
import com.zakscode.FormulaManager.ComponentQuantity;
import com.zakscode.FormulaManager.Configuration;
import com.zakscode.FormulaManager.FileManager;
import com.zakscode.FormulaManager.Formula;
import com.zakscode.FormulaManager.Logger;
import com.zakscode.FormulaManager.Main;
import com.zakscode.FormulaManager.Vendor;
import java.io.File;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Set;
import org.apache.pdfbox.pdmodel.PDDocument;

class Export {
    private static final String HEADER = "<!DOCTYPE html><html><body><center><h1>FH &#38; Sons</h1></center>";
    private static final String FOOTER = "</body></html>";

    Export() {
    }

    public static File vendorsToHTML(FileManager<Vendor> v) {
        try {
            File doc = new File(Main.baseDir + Main.conf.getProperty("temp-path") + "Vendors.html");
            PrintWriter pw = new PrintWriter(doc);
            String html = "<!DOCTYPE html><html><body><center><h1>FH &#38; Sons</h1></center>";
            Set keys = v.keySet();
            Object[] keysArray = keys.toArray(new String[keys.size()]);
            Arrays.sort(keysArray);
            boolean floatLeft = true;
            for (Object key : keysArray) {
                html = html + (floatLeft ? "<div style=\"overflow: hidden;\">" : "") + "<div style=\"float: " + (floatLeft ? "left; " : "right; ") + "width: 45%; overflow: hidden; padding: 0px 20px 0px 20px\">";
                html = html + "<p><strong style=\"float: left\">Name: </strong>" + (String)key + "</p>";
                html = html + (key.trim().equals("") ? "<br />" : "");
                html = html + "<p><strong style=\"float: left\">Phone: </strong> " + ((Vendor)v.get(key)).getPhone() + "</p>";
                html = html + (((Vendor)v.get(key)).getPhone().trim().equals("") ? "<br />" : "");
                html = html + "<p><strong style=\"float: left\">Email: </strong> " + ((Vendor)v.get(key)).getEmail() + "</p>";
                html = html + (((Vendor)v.get(key)).getEmail().trim().equals("") ? "<br />" : "");
                html = html + "<p><strong style=\"float: left\">Website: </strong> " + ((Vendor)v.get(key)).getWebsite() + "</p>";
                html = html + "<br /></div>" + (floatLeft ? "" : "</div><br />");
                floatLeft = !floatLeft;
            }
            if (keysArray.length % 2 == 1) {
                html = html + "</div>";
            }
            html = html + "</body></html>";
            pw.write(html);
            pw.close();
            return doc;
        }
        catch (Exception e) {
            Logger.write(e);
            return null;
        }
    }

    public static File componentsToHTML(FileManager<Component> c) {
        try {
            File doc = new File(Main.baseDir + Main.conf.getProperty("temp-path") + "Components.html");
            PrintWriter pw = new PrintWriter(doc);
            String html = "<!DOCTYPE html><html><body><center><h1>FH &#38; Sons</h1></center>";
            Set keys = c.keySet();
            Object[] keysArray = keys.toArray(new String[keys.size()]);
            Arrays.sort(keysArray);
            boolean floatLeft = true;
            for (Object key : keysArray) {
                html = html + (floatLeft ? "<div style=\"overflow: hidden;\">" : "") + "<div style=\"float: " + (floatLeft ? "left; " : "right; ") + "width: 45%; overflow: hidden; padding: 0px 20px 0px 20px\">";
                html = html + "<p><strong style=\"float: left\">Name:</strong> " + (String)key + "</p>";
                html = html + (key.trim().equals("") ? "<br />" : "");
                html = html + "<p><strong style=\"float: left\">Vendor:</strong> " + ((Component)c.get(key)).getVendor() + "</p>";
                html = html + (((Component)c.get(key)).getVendor().trim().equals("") ? "<br />" : "");
                html = html + "<p><strong style=\"float: left\">Description:</strong> " + ((Component)c.get(key)).getDescription() + "</p>";
                html = html + (((Component)c.get(key)).getDescription().trim().equals("") ? "<br />" : "");
                html = html + "<p><strong style=\"float: left\">Cost (per " + (Main.metric ? "kg" : "lb") + "): </strong> " + new DecimalFormat("$0.00").format(((Component)c.get(key)).getCost() / (double)(Main.metric ? true : true) ? 1 : 0) + "</p>";
                html = html + "</div>" + (floatLeft ? "" : "</div><br />");
                floatLeft = !floatLeft;
            }
            if (keysArray.length % 2 == 1) {
                html = html + "</div>";
            }
            html = html + "</body></html>";
            pw.write(html);
            pw.close();
            return doc;
        }
        catch (Exception e) {
            Logger.write(e);
            return null;
        }
    }

    public static File formulasToHTML(FileManager<Formula> f) {
        try {
            File doc = new File(Main.baseDir + Main.conf.getProperty("temp-path") + "Formulas.html");
            PrintWriter pw = new PrintWriter(doc);
            String html = "<!DOCTYPE html><html><body><center><h1>FH &#38; Sons</h1></center>";
            Set keys = f.keySet();
            Object[] keysArray = keys.toArray(new String[keys.size()]);
            Arrays.sort(keysArray);
            boolean floatLeft = true;
            for (Object key : keysArray) {
                html = html + (floatLeft ? "<div style=\"overflow: hidden;\">" : "") + "<div style=\"float: " + (floatLeft ? "left; " : "right; ") + "width: 45%; overflow: hidden; padding: 0px 20px 0px 20px\">";
                html = html + "<p><strong style=\"float: left\">" + (String)key + "</strong></p> <strong style=\"float: right\">" + ((Formula)f.get(key)).getDescription() + "</strong>";
                double total = 0.0;
                for (ComponentQuantity cq : (Formula)f.get(key)) {
                    total += cq.getQuantity();
                    html = html + "<br />";
                    html = html + "<p style=\"float: left; margin: 0px 0px 0px 0px\">" + cq.getComponent() + "</p><p style=\"float: right; margin: 0px 0px 0px 0px\">" + new DecimalFormat("0.00").format(cq.getQuantity()) + (Main.metric ? " g" : " oz") + "</p>";
                }
                html = html + "<br /><hr></hr>";
                html = html + "<p style=\"float: right; margin: 0px 0px 0px 0px\"><strong>Total: </strong>" + new DecimalFormat("0.00").format(total) + (Main.metric ? " g" : " oz") + "</p>";
                html = html + "<br /><hr></hr></div>" + (floatLeft ? "" : "</div><br />");
                floatLeft = !floatLeft;
            }
            if (keysArray.length % 2 == 1) {
                html = html + "</div>";
            }
            html = html + "</body></html>";
            pw.write(html);
            pw.close();
            return doc;
        }
        catch (Exception e) {
            Logger.write(e);
            return null;
        }
    }

    public static File HTMLToPDF(File in) {
        try {
            File out = new File(in.toString().replace(".html", ".pdf"));
            Runtime rt = Runtime.getRuntime();
            Process pr = rt.exec("\"" + Main.baseDir + "resources/wkhtmltopdf\" \"" + in.getAbsoluteFile() + "\" \"" + out.getAbsolutePath() + "\"");
            pr.waitFor();
            return out;
        }
        catch (Exception e) {
            Logger.write(e);
            return null;
        }
    }

    public static boolean printPDF(File file) {
        try {
            PDDocument doc = PDDocument.load(file);
            doc.print();
            return true;
        }
        catch (Exception e) {
            Logger.write(e);
            return false;
        }
    }
}

