package be.msyben.vinylManager.tools;

import be.msyben.vinylManager.gui.list.albumList.AbstractAlbumList;
import be.msyben.vinylManager.properties.Preferences;
import be.msyben.vinylManager.tools.log.VinylManagerLogger;
import com.toedter.calendar.JDateChooser;
import java.awt.Font;
import java.lang.reflect.Field;
import javax.swing.*;

/**
 * Gestion des fonts de l'applicatin.
 *
 * @author Mathieu Syben
 */
public final class FontTool {

    public static Font font1 = null;
    public static Font font2 = null;

    private FontTool() {
    }

    private static boolean getFont() {
        String fontName = Preferences.getInstance().get(Preferences.POLICE);
        String fontSize = Preferences.getInstance().get(Preferences.POLICE_SIZE);
        if (fontName != null && !fontName.isEmpty() && fontSize != null && !fontSize.isEmpty()) {

            int fontSizeI;
            try {
                fontSizeI = Integer.parseInt(fontSize);
            } catch (NumberFormatException ex) {
                fontSizeI = 12;
            }

            font1 = new Font(fontName, Font.BOLD, fontSizeI);
            font2 = new Font(fontName, Font.PLAIN, fontSizeI);
            return true;
        }

        if ((fontName == null || fontName.isEmpty()) && (fontSize == null || fontSize.isEmpty())) {
            font1 = null;
            font2 = null;
            return true;
        }

        return false;
    }

    /**
     * Sette la police.
     *
     * @return true si changement, false sinon.
     */
    public static boolean setFont() {

        if (getFont()) {
            UIManager.put("Label.font", font1);
            UIManager.put("TextField.font", font2);
            UIManager.put("Menu.font", font1);
            UIManager.put("CheckBox.font", font1);
            UIManager.put("ComboBox.font", font1);
            UIManager.put("TextArea.font", font2);
            UIManager.put("RadioButton.font", font1);
            UIManager.put("Button.font", font1);
            UIManager.put("MenuItem.font", font1);
            UIManager.put("List.font", font2);
            UIManager.put("TabbedPane.font", font2);
            return true;
        }
        return false;
    }

    private static void resetFont(JComponent comp) {
        if (comp instanceof JTextField || comp instanceof JTextArea || comp instanceof JList || comp instanceof JTabbedPane || comp instanceof JDateChooser) {
            comp.setFont(font2);
        } else {
            comp.setFont(font1);
        }
    }

    /**
     * Met à jour la police d'un objet.
     *
     * @param instance l'objet dont il faut changer la police.
     */
    public static void refreshFont(Object instance) {
        //setFont();
        Field[] fields = instance.getClass().getDeclaredFields();
        for (Field f : fields) {
            boolean accessible = f.isAccessible();
            f.setAccessible(true);
            try {
                Object obj = f.get(instance);
                if (obj instanceof JComponent) {
                    resetFont((JComponent) obj);
                }
                if (obj instanceof AbstractAlbumList) {
                    AbstractAlbumList aal = (AbstractAlbumList) obj;
                    aal.setFont(font2);
                }
            } catch (IllegalArgumentException ex) {
                VinylManagerLogger.log(ex);
            } catch (IllegalAccessException ex) {
                VinylManagerLogger.log(ex);
            } finally {
                f.setAccessible(accessible);
            }
        }
    }
}
