import { Game } from "./Game.js";
import { Point } from "./Point.js";
import { Segment } from "./Segment.js";

class Player
{
    /**
     * @param {Number} id 
     * @param {Game} game 
     * @param {Segment} rail 
     * @param {Number} nb_goal 
     * @param {Number} position 
     * @param {Boolean} is_connected 
     */
    constructor(game, id, rail, nb_goal, position, is_connected)
    {
        /**
         * @type {Game}
         */
        this.game = game;

        /**
         * @type {Boolean}
         */
        this.is_connected = is_connected;

        /**
         * @type {Number}
         */
        this.id = id;

        /**
         * @type {Number}
         */
        this.position = position;

         /**
          * @type {Number}
          */
        this.nb_goal = nb_goal;
 
        /**
         * @type {Segment}
         */
        this.rail = rail === undefined ? new Segment() : rail;

        /**
         * @type {Number}
         */
        this.rail_size = Math.abs(this.rail.stop.x - this.rail.start.x) + Math.abs(this.rail.stop.y - this.rail.start.y);
 
        /**
         * @type {Number}
         */
        this.paddle_size = this.rail_size * this.game.config.paddle_ratio;
 
        /**
         * @type {Number}
         */
        this.diff_x = this.rail.stop.x - this.rail.start.x;

        /**
         * @type {Number}
         */
        this.diff_y = this.rail.stop.y - this.rail.start.y;
    }

    /**
     * 
     * @param {Number} new_position 
     */
    update_pos(new_position, time)
    {
        this.position = new_position;
    }

    /**
     * @param {CanvasRenderingContext2D} ctx
     */  
    draw(ctx)
    {
        if (this.is_connected === false)
        {
            ctx.moveTo(this.rail.start.x, this.rail.start.y);
            ctx.lineTo(this.rail.stop.x, this.rail.stop.y);
            return;
        }
        let paddle_pos = new Point(this.rail.start.x + this.diff_x * this.position,
                                   this.rail.start.y + this.diff_y * this.position);

        
        let start_x = paddle_pos.x - (this.diff_x * (this.paddle_size / 2 / this.rail_size)),
            start_y = paddle_pos.y - (this.diff_y * (this.paddle_size / 2 / this.rail_size)),
            stop_x = paddle_pos.x + (this.diff_x * (this.paddle_size / 2 / this.rail_size)),
            stop_y = paddle_pos.y + (this.diff_y * (this.paddle_size / 2 / this.rail_size));

        ctx.moveTo(start_x, start_y);
        ctx.lineTo(stop_x, stop_y);
    }

    from_json(data)
    {
        /**
        * @type {Boolean}
        */
        
        this.is_connected = data.is_connected;
        /**
         * @type {Number}
         */
        this.id = data.user_id;
        
        /**
         * @type {Number}
         */
        this.position = data.position.position ;

        /**
         * @type {Number}
         */
        this.nb_goal = data.nb_goal;

        /**
         * @type {Segment}
         */
        this.rail = this.rail.from_json(data.rail);

        /**
         * @type {Number}
         */
        this.rail_size = Math.abs(this.rail.stop.x - this.rail.start.x) + Math.abs(this.rail.stop.y - this.rail.start.y);

        /**
         * @type {Number}
         */
        this.paddle_size = this.rail_size * this.game.config.paddle_ratio;

        /**
         * @type {Number}
         */
        this.diff_x = this.rail.stop.x - this.rail.start.x;
        
        /**
         * @type {Number}
         */
        this.diff_y = this.rail.stop.y - this.rail.start.y;

        return this
    }
}

export { Player }