package usda.weru.util;

import de.schlichtherle.io.File;
import java.awt.Frame;
import org.apache.log4j.Logger;
import usda.weru.util.gui.AboutDialog_n;

/**
 * A basic implementation of the JDialog class.
 */
public class AboutDialog extends AboutDialog_n {

    private static final Logger LOGGER = Logger.getLogger(AboutDialog.class);

    public AboutDialog(Frame parentFrame, usda.weru.util.Application app) {
        super(parentFrame);
        setTitle(app.getName() + " - About");
        g_messageLabel.setText(app.getDescription());
        StringBuffer buffer = new StringBuffer();
        //buffer.append("<html>");
        try {
            buffer.append("Product Version: " + app.getName() + " " + About.getVersion() + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        try {
            buffer.append("Product Release: " + About.getBuildRelease()+ "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        if (About.getBuildNumber() > 0) {
            try {
                buffer.append("Build: " + About.getBuildNumber() + "\n");
            } catch (Exception e) {
                LOGGER.warn("Error creating about dialog.", e);
            }
            try {
                buffer.append("Build Date: " + About.getBuildDate().toString() + "\n");
            } catch (Exception e) {
                LOGGER.warn("Error creating about dialog.", e);
            }
            try {
                buffer.append("Build Revsion: " + About.getBuildRevision() + "\n");
            } catch (Exception e) {
                LOGGER.warn("Error creating about dialog.", e);
            }
        } else {
            buffer.append("Build: dev\n");
            buffer.append("Build Date: \n");
            buffer.append("Build Revsion: \n");
        }
        try {
            buffer.append("Java: " + About.getJava() + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        try {
            buffer.append("System: " + About.getSystem() + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        try {
            buffer.append("Config: " + nonNullFilePath(About.getConfig()) + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        try {
            buffer.append("User Config: " + nonNullFilePath(About.getUserConfig()) + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        try {
            buffer.append("Log Config: " + nonNullFilePath(About.getLogConfig()) + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }
        try {
            buffer.append("Log: " + nonNullFilePath(About.getLog()) + "\n");
        } catch (Exception e) {
            LOGGER.warn("Error creating about dialog.", e);
        }

        //buffer.append("</html>");

        g_aboutTextArea.setText(buffer.toString());
    }

    private String nonNullFilePath(File file) {
        return file != null ? file.getAbsolutePath() : "";
    }
}

