/*
 * Copyright (c) 2001-2014 JGoodies Software GmbH. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  o Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  o Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 *  o Neither the name of JGoodies Software GmbH nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.jgoodies.looks;

import static com.jgoodies.common.base.Preconditions.checkNotNull;

import java.awt.Font;
import java.lang.reflect.Method;

import javax.swing.UIDefaults;
import javax.swing.plaf.FontUIResource;

import com.jgoodies.common.base.SystemUtils;
import com.jgoodies.common.internal.Messages;
import com.jgoodies.looks.FontSets.DefaultFontSet;


/**
 * Provides predefined FontPolicy implementations.<p>
 *
 * <strong>Note:</strong> The available policies work well on Windows.
 * On other platforms the fonts specified by the runtime environment
 * are chosen. I plan to provide more logic or options for other platforms,
 * for example that a Linux system checks for a Tahoma or Segoe UI.<p>
 *
 * TODO: Add a check for a custom font policy set in the System properties.<p>
 *
 * TODO: Add policies that emulate different Windows setups:
 * default XP on 96dpi with normal fonts ("XP-normal-96"),
 * Vista on 120dpi with large fonts ("Vista-large-120"), etc.
 *
 * @author  Karsten Lentzsch
 * @version $Revision: 1.14 $
 *
 * @see     FontPolicy
 * @see     FontSet
 * @see     FontSets
 * @see     Fonts
 *
 * @since 2.0
 */
public final class FontPolicies {


    private FontPolicies() {
        // Override default constructor; prevents instantation.
    }


    // Getting a FontPolicy *********************************************

    /**
     * Returns a font policy that in turn always returns the specified FontSet.
     * The FontSet will be fixed, but the FontSet itself may
     * return different fonts in different environments.
     *
     * @param fontSet   the FontSet to be return by this policy
     * @return a font policy that returns the specified FontSet.
     */
    public static FontPolicy createFixedPolicy(FontSet fontSet) {
        return new FixedPolicy(fontSet);
    }


    /**
     * Returns a font policy that checks for a custom FontPolicy
     * and a custom FontSet specified in the System settings or UIManager.
     * If no custom settings are available, the given default policy will
     * be used to look up the FontSet.
     *
     * @param defaultPolicy   the policy used if there are no custom settings
     * @return a FontPolicy that checks for custom settings
     *     before the default policy is returned.
     */
    public static FontPolicy customSettingsPolicy(FontPolicy defaultPolicy) {
        return new CustomSettingsPolicy(defaultPolicy);
    }


//    /**
//     * Returns the default platform independent font choice policy.<p>
//     *
//     * The current implementation just returns the logical fonts.
//     * A future version shall check for available good fonts
//     * and shall use them before it falls back to the logical fonts.
//     *
//     * @return the default platform independent font choice policy.
//     */
//    public static FontPolicy getDefaultCrossPlatformPolicy() {
//        return new DefaultCrossPlatformPolicy();
//    }


    /**
     * Returns the default font policy for Plastic on the Windows platform.
     * It differs from the default Windows policy in that it uses a bold font
     * for TitledBorders, titles, and titled separators.
     *
     * @return the default font policy for Plastic on the Windows platform.
     */
    public static FontPolicy getDefaultPlasticOnWindowsPolicy() {
        return new DefaultPlasticOnWindowsPolicy();
    }


    /**
     * Returns the default Plastic FontPolicy that may vary
     * with the platform and environment.
     * On Windows, the PlasticOnWindowsPolicy is returned that
     * is much like the defualt WindowsPolicy but uses a bold title font.
     * On other Platforms, the logical fonts policy is returned
     * that uses the logical fonts as specified by the Java runtime environment.
     *
     * @return a Windows-like policy on Windows, a logical fonts policy
     *    on all other platforms
     */
    public static FontPolicy getDefaultPlasticPolicy() {
        if (SystemUtils.IS_OS_WINDOWS) {
            return getDefaultPlasticOnWindowsPolicy();
        }
        return getLogicalFontsPolicy();
//      return getDefaultCrossPlatformPolicy();
    }


