package model;


/**
 * @author patbgi
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

import com.jme.math.Vector3f;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;
import com.jmex.model.converters.MaxToJme;
import java.awt.Graphics2D;
import java.awt.Color;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import worldserver3d.IconFactory;



public class Obstacle
{
	private double x1, y1;
	private double x2, y2;
        private double Z; //hidden obstacles have Z < 0
        private double previousZ;
	private final double CORNER_SIZE = 4.0;
	//private int depth;
        private float depth = 2.0f; //temporary fixed depth

	private Material material;
        public Box box;
        public MaterialState ms;
        
        static int i=0;

        private Environment env;
        private boolean wasHidden = false;
        public IconFactory rememberMeIcon = null;
       
        
        
	public Obstacle() {}
	public Obstacle(double x, double y, Material material, MaterialState nms, Environment e)
	{
		x1 = x; y1 = y;
		x2 = x; y2 = y;

               //System.out.println("***coords of initial point: x= "+x+" and y= "+y);
		this.material = material;
                ms = nms;
                float dx = (float)(getX2()-getX1())/10;
                float dy = (float)(getY2()-getY1())/10;
                env = e;              
                box = new Box("TestBox"+i++, new Vector3f(),dx/2,2.0f,dy/2);
                System.out.println("dx:"+dx+" dy:"+2.0f+" dz:"+dy);
                box.setRenderState(ms);
                box.setRenderState(env.ls);
	        box.setLocalTranslation(new Vector3f((float)((getX2()+getX1())/20-env.width/20), depth , (float)(((getY2()+getY1())/20)-env.height/20)));
                //box.translatePoints((float)(o.getX2()+o.getX1())/20, 5.0f , (float)(o.getY2()+o.getY1())/20);
                //this.getRootNode().attachChild(box);	// Attach the box to rootNode in DebugGameState
	        //box.setSolidColor(ColorRGBA.red);		// Set random colors on it - it will only be visible if the lights are off though
	        //rememberMeIcon = new RememberMeIconFactory(dx, dy); 
                box.updateRenderState();     
                //rememberMeIcon = new RememberMeIconFactory(x,y);
	}


	public double getX1() { return x1; }
	public double getY1() { return y1; }
	public double getX2() { return x2; }
	public double getY2() { return y2; }
        public double getZ() { return Z; }
	
	public float getDepth() { return depth; }
	
	public Material getMaterial() { return material; }
	
	public double getHeight() { return y2-y1; }
	public double getWidth()  { return x2-x1; }


	public void setX1(double x1) { this.x1 = x1; }
	public void setY1(double y1) { this.y1 = y1; }
	public void setX2(double x2) { this.x2 = x2; }
	public void setY2(double y2) { this.y2 = y2; }
	public void setZ(double z) { this.Z = z; }
        
	public void setDepth(int depth) { this.depth = depth; }
	
	public void setMaterial(Material material) { this.material = material; }

	public void setHeight(double height) { y2 = y1+height; }
	public void setWidth (double width)  { x2 = x1+width; }


	public void moveX1(double dx) { x1 += dx; }
	public void moveY1(double dy) { y1 += dy; }
	public void moveX2(double dx) { x2 += dx; }
	public void moveY2(double dy) { y2 += dy; }
	
	public void move(double dx, double dy)
	{
		x1 += dx; y1 += dy;
		x2 += dx; y2 += dy;
	}


	public boolean contains(double x, double y)
	{
		return ((x >= x1 && x <= x2 || x >= x2 && x <= x1) &&
				(y >= y1 && y <= y2 || y >= y2 && y <= y1)) ? true : false;
	}

	public boolean containsX1Y1(double x, double y)
	{
		return (x >= x1-CORNER_SIZE/2 && x <= x1+CORNER_SIZE/2 &&
				y >= y1-CORNER_SIZE/2 && y <= y1+CORNER_SIZE/2) ? true : false;
	}

	public boolean containsX1Y2(double x, double y)
	{
		return (x >= x1-CORNER_SIZE/2 && x <= x1+CORNER_SIZE/2 &&
				y >= y2-CORNER_SIZE/2 && y <= y2+CORNER_SIZE/2) ? true : false;
	}

	public boolean containsX2Y1(double x, double y)
	{
		return (x >= x2-CORNER_SIZE/2 && x <= x2+CORNER_SIZE/2 &&
				y >= y1-CORNER_SIZE/2 && y <= y1+CORNER_SIZE/2) ? true : false;
	}

	public boolean containsX2Y2(double x, double y)
	{
		return (x >= x2-CORNER_SIZE/2 && x <= x2+CORNER_SIZE/2 &&
				y >= y2-CORNER_SIZE/2 && y <= y2+CORNER_SIZE/2) ? true : false;
	}

	
	public void revertX() { double temp = x1; x1 = x2; x2 = temp; }
	public void revertY() { double temp = x1; x1 = x2; x2 = temp; }

	
	public void draw(Graphics2D g2D)
	{
		/* Calculate rectangle coordinates */
		double x = (x1 < x2) ? x1 : x2;
		double y = (y1 < y2) ? y1 : y2;
		double width  = (x1 < x2) ? (x2-x1) : (x1-x2);
		double height = (y1 < y2) ? (y2-y1) : (y1-y2);
			
		/* Fill big rectangle */
		g2D.setColor(material.getColor());
		g2D.fillRect((int)x, (int)y, (int)width, (int)height);
	}

	public void drawContour(Graphics2D g2D)
	{
		/* Calculate rectangle coordinates */
		double x = (x1 < x2) ? x1 : x2;
		double y = (y1 < y2) ? y1 : y2;
		double width  = (x1 < x2) ? (x2-x1) : (x1-x2);
		double height = (y1 < y2) ? (y2-y1) : (y1-y2);
			
		/* Draw big rectangle */
		g2D.setColor(Color.black);
		g2D.drawRect((int)x, (int)y, (int)width, (int)height);
	}
	
	void drawSelected(Graphics2D g2D)
	{
		/* Calculate rectangle coordinates */
		double x = (x1 < x2) ? x1 : x2;
		double y = (y1 < y2) ? y1 : y2;
		double width  = (x1 < x2) ? (x2-x1) : (x1-x2);
		double height = (y1 < y2) ? (y2-y1) : (y1-y2);
			
		/* Draw big rectangle */
		g2D.setColor(Color.black);
		g2D.drawRect((int)x, (int)y, (int)width, (int)height);

		/* Fill corner rectangle */
		g2D.fillRect((int)(x1-CORNER_SIZE/2), (int)(y1-CORNER_SIZE/2), (int)CORNER_SIZE, (int)CORNER_SIZE);
		g2D.fillRect((int)(x2-CORNER_SIZE/2), (int)(y1-CORNER_SIZE/2), (int)CORNER_SIZE, (int)CORNER_SIZE);
		g2D.fillRect((int)(x2-CORNER_SIZE/2), (int)(y2-CORNER_SIZE/2), (int)CORNER_SIZE, (int)CORNER_SIZE);
		g2D.fillRect((int)(x1-CORNER_SIZE/2), (int)(y2-CORNER_SIZE/2), (int)CORNER_SIZE, (int)CORNER_SIZE);
	}
        
        public boolean returnIfWasHidden() { return wasHidden;}
        public void hideMe(Graphics2D graphics) 
        {
            System.out.println("====  public void hideMe() ===");
            System.out.println("====  z= "+Z);
            previousZ = Z;
            wasHidden = true;
            //refresh myself
            //this.material.setHiddenObstacleMaterial();
            setZ(previousZ-5);
//            Vector3f vector = new Vector3f((float)((getX2()+getX1())/20-env.width/20), depth , (float)(((getY2()+getY1())/20)-env.height/20));
//            box.setLocalTranslation(vector);           
            Vector3f vector = new Vector3f((float)((getX2()+getX1())/20-env.width/20), 5f , (float)(((getY2()+getY1())/20)-env.height/20));
            System.out.println("====  Obstacle:  x1= "+getX1()+" y1= "+getY1()+"x2= "+getX2()+" y2= "+getY2()+" center: "+vector.getX()+"  "+vector.getZ());        
//compile only            rememberMeIcon = new RememberMeIconFactory();
            draw(graphics);            
        }
        public void undoHideMe(Graphics2D graphics) 
        {
            System.out.println("====  public void undoHideMe() ===");
            wasHidden = false;
            //visible again
            setZ(previousZ);
            //box.setLocalTranslation(new Vector3f((float)((getX2()+getX1())/20-env.width/20), depth , (float)(((getY2()+getY1())/20)-env.height/20)));
            draw(graphics);
        }
}
