package net.mkonrad.synes.gui;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.J3DGraphics2D;
import javax.media.j3d.Texture2D;

import net.mkonrad.synes.gui.style.Colors;
import net.mkonrad.synes.util.DbgTools;
import net.mkonrad.synes.util.ErrTools;

/**
 * Canvas Controller
 * Might be usefull for HUD, otherwise deprecated
 * @author mako
 *
 */
public class Canvas3DExt extends Canvas3D/* implements MouseMotionListener*/
{
  private ArrayList<BufferedImage> _instrImgs;
  private ArrayList<int[]> _instrImgPos;
  
  private BufferedImage _overlayImg;
  private Graphics2D _overlayG2D;   // for drawing into drawIm
  private J3DGraphics2D _overlayRenderer;    // for 2D rendering into the 3D canvas
  
  private int _panelWidth;
  private int _panelHeight;
  
  public Canvas3DExt(GraphicsConfiguration gconf, int panelWidth, int panelHeight)
  {
    super(gconf);

    _instrImgs = new ArrayList<BufferedImage>();
    _instrImgPos = new ArrayList<int[]>();
    
    _panelWidth = panelWidth;
    _panelHeight = panelHeight;
    
    _overlayImg = new BufferedImage(panelWidth, panelHeight, 
        BufferedImage.TYPE_4BYTE_ABGR);
    _overlayG2D = _overlayImg.createGraphics();
    _overlayRenderer = this.getGraphics2D();
  }
  
  public void postRender()
  {
    clearSurface();
    
    drawInstrIcons();
    
    _overlayRenderer.drawAndFlushImage(_overlayImg, 0, 0, this);
  }
  
  private void drawInstrIcons()
  {
    int[] pos;
    for (int i = 0; i < _instrImgs.size(); i++)
    {
      pos = _instrImgPos.get(i);
      
      //DbgTools.print("Drawing icon#" + i + " at " + pos[0] + ", " + pos[1]);
      
      _overlayG2D.drawImage(_instrImgs.get(i), pos[0], pos[1], null);
    }
  }
  
  public void clearInstrImgs()
  {
    _instrImgs.clear();
    _instrImgPos.clear();
  }

  public void addInstrImg(int x, int y, String iconFile)
  {
    _instrImgs.add(loadImage(iconFile));
    _instrImgPos.add(new int[] {x, y});
  }
  
  private BufferedImage loadImage(String fnm)
  {
    BufferedImage im = null;
    try
    {
      im = ImageIO.read(new File("./textures/instruments/" + fnm));
      DbgTools.print("Loaded icon " + fnm);
    }
    catch(IOException e)
    {
      DbgTools.print("Error loading icon " + fnm);
      ErrTools.print(e);
    }

    return im;
  }
  
  /**
   * Clear the drawing surface via its graphics context
   * Based on mokopa's code in
   *    http://www.javagaming.org/forums/index.php?topic=7320.0
   */
  private void clearSurface()
  {
    _overlayG2D.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR, 0.0f));
    _overlayG2D.fillRect(0,0, _panelWidth, _panelHeight);
    _overlayG2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f));
    _overlayG2D.setColor(Color.BLACK);
  }
}
