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

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

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

import smart_gs.drawing_tool.ExLine2D;
import smart_gs.drawing_tool.ImageLabel;
import smart_gs.drawing_tool.ImageLabel.Show;
import smart_gs.logical.URICreator;


public class LineSegmentsView extends View{
	Color LineSegmentDefaultColor = new Color(0,0,80,80);
	private List<LineView> lines;
	private int index;
	private IndexView indexView;

	public LineSegmentsView(int index){
		this.lines = new ArrayList<LineView>();
//		Line_segment color shayashi 2011.05.31
		this.defaultColor = LineSegmentDefaultColor;
		this.selectionColor = Color.red;
		this.index = index;
//		this.createIndexView(index);
	}
	public LineSegmentsView(List<ExLine2D> exLines, int index){
		super();
		this.lines = new ArrayList<LineView>();
		for(int i=0;i<exLines.size();i++){
			this.lines.add(new LineView(exLines.get(i)));
		}
		this.lines.add(new LineView(new ExLine2D(exLines.get(exLines.size()-1).getP2(),exLines.get(0).getP1())));
//		Line_segment color shayashi 2011.05.31
		this.defaultColor = LineSegmentDefaultColor;
		this.selectionColor = Color.red;
		this.index = index;
		if (this.lines.size() != 0) {
			this.createIndexView(index);
		}
	}
	
	public LineSegmentsView(List<ExLine2D> lines,boolean isSelected,boolean isEmphasized, boolean isProtected, int index) {
		this(lines, index);
		this.isSelected = isSelected;
		this.isEmphasized = isEmphasized;
		this.isProtected = isProtected;
	}

	public LineSegmentsView(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)));
		}
		this.lines.add(new LineView(new ExLine2D(exLines.get(exLines.size()-1).getP2(),exLines.get(0).getP1())));
		this.defaultColor = LineSegmentDefaultColor;
		this.selectionColor = Color.red;
	}
	
	// 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);
		}
	}
	
	public void drawShape(Graphics2D g, ImageLabel.Show mode) {
		if (mode == ImageLabel.Show.LINE_AND_INDEX) {
			this.drawShape(g);
			this.indexView.drawShape(g);
		} else if (mode == ImageLabel.Show.LINE) {
			this.drawShape(g);
		} else if (mode == ImageLabel.Show.INDEX){
			this.indexView.drawShape(g);
		}
	}

	
	@Override
	public int getType() {
		return View.LINE_SEGMENT;
	}
	
	//2011/01/06 kukita
	public String getTypeString() {
		return URICreator.LINE_SEGMENT;
	}

	@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 LineSegmentsView(newLines,this.isSelected,this.isEmphasized, this.isProtected, this.index);
	}
	@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 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, image);
		}
	}
	@Override
	public Element createXMLElement(Document document) {
		Element element = document.createElement("view");
		element.setAttribute("type",URICreator.LINE_SEGMENT);
		for(int i=0;i<this.lines.size();i++){
			element.appendChild(this.lines.get(i).createXMLElement(document));
		}
		return element;
	}

	public static LineSegmentsView restore(Element element, int index) {
		LineSegmentsView view = new LineSegmentsView(index);
		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 "LineSegment";
	}
	@Override
	public int getIndex() {
		return this.index;
	}

	private void createIndexView(int index) {
		this.indexView = new IndexView(index, this.getMinX(), this.getMinY());
	}
	private int getMinY() {
		int minY = (int) this.lines.get(0).getMinY();
		for (int i = 0; i < this.lines.size(); i++) {
			if (minY > (int)this.lines.get(i).getMinY()) {
				minY = (int)this.lines.get(i).getMinY();
			}
		}
		return minY;
	}
	private int getMinX() {
		int minX = (int) this.lines.get(0).getMinX();
		for (int i = 0; i < this.lines.size(); i++) {
			if (minX > (int)this.lines.get(i).getMinX()) {
				minX = (int)this.lines.get(i).getMinX();
			}
		}
		return minX;
	}

	
	
	
	
	
	
	
	
	private class IndexView {
		private int index;
		private int x;
		private int y;
		
		public IndexView(int index, int x, int y) {
			this.index = index;
			this.x = x;
			this.y = y;
		}

		protected void drawShape(Graphics2D g) {
			FontMetrics fm = g.getFontMetrics();
			int h = fm.getHeight();
			int ast = fm.getAscent();
			int w = fm.stringWidth(this.index+"");
			int xx = x - w/2;
			int yy = y - h/2;
			Rectangle2D rectangle = new Rectangle2D.Double(xx, yy, w + 10, h);
			g.setColor(new Color(204,255,255));
			g.fill(rectangle);
			g.setColor(Color.BLACK);
			g.draw(rectangle);
			g.drawString(index+"", xx + 5, yy + ast);
		}
	}









	@Override
	public void drawLinkIcon(Graphics2D g, Image image) {
		// TODO Auto-generated method stub
		
	}


}