
cc.Class({
    extends: cc.Component,

    properties: {        
        rankPanel:cc.Node,
        monthRankContent:cc.Node,
        legendRankContent:cc.Node,
        crownFrame:[cc.SpriteFrame],
        numberFrame:[cc.SpriteFrame],
        rankList:[cc.Prefab],
        myRank:cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        var self = this
        this.monthRankContent.removeAllChildren()
        this.legendRankContent.removeAllChildren()
        
        // cc.loader.load("https://dn-qn.move-it.club/wxid_2557875578912_1480663047113_4.png",  function (err, frame) {  
        //     cc.log(new cc.SpriteFrame(frame))
        //     self.myRank.getChildByName('mask').getChildByName('head').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(frame)
        // })
        window.eventJsBridge.getRankingList(1)
        var str = window.rankingResponse
            var myRank = str.myRanking
            var rankingList = str.rankingList
            self.myRank.getChildByName('name').getComponent(cc.Label).string = myRank.nickName
            self.myRank.getChildByName('score').getComponent(cc.Label).string = myRank.score+'分'
            self.myRank.getChildByName('rank').getComponent(cc.Label).string = myRank.rank
            cc.loader.load(myRank.portraits, function (err, frame) {  
                self.myRank.getChildByName('mask').getChildByName('head').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(frame)
            })
            console.log(rankingList)
            for(var m=0;m<rankingList.length-1;m++){
                for(var n=0;n<rankingList.length-1;n++){
                    var namenum = parseInt(rankingList[n].rank)
                    var namenum1 = parseInt(rankingList[n+1].rank)
                    if(namenum>namenum1){
                        var rankList = rankingList[n]
                        rankingList[n] = rankingList[n+1]
                        rankingList[n+1] = rankList
                    }
                }
            }
            for(var i=0;i<rankingList.length;i++){
                var rankList = cc.instantiate(this.rankList[i%2])
                rankList.getChildByName('name').getComponent(cc.Label).string = rankingList[i].nickName
                rankList.getChildByName('score').getComponent(cc.Label).string = rankingList[i].score+'分'
                rankList.getChildByName('rank').getComponent(cc.Label).string = rankingList[i].rank
                cc.loader.load(rankingList[i].portraits, function (err, frame) {  
                    self.myRank.getChildByName('mask').getChildByName('head').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(frame)
                })
                rankList.parent = this.monthRankContent
                if(i<3){
                    rankList.getChildByName('crown').active = true
                    rankList.getChildByName('number').active = true
                    rankList.getChildByName('crown').getComponent(cc.Sprite).spriteFrame = this.crownFrame[i]
                    rankList.getChildByName('number').getComponent(cc.Sprite).spriteFrame = this.numberFrame[i]
                    rankList.getChildByName('num').active = false
                    rankList.getChildByName('rank').active = false
                }
                else{
                    rankList.getChildByName('crown').active = false
                    rankList.getChildByName('number').active = false
                    rankList.getChildByName('num').active = true
                }
            }
        
    },
    gameRank(){
        this.rankPanel.active = true
    },

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