/**
 * This file is part of WiiuseJ.
 *
 *  WiiuseJ 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  WiiuseJ 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with WiiuseJ.  If not, see <http://www.gnu.org/licenses/>.
 */
package wiiusej.utils;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Toolkit;
import java.awt.geom.AffineTransform;
import wiiusej.wiiusejevents.physicalevents.ClassicControllerButtonsEvent;
import wiiusej.wiiusejevents.physicalevents.ClassicControllerEvent;
import wiiusej.wiiusejevents.physicalevents.ExpansionEvent;
import wiiusej.wiiusejevents.physicalevents.IREvent;
import wiiusej.wiiusejevents.physicalevents.JoystickEvent;
import wiiusej.wiiusejevents.physicalevents.MotionSensingEvent;
import wiiusej.wiiusejevents.physicalevents.WiimoteButtonsEvent;
import wiiusej.wiiusejevents.utils.WiimoteListener;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.ClassicControllerRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.DisconnectionEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.GuitarHeroRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukInsertedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.NunchukRemovedEvent;
import wiiusej.wiiusejevents.wiiuseapievents.StatusEvent;

/**
 * This panel is used to display what happens on the classic controller.
 * 
 * @author guiguito
 */
public class ClassicControllerButtonsEventPanel extends javax.swing.JPanel implements WiimoteListener {

    private Image mImage;// image for double buffering
    private Image wiimoteImage;// image for double buffering
    private ClassicControllerEvent event;
    private Color pressedColor = Color.RED;
    private Color heldColor = Color.ORANGE;
    private Color releasedColor = Color.YELLOW;
    private Color joystickColor = Color.PINK;
    private Color shoulderColor = Color.BLUE;
    private Shape shapeJoystick = new java.awt.geom.Ellipse2D.Double(0, 0, 15, 15);
    private Shape shapeButton = new java.awt.geom.Ellipse2D.Double(0, 0, 20, 20);
    private static int xAmplitude = 20;
    private static int yAmplitude = 20;

    /**
     * Default constructor. Red : button just pressed. Orange : button held.
     * Yellow : button just released.
     */
    public ClassicControllerButtonsEventPanel() {
        Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
        java.net.URL url = ButtonsEventPanel.class.getResource("/img/classiccontroller.png");
        wiimoteImage = toolkit.createImage(url);
        initComponents();
    }

