package me.teridax.jcash.gui;

import me.teridax.jcash.Main;
import me.teridax.jcash.lang.Locales;

import javax.swing.*;
import java.awt.*;
import java.text.Format;
import java.text.NumberFormat;

public class Utils {

    public static String addHeading(String title) {
        return String.format("<html><h1>%s</h1></html>", title);
    }

    /**
     * Add a new row of components to the specified target component.
     * This will add label to the right side of the next row and the specified component to the left.
     *
     * @param constraints the constraint to use. Must be non-null
     * @param target      the base component to add a row to
     * @param comp        the component to add to the left side
     * @param row         the row to add the components to
     * @param name        the labels text to add to the left side
     */
    public static void addGridBagRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, String name) {
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = row;
        constraints.weightx = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        target.add(new JLabel(name, SwingConstants.RIGHT), constraints);

        if (comp == null)
            return;

        constraints.gridx = 2;
        constraints.gridy = row;
        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        target.add(comp, constraints);
    }

    /**
     * Add a new row of components to the specified target component.
     * This will add label to the right side of the next row and the specified component to the left.
     *
     * @param constraints the constraint to use. Must be non-null
     * @param target      the base component to add a row to
     * @param comp        the component to add to the left side
     * @param row         the row to add the components to
     * @param right       the component to place on the left side
     */
    public static void addGridBagRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, Component right) {
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = row;
        constraints.weightx = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        target.add(right, constraints);

        constraints.gridx = 2;
        constraints.gridy = row;
        constraints.weightx = 1;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        target.add(comp, constraints);
    }

    /**
     * Opens an error message dialog. This function will block the calling thread until the error message
     * disposed.
     * @param message the message to show to the user
     */
    public static void error(String message) {
        JOptionPane.showMessageDialog(Main.getInstance().getWindow(), message, "Error occurred", JOptionPane.ERROR_MESSAGE);
    }
}
