"use strict";
cc._RF.push(module, 'dc2c1dEKyNBJ79z1JWGLjJR', 'rank');
// rank.js

"use strict";

var groupCloudStorage = [];
var showfriendlist = false;
var friendCloudStorage = [];

// 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: {
        scrollview: {
            default: null,
            type: cc.ScrollView
        },

        scrollbg: {
            default: null,
            type: cc.Node
        },

        layer: {
            default: null,
            type: cc.Layout
        }
    },

    /**
    * {
    *  avatarUrl:"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLlK8ljt32foehDttWGarn7iacbVAc3FrjhquCOaFmiboIPWe3MXEUSrfQEIQg0ynKhD5BeXgVpKFhw/132"
    nickname:"乚"
    openid:"o4u4u5V9P1Xbd4pZoDKYse2GFNOM"
    KVDataList:Array(2)
    0:{key: "token", value: "60ee473ca8a72de455ee080b20b4c7cf"}
    1:{key: "highscore", value: "72"}
    * }
    * 
    * 
    * 
    */

    loadPic: function loadPic(spr, data) {
        var avatarUrl = data.avatarUrl;
        var image = wx.createImage();
        image.src = avatarUrl;
        image.onload = function () {
            var texture = new cc.Texture2D();
            texture.initWithElement(image);
            texture.handleLoadedTexture();
            spr.spriteFrame = new cc.SpriteFrame(texture);
            console.log("load!!!!!!!!!!");
        };
    },
    initItem: function initItem(node, data, rkk) {
        node.active = true;
        var child1 = node.getChildByName("rank");
        var rk = child1.getComponent(cc.Label);
        rk.string = rkk;
        var child2 = node.getChildByName("icon");
        var icon = child2.getComponent(cc.Sprite);
        //this.loadPic(icon,data);

        var child3 = node.getChildByName("name");
        child3.x = -92;
        var name = child3.getComponent(cc.Label);
        name.string = data.nickname;

        var child4 = node.getChildByName("score");
        var score = child4.getComponent(cc.Label);
        score.string = data.KVDataList[0].value;
    },
    loadItems: function loadItems() {
        var arr = this.scrollbg.children;
        var arr1 = [];
        arr1[0] = { key: "highscore", value: "109" };

        friendCloudStorage[0] = { openid: "o4u4u5V9P1Xbd4pZoDKYse2GFNOM", nickname: "乚",
            avatarUrl: "https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLlK8ljt…hquCOaFmiboIPWe3MXEUSrfQEIQg0ynKhD5BeXgVpKFhw/132",
            KVDataList: arr1 };
        var rankarr = friendCloudStorage;
        var len = rankarr.length;
        var h = 0;
        var hh = 0;
        console.log("ggggggggggggggggggg");
        console.log(rankarr);
        for (var i = 0; i < arr.length; i++) {
            var item = arr[i];
            var sz = item.getContentSize();
            var y = -h - 40;
            h += sz.height;
            item.y = y;
            //test
            if (i < /*rankarr.length*/10 && len > 0) {
                hh = h;
                this.initItem(item, rankarr[0], i + 1);
            } else {
                item.active = false;
            }
        }
        if (this.scrollbg.height < hh) {
            this.scrollbg.height = hh;
        }
    },
    TouchEvent: function TouchEvent(event, customEventData) {
        var self = this;
        if (event.type == cc.Node.EventType.TOUCH_END) {
            if (customEventData == "sharerank") {
                this.layer.node.active = true;
                this.layer.node.scale = 1.0;
                //utiljs.shareScore("好惊喜,我在疯狂插刀中排行:No."+utiljs.myrank,this);
            }
        }
    },


    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start: function start() {
        this.onListen();
        console.log("rank start>>>>>>>>>>>>>>>>>>>>");
        var listbg = this.layer.node.getChildByName("listbg");
        var winsz = cc.winSize;
        var scales = cc.winSize.width / 640;
        listbg.scale = scales;

        this.loadItems();
    },
    update: function update(dt) {
        if (showfriendlist) {
            console.log("hhhhhhhhhhhhhhhhhhhhhhhhh");
            this.loadItems();
            showfriendlist = false;
        }
    },
    getFriendCloudStorage: function getFriendCloudStorage() {
        console.log("22222222");
        var is = cc.sys.platform == cc.sys.WECHAT_GAME;
        if (is) {
            console.log("444444444");
            wx.getFriendCloudStorage({
                keyList: ['highscore', "token"],
                success: function success(res) {
                    console.log(res);
                    var data = res.data;
                    var len = data.length;
                    for (var i = 0; i < len; i++) {
                        friendCloudStorage[i] = data[i];
                    }
                    showfriendlist = true;
                },
                fail: function fail(res) {
                    console.log(res);
                }

            });
        }
    },
    getGroupCloudStorage: function getGroupCloudStorage(ticket) {
        var is = cc.sys.platform == cc.sys.WECHAT_GAME;
        if (is) {
            wx.getGroupCloudStorage({
                shareTicket: ticket,
                keyList: ['highscore', "token"],
                success: function success(res) {
                    console.log(res);
                    var data = res.data;
                    var len = data.length;
                    for (var i = 0; i < len; i++) {
                        groupCloudStorage[i] = data[i];
                    }
                    showfriendlist = true;
                },
                fail: function fail(res) {
                    console.log(res);
                }
            });
        }
    },
    getUserCloudStorage: function getUserCloudStorage() {
        var self = this;
        var is = cc.sys.platform == cc.sys.WECHAT_GAME;
        if (is) {
            wx.getUserCloudStorage({
                keyList: ['highscore', "token"],
                success: function success(res) {
                    console.log(res);
                    var data = res.data;
                    for (var i = 0; i < friendCloudStorage.length; i++) {
                        if (data.token == friendCloudStorage[i].value) {
                            self.getFriendCloudStorage();
                            //friendCloudStorage[i] = data;
                            break;
                        }
                    }

                    for (var i = 0; i < groupCloudStorage.length; i++) {
                        if (data.token == groupCloudStorage[i].value) {
                            //groupCloudStorage[i] = data;
                            self.getGroupCloudStorage();
                            break;
                        }
                    }
                },
                fail: function fail(res) {
                    console.log(res);
                }
            });
        }
    },
    onListen: function onListen() {
        var self = this;
        var is = cc.sys.platform == cc.sys.WECHAT_GAME;
        if (is) {
            wx.onMessage(function (data) {
                console.log(data);
                var recvdata = data.data;
                console.log(recvdata);
                var flag = recvdata.flag;
                if (flag == "getFriendsData") {
                    self.getFriendCloudStorage();
                } else if (flag == "getGroupData") {
                    var ticket = recvdata.ticket;
                    self.getGroupCloudStorage(ticket);
                } else if (flag == "getUserData") {
                    self.getUserCloudStorage();
                }
            });
        }
    }
});

cc._RF.pop();