'use strict';

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

/**
 * @class Rectangle
 * @augments Thing
 * @param {number} width - Desired width of resulting rectangle.
 * @param {number} height - Desired height of resulting rectangle.
 */
function Rectangle(width, height){
    if (typeof width != "number" || typeof height != "number") {
        throw new TypeError('You must pass two numbers to <span class="code">new Rectangle(width, height)</span>');
    }
    Thing.call(this);
    this.width = width;
    this.height = height;
    this.type = "Rectangle";
}

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

/**
 * Draws the rectangle in the canvas.
 * @param {CodeHSGraphics} __graphics__ - Instance of the __graphics__ module.
 */
Rectangle.prototype.draw = function(__graphics__) {
    var context = __graphics__.getContext();
    context.fillStyle = this.color.toString();

    if(this.hasBorder){
        context.lineWidth = this.lineWidth;
        context.strokeStyle = this.stroke.toString();
    }

    context.beginPath();
    context.rect(this.x,this.y,this.width, this.height);
    context.closePath();

    if(this.hasBorder){
        context.stroke();
    }

    context.fill();
};

/**
 * Checks if the passed point is contained in the rectangle.
 * @param {number} x - The x coordinate of the point being tested.
 * @param {number} y - The y coordinate of the point being tested.
 * @returns {boolean} Whether the passed point is contained in the rectangle.
 */
Rectangle.prototype.containsPoint = function(x, y) {
    return x >= this.x && x <= this.x + this.width &&
           y >= this.y && y <= this.y + this.height;
};


/**
 * Gets the width of the rectangle.
 * @returns {number} Width of the rectangle.
 */
Rectangle.prototype.getWidth = function() {
    return this.width;
};

/**
 * Gets the height of the rectangle.
 * @returns {number} Height of the rectangle.
 */
Rectangle.prototype.getHeight = function() {
    return this.height;
};

module.exports = Rectangle;
