// 对手发球的运动
var Bump = require("bump")

cc.Class({
    extends: cc.Component,

    properties: {
        g: {
            default: null,
            type: cc.Graphics,
        },

        bump: {
            default: null,
            type: Bump,
        },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.initValues();
    },

    // 初始化数据
    initValues:function() {
        // 球的状态信息
        this.ballconfig = require("ballconfig");
        // 球的动作

        // 发球开始的区间
        this.serveStartRight = 167;
        this.serveStartLeft = 105;

        // 发球右的区间
        this.serveRightLeft = 28;
        // 发球中的区间
        this.serveMidLeft = -10;
        // 发球左的区间
        this.serveLeftLeft = -100;

        // 配置方程式的各项数据
        // 发球时数据
        this.serveStartX = 135;
        this.serveStartY = 350;
        this.serveStartDown = -30;

        // 发球时右边位置数据
        this.serveRightX = 118;
        this.serveRightY = 285;
        this.serveRightDown = -45;

        // 发球时中间位置数据
        this.serveMidX = 118;
        this.serveMidY = 190;
        this.serveMidDown = -95;

        // 发球时左侧的位置数据
        this.serveLeftX = 0;
        this.serveLeftY = 18;
        this.serveLeftDown = -92;

        this.g.lineWidth = 4;
        this.g.fillColor.fromHEX('#ffffff');

        this.x = this.serveStartRight;
    },

    ballServe:function() {
        this.unschedule(this.ballUpdate);
        this.x = this.serveStartRight;
        this.schedule(this.ballUpdate, 1.0 / 60);
    },

    ballStop:function() {
        this.unschedule(this.ballUpdate);
    },

    ballUpdate:function (dt) {
        this.g.clear();

        var speed = 0;
        var state;
        if (this.x <= this.serveStartRight + 10 && this.x >= this.serveStartLeft) {
            speed = 1.309;
            state = this.ballconfig.BALL_STATE_SERVE_START;
        } else if (this.x < this.serveStartLeft  && this.x >= this.serveRightLeft) {
            speed = 12;
            state = this.ballconfig.BALL_STATE_SERVE_RIGHT;
        } else if (this.x < this.serveRightLeft && this.x >= this.serveMidLeft) {
            speed = 9;
            state = this.ballconfig.BALL_STATE_SERVE_MID;
        } else if (this.x < this.serveMidLeft && this.x >= this.serveLeftLeft) {
            speed = 8;
            state = this.ballconfig.BALL_STATE_SERVE_LEFT;
        } else {
            speed = 8;
            state = this.ballconfig.BALL_STATE_DROP_IN_MINE;
        }

        if (this.stateCallback != null) {
            // 给控制器传递发球左侧的状态
            this.stateCallback(state);
        }

        // 判断是否需要磕碰
        var next = this.x - speed;
        if (this.x > this.serveMidLeft && next < this.serveMidLeft) {
            this.bump.bump(cc.v2(this.x, this.serveEquation(this.x)));
        }

        this.x = next;

        this.g.lineWidth = 3;
        this.g.moveTo(this.x, this.serveEquation(this.x));
        this.g.bezierCurveTo(this.x, this.serveEquation(this.x), this.x + 5, this.serveEquation(this.x + 5), this.x + 10, this.serveEquation(this.x + 10));
        this.g.stroke();
    },

    // 发球时的方程式
    serveEquation:function(x) {
        // 判断各个不同的区间
        if (x <= this.serveStartRight + 10 && x >= this.serveStartLeft) {
            return (x * x - this.serveStartX * 2 * x + this.serveStartX * this.serveStartX) / this.serveStartDown + this.serveStartY;
        } else if (x < this.serveStartLeft  && x >= this.serveRightLeft) {
            return (x * x - this.serveRightX * 2 * x + this.serveRightX * this.serveRightX) / this.serveRightDown + this.serveRightY;
        } else if (x < this.serveRightLeft && x >= this.serveMidLeft) {
            return (x * x - this.serveMidX * 2 * x + this.serveMidX * this.serveMidX) / this.serveMidDown + this.serveMidY;
        }

        return (x * x - this.serveLeftX * 2 * x + this.serveLeftX * this.serveLeftX) / this.serveLeftDown + this.serveLeftY;
        // return 0;
    },

    // 绘制坐标线
    drawAxle:function() {
        this.g.lineWidth = 2;
        this.g.moveTo(-1000, 0);
        this.g.lineTo(1000, 0);
        this.g.stroke();

        this.g.moveTo(0, -1000);
        this.g.lineTo(0, 1000);
        this.g.stroke();
    },
});
