/*******************************************
  File: WallTool.js
  Created by Hawkeye
  Dreate date: 8/27/2002

  This tool draws walls.  Yay!

  Wall drawing is a multi-step process.  The first step, of course is to start the wall drawing.  Subsequent clicks define wall corners.  Two clicks in the same position mark the end of a wall.  Also a click at the start point of the wall object ends the wall.

  A wall is essentially a multi line with special properties like width.  To start with that's exactly what a wall will be.

  After the initial drawing the line can become the wall mask.
*******************************************/

WallTool.prototype = new DrawingTool(); 
WallTool.prototype.constructor = WallTool;
WallTool.superclass = DrawingTool.prototype;

function WallTool(){}

WallTool.prototype.drawer = ' <input type="button" value="終了" onclick="wallTool.endWalls(false);"><br>'
WallTool.prototype.drawer += ' 塗り <input id="wallFillCheck" type="checkbox" onclick="wallTool.fillWalls(this.checked);"><br> '
WallTool.prototype.drawer += ' 壁厚 <input id="wallWidthSetter" type="text" size="4" onChange="wallTool.setWallWidth(this.value);"><br><br> '


WallTool.prototype.initTool = function(){
  useCanvas();
  eventCatcher.addEventListener( "mouseup", this, false );
  this.actionState = "click0";
  this.lineList = null;
  window.document.getElementById("drawer2").innerHTML = this.drawer;
  if (this.fillColor == null ) this.fillColor = "black";
  document.getElementById("wallFillCheck").checked = this.fillColor == "none" ? false : true;
  document.getElementById("wallWidthSetter").value = wallWidth;
}

WallTool.prototype.mouseup = function(evt){
  switch (this.actionState){
  case "click0":
    this.startWall(evt);
    break;
  case "click1":
  this.setWall(evt);
    break;
  case "click2":

    break;
  default:

    break;
  } 
}

WallTool.prototype.mousemove = function(evt){
  
  switch (this.actionState){
    case "click0":
    alert("mousemove on cick0?");
    break;
  case "click1":
    this.updateLines(evt);
    break;
    case "click2":
    
    break;
  default:

    break;
  } 
}

WallTool.prototype.startWall = function(evt){
  //  debug( "starting wall" );
  var gotx    = getX(evt);
  var goty    = getY(evt);
  this.start = new Point2D(gotx,goty);
  this.current = new Point2D(gotx,goty);
  this.last  = new Point2D();

  this.lineList = new Array();
  this.curLine  = new Line( this.current.x, this.current.y, gotx, goty, true, true );
  this.curLine.tempLine();
  this.curLine.svgNode.setAttribute("style", "stroke-width:"+wallWidth);
  this.curLine.svgNode.setAttribute("opacity", ".5");

  eventCatcher.addEventListener( "mousemove", this, false );
  this.actionState = "click1";
}

WallTool.prototype.updateLines = function(evt){ 
  var gotx    = getX(evt);
  var goty    = getY(evt);
  this.curLine.update( gotx, goty, this.current.x, this.current.y);
}

WallTool.prototype.setWall = function(evt){
  this.last.setFromPoint(this.current);
  this.current.setXY( getX(evt), getY(evt) );

  if ( this.start.distanceFrom(this.current) < 5 ){
    this.curLine.update(  this.start.x, this.start.y, this.last.x, this.last.y );
    this.lineList.push( this.curLine );
    //alert("END THE WALL THING AT START, PLEASE");
    this.endWalls(true);
  } else if ( this.last.distanceFrom(this.current) < 5 ){
    //alert("END THE WALL THING, PLEASE");
    this.endWalls(false);
  } else {
    this.lineList.push( this.curLine );
    this.curLine = new Line(this.current.x, this.current.y, this.current.x, this.current.y, true, true);
    this.curLine.tempLine();
    this.curLine.svgNode.setAttribute("style", "stroke-width:"+wallWidth);
    this.curLine.svgNode.setAttribute("opacity", ".5");
  }
}
// FINISH UI AND DRAW THE WALLS.
WallTool.prototype.endWalls = function( atStart ){
  if (!this.lineList || !this.lineList.length) return 0;
  if (!atStart) this.curLine.destructor();
  var line, nextLine;
  var lastLine = atStart ? this.lineList[this.lineList.length-1] : null;

  for (var i=0;i<this.lineList.length;i++){
    line=this.lineList[i];
    if ( i == this.lineList.length - 1 && atStart ){
      nextLine = this.lineList[0];
    } else {
      nextLine = this.lineList[i+1];
    }
    //    if( !nextLine ) break;
    var wall = new WallObject( line, nextLine , lastLine, true );
    wall.setShapeStyle("fill", this.fillColor);
    lastLine = line;
  }
  for (var i=this.lineList.pop();i;i=this.lineList.pop()){
    wallMask.addObject(i);
  }
  eventCatcher.removeEventListener("mousemove", this, false);
  this.actionState = "click0";
  this.curLine = null;
  this.start = null;
  this.current = null;
  this.last = null;
  this.lineList = null;
}

WallTool.prototype.fillWalls = function( checked ){
  this.fillColor = !checked ? "none" : "black";
  var elems=svgDoc.getElementsByTagName("path");
  for(var i=0; i<elems.length; i++){
    if ( elems.item(i).getAttribute("id") == "wall"){
      elems.item(i).getStyle().setProperty("fill",this.fillColor);
    }
  }
}

WallTool.prototype.setWallWidth = function( val ) {
  // debug("settin"+val)
  if (val) {
    wallWidth = Number(val);
    // debug("set")
  }
}

WallTool.prototype.deactivate = function(evt){
  if ( this.actionState != "click0" ){
    for(var x= this.lineList.pop(); x; x=this.lineList.pop()){
      x.destructor();
    }
    this.curLine.destructor();
    eventCatcher.removeEventListener("mousemove", this, false);
  }
  eventCatcher.removeEventListener("mouseup", this, false);
  window.document.getElementById("drawer2").innerHTML = "";
}



WallTool.prototype.copyconstructor = function( svgNode ){
  var wallPath = svgNode.getAttribute("d");
  var pointList = wallPath.split( /[a-z|A-Z|,|\s]+/ );
  for (var i=0;i<pointList.length;i++){
    pointList[i] = Number( pointList[i] );
  }
  var p1x = ( pointList[0] + pointList[2] ) / 2;
  var p1y = ( pointList[1] + pointList[3] ) / 2;
  var p2x = ( pointList[4] + pointList[6] ) / 2;
  var p2y = ( pointList[5] + pointList[7] ) / 2;
  
  var wallLine  = new Line( p1x,p1y,p2x,p2y, true, true );
  wallLine.tempLine();
  wallLine.svgNode.setAttribute("style", "stroke-width:"+wallWidth);
  wallLine.svgNode.setAttribute("stroke","red");
  wallLine.svgNode.setAttribute("opacity", ".5");
	
	wallMask.addObject(wallLine);
  var wall = new WallObject();
  wall.copyconstructor(svgNode, wallLine);
}


var wallTool = new WallTool();
