'use strict';

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

/**
 * @class Circle
 * @augments Thing
 * @param {number} radius - Desired radius
 */
function Circle(radius){
    if (typeof radius != 'number') {
        throw new TypeError('You must pass a number to the Circle constructor!');
    }
    Thing.call(this);
    this.radius = radius;
    this.color = Color.black;
    this.lineWidth = 3;
    this.type = "Circle";
}

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

/**
 * Draws the circle in the canvas.
 * @param {CodeHSGraphics} __graphics__ - Instance of the Graphics module.
 */
Circle.prototype.draw = function(__graphics__){
    var context = __graphics__.getContext();
    context.beginPath();

    if(this.hasBorder){
        context.strokeStyle = this.stroke.toString();
        context.lineWidth = this.lineWidth;
    }
    context.fillStyle = this.color.toString();
    context.arc(this.x,this.y,this.radius,0,Math.PI*2,true);
    context.closePath();

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

/**
 * Gets the radius of the circle
 * @returns {number} Radius of the circle.
 */
Circle.prototype.getRadius = function(){
    return this.radius;
};

/**
 * Gets the height (diamter) of the circle.
 * @returns {number} Height (diameter) of the circle.
 */
Circle.prototype.getHeight = function(){
    return this.radius * 2;
};

/**
 * Gets the width (diamter) of the circle.
 * @returns {number} Width (diameter) of the circle.
 */
Circle.prototype.getWidth = function(){
    return this.radius * 2;
};

/**
 * Sets the radius of the circle.
 * @param {number} radius - Desired resulting radius of the circle.
 */
Circle.prototype.setRadius = function(radius){
    if (typeof radius != 'number') {
        throw new TypeError('You must pass a number to <span class="code">setRadius(num)</span>');
    }
    radius = Math.max(0, radius);
    this.radius = radius;
};

/**
 * Checks if the passed point is contained in the circle.
 * @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 circle.
 */
Circle.prototype.containsPoint = function(x, y){
    var dist = __graphics__.getDistance(this.x, this.y, x, y);
    /* Must be _strictly_ less than to not return the cirlce itself */
    return dist < this.radius;
};

module.exports = Circle;
