/* $Id: LineSegment.java 654 2013-02-08 10:05:15Z ohura $ */
package smart_gs.logical;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;
import java.awt.Polygon;

import smart_gs.drawing_tool.ExLine2D;
import smart_gs.drawing_tool.view.LineSegView;
import smart_gs.drawing_tool.view.View;
import smart_gs.logical.region.PolygonRegion;
import sml_editor.logical.LineDirection;



/**
 * 
 * @author kazuaki kobayashi
 *
 * Spread上の行を表すクラス
 */
public class LineSegment extends Region{
	
	public LineSegment(Spread spread,List<Point2D> points, 
			String uri, String originalURI, int index){
		super(spread, URICreator.LINE_SEGMENT);
		this.points = points;
		this.createView(index);
		this.setURIs(uri, originalURI);
	}
	
	private void createView(int index) {
		List<ExLine2D> lines = new ArrayList<ExLine2D>();
		for(int i=0;i<this.points.size()-1;i++){
			Point2D start = this.points.get(i);
			Point2D end = this.points.get(i+1);
			ExLine2D line = new ExLine2D(start,end);
			lines.add(line);
		}
		LineDirection lineDirection = this.spread.getLineDirection();
		this.view = new LineSegView(lines, lineDirection, index);
		
	}

	public LineSegment(Spread spread, List<Point2D> points) {
		super(spread, URICreator.LINE_SEGMENT);
		this.points = points;
		this.createView();
		this.setURIs();
	}
	
	public LineSegment(Spread spread, List<Point2D> points, int index) {
		super(spread, URICreator.LINE_SEGMENT);
		this.points = points;
		this.createView(index);
		this.setURIs();
	}
	
	public void setIndex(int i) {
		((LineSegView)view).setIndex(i);
	}

	private void createView(){
		List<ExLine2D> lines = new ArrayList<ExLine2D>();
		for(int i=0;i<this.points.size()-1;i++){
			Point2D start = this.points.get(i);
			Point2D end = this.points.get(i+1);
			ExLine2D line = new ExLine2D(start,end);
			lines.add(line);
		}
		this.view = new LineSegView(lines);
	}
	public Spread getSpread() {
		return this.spread;
	}
	public List<Point2D> getPoints() {
		return this.points;
	}
	public View getView(){
		return this.view;
	}
	
	//20110221 shimizu add
	public void changeSize(double m){
	  List<Point2D> points = this.getPoints();
	  for(int i=0;i<points.size();i++){
		  Point2D point = points.get(i);
		  double x = point.getX()*m;
		  double y = point.getY()*m;
		  point.setLocation(x,y);
	  }
	  int index = this.getView().getIndex();
	  this.createView(index);
	}
	
	public void slideLine(double x1, double y1){
		List<Point2D> points = this.getPoints();
		for(int i=0;i<points.size();i++){
			Point2D point = points.get(i);
			double x = point.getX() + x1;
			double y = point.getY() + y1;
			point.setLocation(x,y);
		}
		int index = this.getView().getIndex();
		this.createView(index);
	}
	//20100825 shimizu add
	public Polygon getPolygon(){
		Polygon polygon = new Polygon();
		for(int i=0; i<this.points.size()-1;i++){
			int x = (int)this.points.get(i).getX();
			int y = (int)this.points.get(i).getY();
			polygon.addPoint(x, y);
		}
		return polygon;
	}
	//20101110 shimizu wrote;
	public void addPoint(Point2D point){
		List<ExLine2D> lines = new ArrayList<ExLine2D>();
		for(int i=0;i<this.points.size()-1;i++){
			Point2D start = this.points.get(i);
			Point2D end = this.points.get(i+1);
			ExLine2D line = new ExLine2D(start,end);
			lines.add(line);
		}
	    double dist = lines.get(0).ptLineDist(point);
	    int x = 0;
		for(int i=1;i<lines.size();i++){
			Double d = lines.get(i).ptLineDist(point);
			if(d<dist){
				dist = d;
				x = i;
			}
			
		}
		this.points.add(x+1,point);
		int index = this.getView().getIndex();
		this.createView(index);
	}

	public Region transformToPolygonRegion(Spread spread) {
		return new PolygonRegion(spread,this.getPoints());
	}
}
