'use strict';

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

/**
 * @class Polygon
 * @augments Thing
 * @param {number} width - Desired width of resulting rectangle.
 * @param {number} height - Desired height of resulting rectangle.
 */
function Polygon(width, height){
    Thing.call(this);
    this.points = [];
}

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

/**
 * Draws the polygon in the canvas.
 * @param {CodeHSGraphics} __graphics__ - Instance of the __graphics__ module.
 */
Polygon.prototype.draw = function(__graphics__){
    if(this.points.length === 0) return;

    var context = __graphics__.getContext();
    context.fillStyle = this.color.toString();
    context.beginPath();

    var first = this.points[0];
    context.moveTo(first.x, first.y);
    for(var i = 1; i < this.points.length; i++){
        var cur = this.points[i];
        context.lineTo(cur.x, cur.y);
    }
    context.closePath();
    context.fill();
};

/**
 * Checks if the passed point is contained in the polygon.
 * @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 polygon.
 */
Polygon.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.
 */
Polygon.prototype.getWidth = function(){
    return this.width;
};

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

/**
 * Adds a vertex to the polygon.
 * @param {number} x - The x coordinate of the desired new vertex.
 * @param {number} y - The y coordinate of the desired new vertex.
 */
Polygon.prototype.addPoint = function(x, y){
    this.points.push({x:x, y: y});
};

module.exports = Polygon;