    /**
     * Constructor used to set colors and shape used.
     * 
     * @param pressColor
     *            color of a button just pressed.
     * @param hColor
     *            color of a button held.
     * @param relColor
     *            color of a button just released.
     * @param jsColor
     *            color of the joysticks.
     * @param shouldColor
     *            color of the shoulders.
     * @param js
     *            shape drawn on the joysticks.
     * @param sh
     *            shape drawn on the buttons.
     */
    public ClassicControllerButtonsEventPanel(Color pressColor, Color hColor, Color relColor,
            Color jsColor, Color shouldColor, Shape js, Shape sh) {
        pressedColor = pressColor;
        heldColor = hColor;
        releasedColor = relColor;
        shapeButton = sh;
        shapeJoystick = js;
        joystickColor = jsColor;
        shoulderColor = shouldColor;
        Toolkit toolkit = java.awt.Toolkit.getDefaultToolkit();
        wiimoteImage = toolkit.createImage("img\\wiimote.png");
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Dimension d = getSize();
        checkOffScreenImage();
        Graphics offG = mImage.getGraphics();
        // offG.setColor(backgroundColor);
        offG.fillRect(0, 0, d.width, d.height);
        Graphics2D g2 = (Graphics2D) mImage.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        //draw classic controller
        g2.drawImage(wiimoteImage, 0, 0, this);
        g2.setTransform(new AffineTransform());

        if (event != null) {
            // draw buttons pushed
            ClassicControllerButtonsEvent buttons = event.getButtonsEvent();

            /* button A */
            if (buttons.isButtonAJustPressed()) {
                drawFunction(g2, pressedColor, 304, 76, shapeButton);
            }
            if (buttons.isButtonAHeld()) {
                drawFunction(g2, heldColor, 304, 76, shapeButton);
            }
            if (buttons.isButtonAJustReleased()) {
                drawFunction(g2, releasedColor, 304, 76, shapeButton);
            }

            /* button B */
            if (buttons.isButtonBJustPressed()) {
                drawFunction(g2, pressedColor, 269, 98, shapeButton);
            }
            if (buttons.isButtonBHeld()) {
                drawFunction(g2, heldColor, 269, 98, shapeButton);
            }
            if (buttons.isButtonBJustReleased()) {
                drawFunction(g2, releasedColor, 269, 98, shapeButton);
            }
                             
            /* button Down */
            if (buttons.isButtonDownJustPressed()) {
                drawFunction(g2, pressedColor, 60, 97, shapeButton);
            }
            if (buttons.isButtonDownHeld()) {
                drawFunction(g2, heldColor, 60, 97, shapeButton);
            }
            if (buttons.isButtonDownJustReleased()) {
                drawFunction(g2, releasedColor, 60, 97, shapeButton);
            }

            /* button FullLeft */
            if (buttons.isButtonFullLeftJustPressed()) {
                drawFunction(g2, pressedColor, 55, 4, shapeButton);
            }
            if (buttons.isButtonFullLeftHeld()) {
                drawFunction(g2, heldColor, 55, 4, shapeButton);
            }
            if (buttons.isButtonFullLeftJustReleased()) {
                drawFunction(g2, releasedColor, 55, 4, shapeButton);
            }

            /* button FullRight */
            if (buttons.isButtonFullRightJustPressed()) {
                drawFunction(g2, pressedColor, 276, 4, shapeButton);
            }
            if (buttons.isButtonFullRightHeld()) {
                drawFunction(g2, heldColor, 276, 4, shapeButton);
            }
            if (buttons.isButtonFullRightJustReleased()) {
                drawFunction(g2, releasedColor, 276, 4, shapeButton);
            }

            /* button Home */
            if (buttons.isButtonHomeJustPressed()) {
                drawFunction(g2, pressedColor, 166, 76, shapeButton);
            }
            if (buttons.isButtonHomeHeld()) {
                drawFunction(g2, heldColor, 166, 76, shapeButton);
            }
            if (buttons.isButtonHomeJustReleased()) {
                drawFunction(g2, releasedColor, 166, 76, shapeButton);
            }

            /* button Left */
            if (buttons.isButtonLeftJustPressed()) {
                drawFunction(g2, pressedColor, 34, 75, shapeButton);
            }
            if (buttons.isButtonLeftHeld()) {
                drawFunction(g2, heldColor, 34, 75, shapeButton);
            }
            if (buttons.isButtonLeftJustReleased()) {
                drawFunction(g2, releasedColor, 34, 75, shapeButton);
            }

            /* button Minus */
            if (buttons.isButtonMinusJustPressed()) {
                drawFunction(g2, pressedColor, 140, 76, shapeButton);
            }
            if (buttons.isButtonMinusHeld()) {
                drawFunction(g2, heldColor, 140, 76, shapeButton);
            }
            if (buttons.isButtonMinusJustReleased()) {
                drawFunction(g2, releasedColor, 140, 76, shapeButton);
            }

            /* button Plus */
            if (buttons.isButtonPlusJustPressed()) {
                drawFunction(g2, pressedColor, 191, 76, shapeButton);
            }
            if (buttons.isButtonPlusHeld()) {
                drawFunction(g2, heldColor, 191, 76, shapeButton);
            }
            if (buttons.isButtonPlusJustReleased()) {
                drawFunction(g2, releasedColor, 191, 76, shapeButton);
            }

            /* button Right */
            if (buttons.isButtonRightJustPressed()) {
                drawFunction(g2, pressedColor, 86, 75, shapeButton);
            }
            if (buttons.isButtonRightHeld()) {
                drawFunction(g2, heldColor, 86, 75, shapeButton);
            }
            if (buttons.isButtonRightJustReleased()) {
                drawFunction(g2, releasedColor, 86, 353, shapeButton);
            }

            /* button Up */
            if (buttons.isButtonUpJustPressed()) {
                drawFunction(g2, pressedColor, 60, 50, shapeButton);
            }
            if (buttons.isButtonUpHeld()) {
                drawFunction(g2, heldColor, 60, 50, shapeButton);
            }
            if (buttons.isButtonUpJustReleased()) {
                drawFunction(g2, releasedColor, 60, 50, shapeButton);
            }

            /* button X */
            if (buttons.isButtonXJustPressed()) {
                drawFunction(g2, pressedColor, 271, 53, shapeButton);
            }
            if (buttons.isButtonXHeld()) {
                drawFunction(g2, heldColor, 271, 53, shapeButton);
            }
            if (buttons.isButtonXJustReleased()) {
                drawFunction(g2, releasedColor, 271, 53, shapeButton);
            }

            /* button Y */
            if (buttons.isButtonYJustPressed()) {
                drawFunction(g2, pressedColor, 237, 76, shapeButton);
            }
            if (buttons.isButtonYHeld()) {
                drawFunction(g2, heldColor, 237, 76, shapeButton);
            }
            if (buttons.isButtonYJustReleased()) {
                drawFunction(g2, releasedColor, 237, 76, shapeButton);
            }

            /* button ZL */
            if (buttons.isButtonZLJustPressed()) {
                drawFunction(g2, pressedColor, 123, 4, shapeButton);
            }
            if (buttons.isButtonZLHeld()) {
                drawFunction(g2, heldColor, 123, 4, shapeButton);
            }
            if (buttons.isButtonZLJustReleased()) {
                drawFunction(g2, releasedColor, 123, 4, shapeButton);
            }

            /* button ZR */
            if (buttons.isButtonZRJustPressed()) {
                drawFunction(g2, pressedColor, 208, 4, shapeButton);
            }
            if (buttons.isButtonZRHeld()) {
                drawFunction(g2, heldColor, 208, 4, shapeButton);
            }
            if (buttons.isButtonZRJustReleased()) {
                drawFunction(g2, releasedColor, 208, 4, shapeButton);
            }

            //joysticks
            int halfWidth = (int) Math.round(shapeJoystick.getBounds().getWidth() / 2);
            int halfHeight = (int) Math.round(shapeJoystick.getBounds().getHeight() / 2);

            // left joystick
            JoystickEvent jl = event.getClassicControllerLeftJoystickEvent();
            int xCenter1 = 121;
            int yCenter1 = 125;

            double xAng1 = Math.sin(jl.getAngle() * Math.PI / 180.0) * jl.getMagnitude();
            double yAng1 = Math.cos(jl.getAngle() * Math.PI / 180.0) * jl.getMagnitude();

            int xShift1 = (int) Math.round(xAng1 * xAmplitude);
            int yShift1 = (int) Math.round(yAng1 * yAmplitude);
            int x1 = xCenter1 + xShift1 - halfWidth;
            int y1 = yCenter1 - yShift1 - halfHeight;
            // draw shape
            drawFunction(g2, joystickColor, x1, y1, shapeJoystick);

            //Right joystick
            JoystickEvent jr = event.getClassicControllerRightJoystickEvent();
            int xCenter2 = 213;
            int yCenter2 = 125;

            double xAng2 = Math.sin(jr.getAngle() * Math.PI / 180.0) * jr.getMagnitude();
            double yAng2 = Math.cos(jr.getAngle() * Math.PI / 180.0) * jr.getMagnitude();

            int xShift2 = (int) Math.round(xAng2 * xAmplitude);
            int yShift2 = (int) Math.round(yAng2 * yAmplitude);
            int x2 = xCenter2 + xShift2 - halfWidth;
            int y2 = yCenter2 - yShift2 - halfHeight;
            // draw shape
            drawFunction(g2, joystickColor, x2, y2, shapeJoystick);

            event = null;
        }

        // put offscreen image on the screen
        g.drawImage(mImage, 0, 0, null);
    }

