// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        this.left = UtilsNode.getNode("left",this.node)
        //右面玩家的图标
        this.right = UtilsNode.getNode("right",this.node)
        //匹配图标
        this.default = UtilsNode.getNode("default",this.node)
        this.PinkFrameFillColor = UtilsNode.getNode("PinkFrameFillColor",this.default)


        this.right.active = false
        this.default.active = false

        if (this.LoadSucceedLishener) {
            this.LoadSucceedLishener();
        }

    },

    setLoadSucceedLishener(LoadSucceedLishener){
        this.LoadSucceedLishener = LoadSucceedLishener
    },


    setHeadData(type,data){
        var headNode
        if (type == 0) {
            headNode =  this.left
        }else{
            headNode =  this.right
        }

        var BlueFrameNew = UtilsNode.getNode("BlueFrameNew",headNode)
        var mask = UtilsNode.getNode("mask",BlueFrameNew)
        var AvatarMale = UtilsNode.getNode("AvatarMale",mask)

        var name = UtilsNode.getNode("name",headNode)

        utils.loadHttpSpriteFrame(data.head,AvatarMale.getComponent(cc.Sprite))
        name.getComponent(cc.Label).string = data.name


        var sex = UtilsNode.getNode("sex",headNode)
        var Male = UtilsNode.getNode("Male",sex)
        var Female = UtilsNode.getNode("Female",sex)
        if (data.gender == 0) {
            Male.active = true
            Female.active = false
        }else{
            Male.active = false
            Female.active = true
        }

    },



    //开始匹配
    startMatch(){
        this.default.active = true
        this.right.active = false
        this.readyRight()
    },
    //取消匹配
    cancelMatch(){
        this.default.active = false
        this.right.active = false
        this.unschedule(this.callback)
    },
    //完成匹配
    succeedMatch(){
        this.default.active = false
        this.right.active = true

        this.unschedule(this.callback)
    },
    //匹配动画
    readyRight: function () {

        var left = UtilsNode.getNode("left", this.PinkFrameFillColor);
        var middle = UtilsNode.getNode("middle", this.PinkFrameFillColor);
        var right = UtilsNode.getNode("right", this.PinkFrameFillColor);

        var left_Position = left.getPosition();
        var middle_Position = middle.getPosition();
        var right_Position = right.getPosition();

        // var arrp = [];
        // arrp.push(left_Position);
        // arrp.push(middle_Position);
        // arrp.push(right_Position);
        var arrp = [];
        arrp.push(right_Position);
        arrp.push(middle_Position);
        arrp.push(left_Position);

        var arrn = [];
        arrn.push(left);
        arrn.push(middle);
        arrn.push(right);

        this.count = 0;
        this.unschedule(this.callback)
        this.callback = function () {

            var temp = arrp[arrp.length - 1];
            arrp.unshift(temp);
            arrp.pop()

            // console.log("操作",arrn);
            if (this.count == arrp.length) {
                // 在第六次执行回调时取消这个计时器
                // this.unschedule(this.callback);
                this.count = 0;
            }
            for (var i = 0; i < arrp.length; i++) {
                arrn[i].x = arrp[i].x;
            }


            this.count++;

        }
        this.schedule(this.callback, 0.5);
    },

    // update (dt) {},
});
