// 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;
        //     }
        // },
        itemTemplate: { // item template to instantiate other items
            default: null,
            type: cc.Node
        },
        content:cc.Node,
        totalRankUrl:"https://api-touhu.duowan.com/user/getAllRank.do",
        timeline:"0",
        count:10,
        // item:[],
        // ver:100,
        // os:1,
        // channel:"wexin",
        // hasMore:true,

        spacing:0,
        current:0,
        userInfo:null,
        mine:null,
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        
    },

    onEnable(){
        this.users = [];
        this.items = [];
        this.current = 0;
        this.timeline = "0";
        // this.hasMore = true;
        this.getTotalRank(Global.user);
    },
    
    onDisable(){

    },
    
    getTotalRank:function(userInfo){
        console.log(userInfo.token)
        console.log(userInfo.uid)
        this.userInfo = userInfo;
        if(wx!=undefined){
            // if(this.hasMore){
                wx.showLoading({
                    mask:true,
                });
                wx.request({
                    url:this.totalRankUrl,
                    data:{
                        token:userInfo.token,
                        uid:userInfo.uid,
                        channel:Global.channel,
                        os:Global.os,
                        ver:Global.ver,
                        timeline:this.timeline,
                        count:this.count
                    },
                    success:(response)=>{
                        console.log(response);
                        if(response.data.code==-5){
                            wx.showToast({
                                title:'登录过期'
                            })
                            Global.showLoginPage = true;
                            cc.director.loadScene('MainMenu');
                        }
                        var data =response.data.data;
                        if(data!=undefined){
                            console.log(this.users)
                            this.users = this.users.concat(data.list);
                            this.mine = data.me;
                            this.fillMine();
                            this.notifyData();
                        }else{
                            wx.showToast({
                                title:"网络错误",
                            })
                        }
                    },
                    fail:()=>{
                        wx.showToast({
                            title:"网络错误",
                        })
                    },
                    complete:()=>{
                        wx.hideLoading();
                    }
                })
            // }
        }
    },

    fillMine(){
        var mineItem = this.itemTemplate.getComponent('Item');
        mineItem.updateItem(this.mine);
        if(this.mine.rank==0){
            mineItem.outSide();
        }
    },

    notifyData(){
        let start = this.current;
        let end = this.users.length-start>5?start+5:this.users.length;
        let users = this.users.slice(start,end);
        let offset = this.content.height/2-20;
        this.content.removeAllChildren();
        console.log(users);
        for (let i = 0; i < users.length; ++i) {
            let item = cc.instantiate(this.itemTemplate);
            this.content.addChild(item);
            item.getComponent('Item').isSelf = false;
            item.setPosition(0, offset-item.height * (0.5 + i) - this.spacing * (i + 1));
            let user = this.users[this.current+i];
            item.getComponent('Item').updateItem(user,i);
    	}
    },

    previousPage(){
        if(this.current>0){
            let start = this.current - 5;
            this.current = start;
            this.notifyData();
            
        }else{
            wx.showToast({
                title:'已经是第一页了～'
            }) 
        }
    },

    nextPage(){
        let start = this.current + 5;
        if(start >= this.users.length){
            // if(this.hasMore){
            //     this.current = start;
            //     this.getTotalRank(Global.user);
            // }else{
                wx.showToast({
                    title:'没有下一页了～'
                })
            // }
        }else{
            this.current = start;
            this.notifyData();
        }
    }
});