    /**
     * Function used to factorize code.
     * 
     * @param g2
     *            where to draw a shape.
     * @param col
     *            color to use.
     * @param x
     *            x coordinates.
     * @param y
     *            y coordinates.
     * @param sh 
     *            shape to draw.
     */
    private void drawFunction(Graphics2D g2, Color col, int x, int y, Shape sh) {
        g2.setPaint(col);
        g2.translate(x, y);
        g2.draw(sh);
        g2.fill(sh);
        g2.setTransform(new AffineTransform());
    }

    /**
     * check if the mImage variable has been initialized. If it's not the case
     * it initializes it with the dimensions of the panel. mImage is for double
     * buffering.
     */
    private void checkOffScreenImage() {
        Dimension d = getSize();
        if (mImage == null || mImage.getWidth(null) != d.width || mImage.getHeight(null) != d.height) {
            mImage = createImage(d.width, d.height);
        }
    }

    public void onButtonsEvent(WiimoteButtonsEvent arg0) {
    //do nothing
    }

    public void onIrEvent(IREvent arg0) {
    //do nothing
    }

    public void onMotionSensingEvent(MotionSensingEvent arg0) {
    //do nothing
    }

    public void onExpansionEvent(ExpansionEvent arg0) {
        if (arg0 instanceof ClassicControllerEvent) {
            event = (ClassicControllerEvent) arg0;
        }
        repaint();
    }

