package kinectintruderalarm;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

/** ImagePanel: Panel for depth and rgb image
 *
 */
public class KoImagePanelLIGHT extends JPanel {

    int sizeX, sizeY;
    BufferedImage image = null;
    Color color = Color.black;
    String name;

    // --------------------------------------------------------------------
    public KoImagePanelLIGHT(int sx, int sy) {
        super();
        sizeX = sx;
        sizeY = sy;
        image = new BufferedImage(sizeX, sizeY, BufferedImage.TYPE_3BYTE_BGR);
        setPreferredSize(new Dimension(sx, sy));
    }

    /**setImage: creates buffered image from pixel values
     *
     * @param pixels
     */
    public void setImage(int[] pixels) {
        int base = (int) (Math.sqrt(pixels.length / 12));
        int resX = base * 4;
        int resY = base * 3;
        image = new BufferedImage(resX, resY, BufferedImage.TYPE_3BYTE_BGR);
        image.setRGB(0, 0, resX, resY, pixels, 0, resX);
        repaint();
    }

    /**convert depth data to int[] image, which can then be used as input for setImage
     *
     * @param depth
     * @return
     */
    public int[] convertDepthToImage(int[][] depth) {
        // convert to color
        int[] pixels = new int[depth.length];
        // color table for index (detected humans)
        int[][] col = {{200, 0, 0}, {0, 200, 0}, {0, 0, 200}, {200, 200, 0}, {0, 200, 200}, {200, 0, 200}};

        for (int i = 0; i < pixels.length; i++) {
            int distance = depth[i][0];
            int red, green, blue;
            int index = depth[i][1];
            if (index > 0) {
                red = Math.min(col[index - 1][0] + (int) (Math.random() * 255), 255);
                green = Math.min(col[index - 1][1] + (int) (Math.random() * 255), 255);
                blue = Math.min(col[index - 1][2] + (int) (Math.random() * 255), 255);
            } else {
                blue = 255 * (distance - 300) / (1500 - 300);
                green = 255 * (distance - 1500) / (2500 - 1500);
                red = 255 * (distance - 2500) / (6000 - 2500);
                if (blue < 0) {
                    blue = 0;
                }
                if (blue > 512) {
                    blue = 0;
                }
                if (blue > 255) {
                    blue = 512 - blue;
                }
                if (green < 0) {
                    green = 0;
                }
                if (green > 512) {
                    green = 0;
                }
                if (green > 255) {
                    green = 512 - green;
                }
                if (red < 0) {
                    red = 0;
                }
                if (red > 512) {
                    red = 0;
                }
                if (red > 255) {
                    red = 512 - red;
                }
            }
            pixels[i] = (0xff << 24 | red << 16 | green << 8 | blue);
        }
        return (pixels);
    }


    // --------------------------------------------------------------------
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D) g;
        if (image != null) {
            //AffineTransform at = new AffineTransform();
            double scale = (double) sizeX / image.getWidth();
            AffineTransform tx = AffineTransform.getScaleInstance(-scale, scale);
            tx.translate(-image.getWidth(null), 0);
            AffineTransformOp scaleOp = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
            g2.drawImage(image, scaleOp, 0, 0);
            //label
            g.setColor(Color.black);
            g.fillRect(0, 0, 45, 15);
            g.setColor(Color.green);
            g.drawString("Image", 5, 10);
        } else {
            g.setColor(color);
            g.fillRect(0, 0, sizeX, sizeY);
            g.setColor(Color.red);
            g.drawString("No Image", sizeX / 2, sizeY / 2);
        }
    }
}
