'use strict';

var Thing = require('./thing.js');

/**
 * @class Line
 * @augments Thing
 * @param {number} x1 - x coordinate of starting point of line.
 * @param {number} y1 - y coordinate of starting point of line.
 * @param {number} x2 - x coordinate of end point of line.
 * @param {number} y2 - y coordinate of end point of line.
 */
function Line(x1, y1, x2, y2) {
    if (typeof x1 != 'number' || typeof y1 != 'number' || typeof x2 != 'number' || typeof y2 != 'number') {
        throw new TypeError('You must pass 4 numbers to <span class="code">new Line(x1, y1, x2, y2)</span>');
    }
    Thing.call(this);
    this.x1 = x1;
    this.y1 = y1;
    this.x2 = x2;
    this.y2 = y2;
    this.lineWidth = 2;
    this.type = 'Line';
}

Line.prototype = new Thing();
Line.prototype.constructor = Line;

/**
 * Sets the color of a line.
 * @param {Color} color - Sets the color of the line.
 */
Line.prototype.setColor = function(color) {
    this.stroke = color;
};

/**
 * Gets the color of a line.
 * @returns {Color} Color of the line.
 */
Line.prototype.getColor = function() {
    return this.stroke;
};

/**
 * Draws the line in the canvas.
 * @param {CodeHSGraphics} __graphics__ - Instance of the __graphics__ module.
 */
Line.prototype.draw = function(__graphics__) {
    var context = __graphics__.getContext();
    context.fillStyle = this.color.toString();
    context.beginPath();
    context.strokeStyle = this.stroke.toString();
    context.lineWidth = this.lineWidth;
    context.moveTo(this.x1, this.y1);
    context.lineTo(this.x2, this.y2);
    context.closePath();
    context.stroke();
};

/**
 * Checks if a given point is contained in the line.
 * Will always be false, just written as an override.
 * @param {number} x - x coordinate of the point being tested.
 * @param {number} y - y coordinate of the point being tested.
 */
Line.prototype.containsPoint = function(x, y) {
    return false;
};

/**
 * Returns the width of the line.
 * @returns {number} The width of the line.
 */
Line.prototype.getWidth = function() {
    return this.width;
};

/**
 * Returns the height of the line.
 * @returns {number} The width of the line.
 */
Line.prototype.getHeight = function() {
    return this.height;
};

/**
 * Sets the width of the line.
 * @param {number} width - The resulting width of the line.
 */
Line.prototype.setLineWidth = function(width) {
    if (typeof width != 'number') {
        throw new TypeError('You must pass a number to <span class="code">setLineWidth(width)</span>');
    }
    this.lineWidth = width;
};

/**
 * Sets the *starting* point of the line.
 * @param {number} x - The x coordinate of the resulting starting point.
 * @param {number} y - The y coordinate of the resulting starting point.
 */
Line.prototype.setPosition = function(x,y) {
    if (typeof x != 'number' || typeof y != 'number') {
        throw new TypeError('You must pass 2 numbers to <span class="code">setPosition(x, y)</span>');
    }
    this.x1 = x;
    this.y1 = y;
};

/**
 * Sets the *ending* point of the line.
 * @param {number} x - The x coordinate of the resulting ending point.
 * @param {number} y - The y coordinate of the resulting ending point.
 */
Line.prototype.setEndpoint = function(x, y) {
    if (typeof x != 'number' || typeof y != 'number') {
        throw new TypeError('You must pass 2 numbers to <span class="code">setEndpoint(x, y)</span>');
    }
    this.x2 = x;
    this.y2 = y;
};

/**
 * Moves the entire line.
 * @param {number} dx - The change in x coordinate of both starting and ending points.
 * @param {number} y - The change in y coordinate of both starting and ending points.
 */
Line.prototype.move = function(dx, dy) {
    if (typeof dx != 'number' || typeof dy != 'number') {
        throw new TypeError('You must pass 2 numbers to <span class="code">move(x, y)</span>');
    }
    this.x1 += dx;
    this.y1 += dy;
    this.x2 += dx;
    this.y2 += dy;
};

module.exports = Line;
