package CookieCutter.g1;

import CookieCutter.*;
import java.util.*;

public class PixelCookiePlacer implements CookiePlacer
{
    CookieGroup cookiegroup;

    public void placeCookies(Cookie[] iCookies)
    {
	double maxX = 0;

	PixelFitter pix = new PixelFitter(iCookies[0]);
	PixelSolution solution = pix.solve();
	solution.print();

	Cookie fixed = iCookies[0].copy();
	Vertex shape[] = fixed.getVertices();

	List cookies = new ArrayList();

	for (int i = 0; i < iCookies.length; i++)
	{
	    try
	    {

		if (i%2==0)
		{
		    // place even cookies any stupid place
		    Vertex placement[] = translate(shape, maxX, 0);
		    iCookies[i] = fixed.transform(CookieCutter.centroid(placement), 0);

		    if (i==0)
			cookies.add(fixed.transform(CookieCutter.centroid(placement), 0));
		    
		    print(placement);		    
		}
		else
		{
		    if (solution.xOffset==0 && solution.yOffset==0)
		    {
			// no solution found			
			maxX+=.4;
			Vertex placement[] = translate(shape, maxX, 0);
			iCookies[i] = fixed.transform(CookieCutter.centroid(placement), 0);

			if (i==1)
			    cookies.add(fixed.transform(CookieCutter.centroid(placement), 0));


			print(placement);			
		    }
		    else
		    {
			// place odd cookies according to solution we found			
			Vertex placement[] = shape;
						
			print(placement);
			// rotate first
			if (solution.angle==3)
			{
			    placement = rotate180(placement);
			}
			print(placement);
			// then translate
			placement = translate(placement,
					      maxX-solution.xOffset+.001,
					      -solution.yOffset+.001);

			print(placement);

			iCookies[i] = fixed.transform(CookieCutter.centroid(placement),
						      Math.PI);

			if (i==1)
			    cookies.add(fixed.transform(CookieCutter.centroid(placement),
							Math.PI));



		    }
		}
		maxX+=.4;
	    }
	    catch (Exception e)
	    {
		iCookies[i]=fixed.copy();
		maxX+=4;
	    }

	}

	cookiegroup = new CookieGroup(cookies);
    }


    public static void print(Vertex[] v)
    {	
    }
    
    public static double getMaxX(Vertex[] v)
    {
	double max = v[0].xpos();
	
	for (int i = 1; i < v.length; i++)
	{
	    if (v[i].xpos()>max)
	    {
		max = v[i].xpos();
	    }
	}
	return max;
    }

    public static double getMinX(Vertex[] v)
    {
	double min = v[0].xpos();
	
	for (int i = 1; i < v.length; i++)
	{
	    if (v[i].xpos()<min)
	    {
		min = v[i].xpos();
	    }
	}
	return min;
    }

    public static double getMaxY(Vertex[] v)
    {
	double max = v[0].ypos();
	
	for (int i = 1; i < v.length; i++)
	{
	    if (v[i].ypos()>max)
	    {
		max = v[i].ypos();
	    }
	}
	return max;
    }

    public static double getMinY(Vertex[] v)
    {
	double min = v[0].ypos();
	
	for (int i = 1; i < v.length; i++)
	{
	    if (v[i].ypos()<min)
	    {
		min = v[i].ypos();
	    }
	}
	return min;
    }
    
    public static Vertex[] translate(Vertex shape[], double xOffset, double yOffset)
    {
	Vertex newPosition[] = new Vertex[shape.length];

	// go through and move x and y coordinates relative to
	// bounding box
	for (int i = 0; i < shape.length; i++)
	{
	    double x = shape[i].xpos();
	    double y = shape[i].ypos();
	    x += xOffset;
	    y += yOffset;

	    // sanitize by rounding to ten-thousands place
	    x = ((double)((int)(x*10000d+.5d)))/10000;
	    y = ((double)((int)(y*10000d+.5d)))/10000;

	    newPosition[i] = new Vertex(x, y);
	}

	return newPosition;
    }

    public static Vertex[] rotate180(Vertex shape[])
    {
	Vertex newPosition[] = new Vertex[shape.length];

	double top, bottom, right, left;
	top = getMaxY(shape);
	bottom = getMinY(shape);
	right = getMaxX(shape);
	left = getMinX(shape);

	double width = left - right;
	double height = top - bottom;
	double maxDimension = width;
	if (height>width)
	    maxDimension=height;	
	
	// go through and move x and y coordinates relative to
	for (int i = 0; i < shape.length; i++)
	{
//	    double x = (left+maxDimension) - (shape[i].xpos()-right);
//	    double y = (bottom+maxDimension) - (shape[i].ypos()-top);

	    double x = left   - (shape[i].xpos()-(left+maxDimension));
	    double y = bottom - (shape[i].ypos()-(bottom+maxDimension));

	    // sanitize by rounding to ten-thousands place
	    x = ((double)((int)(x*10000d+.5d)))/10000;
	    y = ((double)((int)(y*10000d+.5d)))/10000;

	    newPosition[i] = new Vertex(x, y);
	}
	
	return newPosition;
    }
}
