/*
* Copyright (C) 1998-2018 by Northwoods Software Corporation
* All Rights Reserved.
*
* FLOOR PLANNER - WALL BUILDING TOOL
* Used to construct new Walls in a Floorplan with mouse clicking / mouse point
*/

import * as go from "../../../release/go"
import WallReshapingTool = require("./WallReshapingTool");
import Floorplan = require("./Floorplan");

class WallBuildingTool extends go.Tool {

	private _startPoint: go.Point;
	private _endPoint: go.Point;
	private _wallReshapingTool: WallReshapingTool;

	constructor() {
		super();

		this.name = "WallBuilding";
	    this._startPoint = null;
	    this._endPoint = null;
	    this._wallReshapingTool = null;
	}

	// Get / set the current startPoint
	get startPoint() { return this._startPoint; }
	set startPoint(value: go.Point) { this._startPoint = value }

	// Get / set the current endPoint
	get endPoint() { return this._endPoint; }
	set endPoint(value: go.Point) { this._endPoint = value }

	// Get / set the floorplan's WallReshapingTool
	get wallReshapingTool() { return this._wallReshapingTool; }
	set wallReshapingTool(value: WallReshapingTool) { this._wallReshapingTool = value }

	// Start transaction, capture the mouse, use a crosshair cursor
	public doActivate (): void {
	    this.endPoint = null;
	    this.startTransaction(this.name);
	    this.diagram.isMouseCaptured = true;
	    let diagram: go.Diagram = this.diagram;
	    const tool = this;

	    // update wallThickness, based on the current value of the HTML input element 
	    // pre-condition: diagram.floorplanUI exists
	    /*if (diagram.floorplanUI) {
	        var el = document.getElementById(diagram.floorplanUI.state.wallThicknessInputId);
	        if (isNaN(el.value) || el.value === null || el.value === '' || el.value === undefined) el.value = diagram.convertPixelsToUnits(5);
	        diagram.model.setDataProperty(diagram.model.modelData, "wallThickness", diagram.convertUnitsToPixels(parseFloat(el.value)));
	    }
	    else*/ 
	    let fp: Floorplan = <Floorplan>diagram;
	    diagram.model.setDataProperty(diagram.model.modelData, "wallThickness", fp.convertUnitsToPixels(parseFloat("5")));

	    // assign startpoint based on grid
	    let point1: go.Point = tool.diagram.lastInput.documentPoint;
	    let gs: number = diagram.model.modelData.gridSize;
	    if (!(tool.diagram.toolManager.draggingTool.isGridSnapEnabled)) gs = 1;
	    let newx: number = gs * Math.round(point1.x / gs);
	    let newy: number = gs * Math.round(point1.y / gs);
	    let newPoint1: go.Point = new go.Point(newx, newy);
	    this.startPoint = newPoint1;

	    this.wallReshapingTool = <WallReshapingTool>tool.diagram.toolManager.mouseDownTools.elt(3);
	    // Default functionality:
	    this.isActive = true;
	}

	public doMouseDown() {
		const diagram: go.Diagram = this.diagram;
	    const tool = this;
	    tool.diagram.currentCursor = 'crosshair';
	    let data = { key: "wall", category: "WallGroup", caption: "Wall", type: "Wall", startpoint: tool.startPoint, endpoint: tool.startPoint, thickness: parseFloat(diagram.model.modelData.wallThickness), isGroup: true, notes: "" };
	    this.diagram.model.addNodeData(data);
	    let wall: go.Group = <go.Group>diagram.findPartForKey(data.key);
	    let fp: Floorplan = <Floorplan>diagram;
	    fp.updateWall(wall);
	    let part: go.Part = diagram.findPartForData(data);
	    // set the TransactionResult before raising event, in case it changes the result or cancels the tool
	    tool.transactionResult = tool.name;
	    diagram.raiseDiagramEvent('PartCreated', part);

	    // start the wallReshapingTool, tell it what wall it's reshaping (more accurately, the shape that will have the reshape handle)
	    tool.wallReshapingTool.isEnabled = true;
	    diagram.select(part);
	    tool.wallReshapingTool.isBuilding = true;
	    tool.wallReshapingTool.adornedShape = <go.Shape>part.findObject("SHAPE");
	    tool.wallReshapingTool.doActivate();
	}

	// If user presses Esc key, cancel the wall building
	public doKeyDown(): void {
	    const fp: Floorplan = <Floorplan>this.diagram;
	    let e: go.InputEvent = fp.lastInput;
	    if (e.key === "Esc") {
	        let wall: go.Group = <go.Group>fp.selection.first();
	        fp.remove(wall);
	        fp.pointNodes.iterator.each(function (node) { fp.remove(node); });
	        fp.dimensionLinks.iterator.each(function (link) { fp.remove(link); });
	        fp.pointNodes.clear();
	        fp.dimensionLinks.clear();
	        this.doDeactivate();
	    }
	    go.Tool.prototype.doKeyDown.call(this);
	}

	// When the mouse moves, reshape the wall
	public doMouseMove(): void {
	    this.wallReshapingTool.doMouseMove();
	}

	// End transaction
	public doDeactivate(): void {
	    const diagram: go.Diagram = this.diagram;
	    this.diagram.currentCursor = "";
	    this.diagram.isMouseCaptured = false;

	    this.wallReshapingTool.isEnabled = false;
	    this.wallReshapingTool.adornedShape = null;
	    this.wallReshapingTool.doDeactivate();
	    this.wallReshapingTool.isBuilding = false;

	    let fp: Floorplan = <Floorplan>diagram;
	    fp.updateWallDimensions();

	    this.stopTransaction();
	    
	    this.isActive = false; // Default functionality
	}

}

export = WallBuildingTool;