    public void onStatusEvent(StatusEvent arg0) {
    //do nothing
    }

    public void onDisconnectionEvent(DisconnectionEvent arg0) {
    //do nothing
    }

    public void onNunchukInsertedEvent(NunchukInsertedEvent arg0) {
    //do nothing
    }

    public void onNunchukRemovedEvent(NunchukRemovedEvent arg0) {
    //do nothing
    }

    public void onGuitarHeroInsertedEvent(GuitarHeroInsertedEvent arg0) {
    //do nothing
    }

    public void onGuitarHeroRemovedEvent(GuitarHeroRemovedEvent arg0) {
    //do nothing
    }

    public void onClassicControllerInsertedEvent(ClassicControllerInsertedEvent arg0) {
    //do nothing
    }

    public void onClassicControllerRemovedEvent(ClassicControllerRemovedEvent arg0) {
        clearView();
    }

    public Color getHeldColor() {
        return heldColor;
    }

    public Color getJoystickColor() {
        return joystickColor;
    }

    public Color getPressedColor() {
        return pressedColor;
    }

    public Color getReleasedColor() {
        return releasedColor;
    }

    public Color getShoulderColor() {
        return shoulderColor;
    }

    public Shape getShapeButton() {
        return shapeButton;
    }

    public Shape getShapeJoystick() {
        return shapeJoystick;
    }

    public void setHeldColor(Color heldColor) {
        this.heldColor = heldColor;
    }

    public void setJoystickColor(Color joystickColor) {
        this.joystickColor = joystickColor;
    }

    public void setPressedColor(Color pressedColor) {
        this.pressedColor = pressedColor;
    }

    public void setReleasedColor(Color releasedColor) {
        this.releasedColor = releasedColor;
    }

    public void setShoulderColor(Color shoulderColor) {
        this.shoulderColor = shoulderColor;
    }

    public void setShapeButton(Shape shapeButton) {
        this.shapeButton = shapeButton;
    }

    public void setShapeJoystick(Shape shapeJoystick) {
        this.shapeJoystick = shapeJoystick;
    }

    public void clearView() {
        event = null;
        repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