    /**
     * Returns the default font policy for the Windows platform.
     * It aims to return a FontSet that is close to the native guidelines
     * and useful for the current Java environment.<p>
     *
     * The control font scales with the platform screen resolution
     * (96dpi/101dpi/120dpi/144dpi/...) and honors the desktop font settings
     * (normal/large/extra large).
     *
     * @return the default font policy for the Windows platform.
     */
    public static FontPolicy getDefaultWindowsPolicy() {
        return new DefaultWindowsPolicy();
    }


    /**
     * Returns a font policy that returns the logical fonts
     * as specified by the Java runtime environment.
     *
     * @return a font policy that returns logical fonts.
     */
    public static FontPolicy getLogicalFontsPolicy() {
        return createFixedPolicy(FontSets.getLogicalFontSet());
    }


    // Utility Methods ********************************************************

    /**
     * Looks up and returns a custom FontSet for the given
     * Look&amp;Feel name, or {@code null} if no custom font set
     * has been defined for this Look&amp;Feel.
     *
     * @param the name of the Look&amp;Feel, one of {@code "Plastic"} or
     *     {@code "Windows"}
     * @return a custom FontPolicy - if any - or otherwise {@code null}
     */
    private static FontSet getCustomFontSet(String lafName) {
        String controlFontKey = lafName + ".controlFont";
        String menuFontKey    = lafName + ".menuFont";
        String decodedControlFont = LookUtils.getSystemProperty(controlFontKey);
        if (decodedControlFont == null) {
            return null;
        }
        Font controlFont = Font.decode(decodedControlFont);
        String decodedMenuFont = LookUtils.getSystemProperty(menuFontKey);
        Font menuFont = decodedMenuFont != null
            ? Font.decode(decodedMenuFont)
            : null;
        Font titleFont = "Plastic".equals(lafName)
            ? controlFont.deriveFont(Font.BOLD)
            : controlFont;
        return FontSets.createDefaultFontSet(controlFont, menuFont, titleFont);
    }


    /**
     * Looks up and returns a custom FontPolicy for the given
     * Look&amp;Feel name, or {@code null} if no custom policy has been
     * defined for this Look&amp;Feel.
     *
     * @param the name of the Look&amp;Feel, one of {@code "Plastic"} or
     *     {@code "Windows"}
     * @return a custom FontPolicy - if any - or otherwise {@code null}
     */
    private static FontPolicy getCustomPolicy(String lafName) {
        // TODO: Look up predefined font choice policies
        return null;
    }


    private static final class CustomSettingsPolicy implements FontPolicy {

        private final FontPolicy wrappedPolicy;

        CustomSettingsPolicy(FontPolicy wrappedPolicy) {
            this.wrappedPolicy = wrappedPolicy;
        }

        @Override
		public FontSet getFontSet(String lafName, UIDefaults table) {
            FontPolicy customPolicy = getCustomPolicy(lafName);
            if (customPolicy != null) {
                return customPolicy.getFontSet(null, table);
            }
            FontSet customFontSet = getCustomFontSet(lafName);
            if (customFontSet != null) {
                return customFontSet;
            }
            return wrappedPolicy.getFontSet(lafName, table);
        }
    }


//    private static final class DefaultCrossPlatformPolicy implements FontPolicy {
//
//        public FontSet getFontSet(String lafName, UIDefaults table) {
//            // TODO: If Tahoma or Segoe UI is available, return them
//            // in a size appropriate for the screen resolution.
//            // Otherwise return the logical font set.
//            return FontSets.getLogicalFontSet();
//        }
//    }


    /**
     * Implements the default font lookup for the Plastic L&f family
     * when running in a Windows environment.
     */
    private static final class DefaultPlasticOnWindowsPolicy extends DefaultWindowsPolicy {

        @Override
		public FontSet getFontSet(String lafName, UIDefaults table) {
            FontSet fontSet = super.getFontSet(lafName, table);
            
           
            Font titleFont = fontSet.getTitleFont().deriveFont(Font.BOLD);

            return new DefaultFontSet(
                    fontSet.getControlFont(),
                    fontSet.getMenuFont(),
                    titleFont,
                    fontSet.getMessageFont(),
                    fontSet.getSmallFont(),
                    fontSet.getWindowTitleFont());
        }
    }


