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

import com.zakscode.FormulaManager.Component;
import com.zakscode.FormulaManager.Configuration;
import com.zakscode.FormulaManager.FileManager;
import com.zakscode.FormulaManager.Formula;
import com.zakscode.FormulaManager.Frame;
import com.zakscode.FormulaManager.Logger;
import com.zakscode.FormulaManager.Vendor;
import java.awt.EventQueue;
import java.awt.Image;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

class Main {
    public static String baseDir;
    public static Configuration conf;
    public static FileManager<Vendor> vendors;
    public static FileManager<Vendor> userVendors;
    public static FileManager<Component> components;
    public static FileManager<Component> userComponents;
    public static FileManager<Formula> formulas;
    public static FileManager<Formula> userFormulas;
    private static int windowCount;
    public static boolean metric;
    public static boolean largeUnits;
    public static ImageIcon icon;

    Main() {
    }

    public static void main(String[] args) {
        baseDir = new File(" ").getAbsolutePath().trim();
        icon = new ImageIcon(baseDir + "icon.png");
        conf = new Configuration();
        JFrame splash = new JFrame("Formula Manager");
        splash.setIconImage(icon.getImage());
        splash.setUndecorated(true);
        splash.add(new JLabel(new ImageIcon(baseDir + "splash.png")));
        splash.pack();
        splash.setLocationRelativeTo(null);
        splash.setVisible(true);
        File dataLocation = new File(baseDir + conf.getProperty("data-path"));
        if (!dataLocation.exists()) {
            dataLocation.mkdir();
        }
        if (!(dataLocation = new File(baseDir + conf.getProperty("temp-path"))).exists()) {
            dataLocation.mkdir();
        }
        metric = Boolean.parseBoolean(conf.getProperty("metric"));
        largeUnits = Boolean.parseBoolean(conf.getProperty("large-units"));
        windowCount = 0;
        Main.downloadData(true, false);
        userVendors = new FileManager("userVendors.data");
        userVendors.load();
        userComponents = new FileManager("userComponents.data");
        userComponents.load();
        userFormulas = new FileManager("userFormulas.data");
        userFormulas.load();
        EventQueue.invokeLater(Main::newWindow);
        splash.dispose();
    }

    public static void newWindow() {
        ++windowCount;
        new Frame(Integer.parseInt(conf.getProperty("resolution-x")), Integer.parseInt(conf.getProperty("resolution-y")));
    }

    public static void close(JFrame frame) {
        if (--windowCount <= 0) {
            conf.setProperty("metric", String.valueOf(metric));
            conf.setProperty("large-units", String.valueOf(largeUnits));
            conf.setProperty("resolution-x", String.valueOf(frame.getWidth()));
            conf.setProperty("resolution-y", String.valueOf(frame.getHeight()));
            conf.save();
            System.exit(0);
        }
        frame.dispose();
    }

    public static void downloadData(boolean suppress, boolean overwrite) {
        long date;
        ReadableByteChannel rbc;
        FileOutputStream fos;
        HttpURLConnection httpCon;
        boolean success = true;
        try {
            URL vendorURL = new URL("http://" + conf.getProperty("URL") + conf.getProperty("data-URL") + "vendors.data");
            httpCon = (HttpURLConnection)vendorURL.openConnection();
            date = httpCon.getLastModified();
            if (date > new File(baseDir + conf.getProperty("data-path") + "vendors.data").lastModified() || overwrite) {
                fos = new FileOutputStream(baseDir + conf.getProperty("data-path") + "vendors.data");
                rbc = Channels.newChannel(vendorURL.openStream());
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                rbc.close();
                fos.close();
            }
        }
        catch (IOException e) {
            success = false;
            Logger.write(e);
        }
        vendors = new FileManager("vendors.data");
        vendors.load();
        try {
            URL componentURL = new URL("http://" + conf.getProperty("URL") + conf.getProperty("data-URL") + "components.data");
            httpCon = (HttpURLConnection)componentURL.openConnection();
            date = httpCon.getLastModified();
            if (date > new File(baseDir + conf.getProperty("data-path") + "components.data").lastModified() || overwrite) {
                fos = new FileOutputStream(baseDir + conf.getProperty("data-path") + "components.data");
                rbc = Channels.newChannel(componentURL.openStream());
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                rbc.close();
                fos.close();
            }
        }
        catch (IOException e) {
            success = false;
            Logger.write(e);
        }
        components = new FileManager("components.data");
        components.load();
        try {
            URL formulaURL = new URL("http://" + conf.getProperty("URL") + conf.getProperty("data-URL") + "formulas.data");
            httpCon = (HttpURLConnection)formulaURL.openConnection();
            date = httpCon.getLastModified();
            if (date > new File(baseDir + conf.getProperty("data-path") + "formulas.data").lastModified() || overwrite) {
                fos = new FileOutputStream(baseDir + conf.getProperty("data-path") + "formulas.data");
                rbc = Channels.newChannel(formulaURL.openStream());
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                rbc.close();
                fos.close();
            }
        }
        catch (IOException e) {
            success = false;
            Logger.write(e);
        }
        formulas = new FileManager("formulas.data");
        formulas.load();
        if (!suppress) {
            if (success) {
                JOptionPane.showMessageDialog(null, "Update completed successfully", "Update", 1);
            } else {
                JOptionPane.showMessageDialog(null, "Update Failed\nCheck log for additional information", "Update", 0);
            }
        }
    }

    public static void saveAll() {
        vendors.save();
        userVendors.save();
        components.save();
        userComponents.save();
        formulas.save();
        userFormulas.save();
    }
}

