/* $Id: PolygonView.java 475 2011-12-02 08:46:54Z ohura $ */
package smart_gs.drawing_tool.view;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import smart_gs.GSConstants;
import smart_gs.drawing_tool.ExLine2D;
import smart_gs.logical.URICreator;


public class PolygonView extends View{
	
	private List<LineView> lines;

	public PolygonView(){
		this.lines = new ArrayList<LineView>();
		this.defaultColor = Color.BLUE;
		this.selectionColor = Color.red;
	}
	public PolygonView(List<ExLine2D> exLines){
		super();
		this.lines = new ArrayList<LineView>();
		for(int i=0;i<exLines.size();i++){
			this.lines.add(new LineView(exLines.get(i)));
		}
		//added 2011.10.02 shayashi to close the polygon
		this.lines.add(new LineView(new ExLine2D(exLines.get(exLines.size()-1).getP2(),exLines.get(0).getP1())));
		this.defaultColor = Color.BLUE;
		this.selectionColor = Color.red;
	}
	
	public PolygonView(List<ExLine2D> lines,boolean isSelected,boolean isEmphasized, boolean isProtected) {
		this(lines);
		this.isSelected = isSelected;
		this.isEmphasized = isEmphasized;
//		this.defaultColor = Color.BLUE;
//		this.selectionColor = Color.red;
		this.isProtected = isProtected;
	}

	// changed 2011.01.02 shayashi
	@Override
	public boolean contains(Point2D point) {
		int xpoints[],  ypoints[];
		xpoints = new int[lines.size()];
		ypoints = new int[lines.size()];
		Polygon polygon;
		for(int i=0;i<this.lines.size();i++){
			xpoints[i] = (int)this.lines.get(i).getLine().getP1().getX();
			ypoints[i] = (int)this.lines.get(i).getLine().getP1().getY();
		}
		polygon = new Polygon(xpoints,ypoints,lines.size());
		return polygon.contains(point);
	}
	
	public void addLineView(LineView line){
		this.lines.add(line);
	}
	@Override
	public void drawShape(Graphics2D g) {
		for(int i=0;i<this.lines.size();i++){
			this.lines.get(i).drawShape(g);
		}
	}

	@Override
	public int getType() {
		return View.POLYGON;
	}

	@Override
	public void moveBy(Point2D d) {
		for(int i=0;i<this.lines.size();i++){
			this.lines.get(i).moveBy(d);
		}
	}

	@Override
	public View enlargedView(double ratio, double gapX, double gapY) {
		List<ExLine2D> newLines = new ArrayList<ExLine2D>();
		for(int i=0;i<this.lines.size();i++){
			LineView line = this.lines.get(i);
			line = (LineView)line.enlargedView(ratio,gapX,gapY);
			newLines.add(line.getLine());
		}
		return new PolygonView(newLines,this.isSelected,this.isEmphasized, this.isProtected);
	}
	@Override
	public Rectangle2D getRectangle2D() {
		LineView line1 = this.lines.get(0);
		double minX = line1.getMinX(),minY = line1.getMinY(),maxX = line1.getMaxX(),maxY = line1.getMaxY(); 
		for(int i=0;i<this.lines.size();i++){
			LineView line = this.lines.get(i);
			if(minX > line.getMinX() || minX == -1){
				minX = line.getMinX();
			}
			if(minY > line.getMinY() || minX == -1){
				minY = line.getMinY();
			}
			if(maxX < line.getMaxX() || minX == -1){
				maxX = line.getMaxX();
			}
			if(maxY < line.getMaxY() || minX == -1){
				maxY = line.getMaxY();
			}
		}
		
		return new Rectangle2D.Double(minX,minY,maxX-minX,maxY-minY);
	}
	@Override
	public String getTypeString() {
		return URICreator.POLYGON;
	}
	@Override
	public void drawLinkedShape(Graphics2D g, Image image) {
//		if(this.isSelected){
//			this.draw(g);
//			return;
//		};
//		for(int i=0;i<this.lines.size();i++){
//			this.lines.get(i).drawLinkedShape(g);
//		}
		this.draw(g);
		this.drawLinkIcon(g, image);
	}
	@Override
	public Element createXMLElement(Document document) {
		Element element = document.createElement("view");
		element.setAttribute("type",URICreator.POLYGON);
		for(int i=0;i<this.lines.size();i++){
			element.appendChild(this.lines.get(i).createXMLElement(document));
		}
		return element;
	}

	public static PolygonView restore(Element element) {
		PolygonView view = new PolygonView();
		NodeList nodes = element.getChildNodes();

		for(int i=0;i<nodes.getLength();i++){
			if(nodes.item(i).getNodeType() == Node.ELEMENT_NODE){
				Element elem = (Element)nodes.item(i);
				String tagname = elem.getNodeName();
				if(tagname.equals("view")){
					LineView line = LineView.restore(elem);
					view.addLineView(line);
				}
			}
		}
		return view;
	}
	@Override
	public Point getCenterPoint() {
		return new Point((int)this.getRectangle2D().getCenterX(),
							(int)this.getRectangle2D().getCenterY());
	}
	@Override
	public String getTypeStringForDisplay() {
		return "Polygon";
	}
	@Override
	public void drawLinkIcon(Graphics2D g, Image image) {
		if (this.lines.size()==0) {
			return;
		}
		int x = (int)this.lines.get(0).getMinX();
		int y = (int)this.lines.get(0).getMinY();
		int w = image.getWidth(null) / 3;
		int h = image.getHeight(null) / 3;
		g.drawImage(image, x, y, w, h, null);
	}

}