    /**
     * Implements the default font lookup on the Windows platform.
     */
    private static class DefaultWindowsPolicy implements FontPolicy {
        
        private static final String FONT_UTILITIES_CLASS_NAME =
                SystemUtils.IS_JAVA_6
                    ? "sun.font.FontManager"
                    : "sun.font.FontUtilities";

        @Override
		public FontSet getFontSet(String lafName, UIDefaults table) {
            Font windowsControlFont = Fonts.getWindowsControlFont();
            Font controlFont = lookUpControlFont(windowsControlFont, table);
            Font menuFont = table == null
                ? controlFont
                : table.getFont("Menu.font");
            Font titleFont = controlFont;
            Font messageFont = table == null
                ? controlFont
                : table.getFont("OptionPane.font");
            Font smallFont = table == null
                ? controlFont.deriveFont(controlFont.getSize2D() - 2f)
                : table.getFont("ToolTip.font");
            Font windowTitleFont = table == null
                ? controlFont
                : table.getFont("InternalFrame.titleFont");
            return FontSets.createDefaultFontSet(
                    controlFont,
                    menuFont,
                    titleFont,
                    messageFont,
                    smallFont,
                    windowTitleFont);
        }
        
        
        private static Font lookUpControlFont(Font windowsControlFont, UIDefaults table) {
            checkNotNull(windowsControlFont, Messages.MUST_NOT_BE_NULL, "Windows control font");
            if (table == null) {
                return windowsControlFont;
            }
            // Obtain the table's button font and compare it with the windows control font.
            final Font buttonFont = table.getFont("Button.font");
            if (   buttonFont != null
                && buttonFont.getName().equals(windowsControlFont.getName())) {
                // If size and style are the same, we're done.
                if(   buttonFont.getSize()  == windowsControlFont.getSize()
                   || buttonFont.getStyle() == windowsControlFont.getStyle()) {
                    return buttonFont;
                }
                // Otherwise derive a font with the the style and size we want.
                return buttonFont.deriveFont(
                        windowsControlFont.getStyle(),
                        windowsControlFont.getSize2D());
            }
            // In case we want a font other than the Oracle Windows L&amp;F,
            // we try to use private Sun classes to obtain a a composite font.
            // This is necessary to for using a fallback font chain.
            if (fontSupportsDefaultEncoding(windowsControlFont)) {
                return windowsControlFont;
            }
            FontUIResource fuir = getCompositeFontUIResource(windowsControlFont);
            return fuir != null ? fuir : windowsControlFont;
        }
        
        
        private static boolean fontSupportsDefaultEncoding(Font font) {
            try {
                Method m = Class.forName(FONT_UTILITIES_CLASS_NAME).getMethod(
                    "fontSupportsDefaultEncoding", Font.class);
                if (m != null) {
                    return ((Boolean) m.invoke(null, font)).booleanValue();
                }
                return true;
            } catch (Exception ex) {
                // Ignore
            }
            return true;
        }
        
        
        private static FontUIResource getCompositeFontUIResource(Font font) {
            try {
                Method m = Class.forName(FONT_UTILITIES_CLASS_NAME).getMethod(
                    "getCompositeFontUIResource", Font.class);
                if (m != null) {
                    return (FontUIResource) m.invoke(null, font);
                }
                return null;
            } catch (Exception ex) {
                // Ignore
            }
            return null;
        }
        
    }


    /**
     * A FontPolicy that returns a fixed FontSet and that ignores
     * the laf name and UIDefaults table.
     */
    private static final class FixedPolicy implements FontPolicy {

        private final FontSet fontSet;

        FixedPolicy(FontSet fontSet) {
            this.fontSet = fontSet;
        }

        @Override
		public FontSet getFontSet(String lafName, UIDefaults table) {
            return fontSet;
        }
    }


}
