/*
 * Copyright (c) 2014 Paul Scherrer Institute. All rights reserved.
 */
package ch.psi.plot.utils;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.TransferHandler;
import javax.swing.filechooser.FileFilter;

/**
 *
 */
public class SwingUtils {

    public static void centerOnParent(Component parent, Component component) {
        Rectangle r = parent.getBounds();
        Dimension w = component.getSize();
        int x = Math.max(r.x + (r.width - w.width) / 2, 4);
        int y = Math.max(r.y + (r.height - w.height) / 2, 4);
        component.setLocation(x, y);
    }

    public static void centerOnScreen(Component component) {
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        component.setLocation(dim.width / 2 - component.getSize().width / 2, dim.height / 2 - component.getSize().height / 2);
    }

    public static Color generateRandomColor(long seed) {
        Random random = new Random(seed);
        return generateRandomColor(random);
    }

    public static Color generateRandomColor() {
        Random random = new Random();
        return generateRandomColor(random);
    }

    public static Color generateRandomColor(Random random) {

        Color mix = Color.WHITE;
        int red = (random.nextInt(256) + mix.getRed()) / 2;
        int green = (random.nextInt(256) + mix.getGreen()) / 2;
        int blue = (random.nextInt(256) + mix.getBlue()) / 2;

        Color color = new Color(red, green, blue);
        return color;
    }

    public static void showMessage(Component parent, String title, String msg) {
        JOptionPane.showMessageDialog(parent, msg, title, JOptionPane.INFORMATION_MESSAGE, null);
    }

    public static void showException(Component parent, Exception ex) {
        JOptionPane.showMessageDialog(parent, ex.getMessage(), "Exception", JOptionPane.WARNING_MESSAGE, null);
    }
    
    public static int  showOption(Component parent, String title, String msg, int optionType) 
    {        
        return JOptionPane.showOptionDialog(parent,msg,title,optionType,JOptionPane.QUESTION_MESSAGE,null,null,null);      
    }    

    public static String getString(Component parent, String msg, Object current_value) {
        return JOptionPane.showInputDialog(parent, msg, String.valueOf(current_value));
    }
    

    public static BufferedImage createImage(Component panel) {
        if (panel == null) {
            return null;
        }
        Dimension size = panel.getSize();
        BufferedImage image = new BufferedImage(
                size.width, size.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = image.createGraphics();
        panel.paint(g2);
        return image;
    }

    public static class ImageTransferHandler extends TransferHandler implements Transferable {

        private static final DataFlavor[] dataFlavors = {DataFlavor.imageFlavor};
        private Image image;

        public ImageTransferHandler(Image data) {
            this.image = data;
        }

        @Override
        public Object getTransferData(DataFlavor flavor) {
            if (isDataFlavorSupported(flavor)) {
                return image;
            }
            return null;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return dataFlavors;
        }

        @Override
        public int getSourceActions(JComponent c) {
            return TransferHandler.COPY;
        }

        @Override
        public boolean canImport(JComponent comp, DataFlavor flavor[]) {
            if (!(comp instanceof JLabel)) {
                return false;
            }
            for (int i = 0, n = flavor.length; i < n; i++) {
                for (int j = 0, m = dataFlavors.length; j < m; j++) {
                    if (flavor[i].equals(dataFlavors[j])) {
                        return true;
                    }
                }
            }
            return false;
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.equals(DataFlavor.imageFlavor);
        }
    }

    public static class ExtensionFileFilter extends FileFilter {

        String description;

        String extensions[];

        public ExtensionFileFilter(String description, String extension) {
            this(description, new String[]{extension});
        }

        public ExtensionFileFilter(String description, String extensions[]) {
            if (description == null) {
                this.description = extensions[0];
            } else {
                this.description = description;
            }
            this.extensions = (String[]) extensions.clone();
            for (int i = 0, n = extensions.length; i < n; i++) {
                extensions[i] = extensions[i].toLowerCase();
            }
        }

        public String getDescription() {
            return description;
        }

        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            } else {
                String path = file.getAbsolutePath().toLowerCase();
                for (int i = 0, n = extensions.length; i < n; i++) {
                    String extension = extensions[i];
                    if ((path.endsWith(extension) && (path.charAt(path.length() - extension.length() - 1)) == '.')) {
                        return true;
                    }
                }
            }
            return false;
        }

    }

}
