/*
 * aTunes 2.1.0-SNAPSHOT
 * Copyright (C) 2006-2011 Alex Aranda, Sylvain Gaudard and contributors
 *
 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors
 *
 * http://www.atunes.org
 * http://sourceforge.net/projects/atunes
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package net.sourceforge.atunes.gui.views.controls;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import net.sourceforge.atunes.utils.ImageUtils;

import com.jhlabs.image.PerspectiveFilter;

/**
 * A three dimensional cover.
 */
public final class Cover3D extends JPanel {

    private static final long serialVersionUID = -3836270786764203330L;

    private static final int GAP = 10;
    private static final float OPACITY = 0.5f;
    private static final float FADE_HEIGHT = 0.4f;

    private int angle;
    private BufferedImage image;
    private BufferedImage reflectedImage;

    /**
     * Instantiates a new three dimensional cover.
     */
    public Cover3D(int angle) {
        super(false);
        this.angle = angle;
        setOpaque(false);
    }

    @Override
    public void paintComponent(Graphics g) {
        if (image == null) {
            super.paintComponent(g);
            return;
        }

        Graphics2D g2d = (Graphics2D) g;
        g2d.translate((getWidth() - image.getWidth()) / 2, getHeight() / 2 - image.getHeight() / 2);
        g2d.drawRenderedImage(image, null);
        g2d.translate(0, 2 * image.getHeight() + GAP);
        g2d.scale(1, -1);
        g2d.drawRenderedImage(reflectedImage, null);
        g2d.dispose();
    }

    /**
     * Sets the image.
     * 
     * @param image
     *            the image to set
     */
    public void setImage(Image image, int width, int height) {
        if (image != null) {
            // IMAGE
            this.image = ImageUtils.scaleBufferedImageBicubic(image, width, height);
            if (angle != 0) {
                PerspectiveFilter filter1 = new PerspectiveFilter(0, angle, width - angle / 2, (int) (angle * (5.0 / 3.0)), width - angle / 2, height, 0, height + angle);
                this.image = filter1.filter(this.image, null);
            }

            // REFLECTED IMAGE
            BufferedImage reflection = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
            Graphics2D rg = reflection.createGraphics();
            rg.drawRenderedImage(this.image, null);
            rg.setComposite(AlphaComposite.getInstance(AlphaComposite.DST_IN));
            rg.setPaint(new GradientPaint(0, height * FADE_HEIGHT, new Color(0.0f, 0.0f, 0.0f, 0.0f), 0, height, new Color(0.0f, 0.0f, 0.0f, OPACITY)));
            rg.fillRect(0, 0, width, height);
            rg.dispose();

            if (angle != 0) {
                PerspectiveFilter filter2 = new PerspectiveFilter(0, 0, width - angle / 2, angle * 2, width - angle / 2, height + angle * 2, 0, height);
                reflectedImage = filter2.filter(reflection, null);
            } else {
                reflectedImage = reflection;
            }
        } else {
            this.image = null;
            this.reflectedImage = null;
        }
    }
}
