// 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;
        //     }
        // },

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

        friendRank:{
            default:null,
            type:cc.Node
        },
        totalRank:{
            default:null,
            type:cc.Node
        },

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

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

        rankingScrollView: cc.Sprite,

        shareDialog:cc.Node,
        
        loginPage:cc.Node,

        audioManager:cc.Node,

        shareUrls:[],
        shareTitle:[],
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.loginPage.active = Global.showLoginPage;
        this.audioManager.getComponent("AudioMng").stopAll();

        this.shareUrls[0]='http://pub.dwstatic.com/wxgame/dwgame01/wxshare/share1.jpg';
        this.shareUrls[1]='http://pub.dwstatic.com/wxgame/dwgame01/wxshare/share2.jpg';
        this.shareUrls[2]='http://pub.dwstatic.com/wxgame/dwgame01/wxshare/share3.jpg';
        this.shareUrls[3]='http://pub.dwstatic.com/wxgame/dwgame01/wxshare/share1.jpg';
        this.shareUrls[4]='http://pub.dwstatic.com/wxgame/dwgame01/wxshare/share2.jpg';
        this.shareUrls[5]='http://pub.dwstatic.com/wxgame/dwgame01/wxshare/share3.jpg';

        this.shareTitle[0] = '这游戏怎么一玩上就停不下来呢！';
        this.shareTitle[1] = '点一下～玩一年～皮肤不用一分钱！';
        this.shareTitle[2] = '这游戏怎么一玩上就停不下来呢！';
        this.shareTitle[3] = '点一下～玩一年～皮肤不用一分钱！';
        this.shareTitle[4] = '这游戏怎么一玩上就停不下来呢！';
        this.shareTitle[5] = '点一下～玩一年～皮肤不用一分钱！';

        if(Global.showTotalRank){
            this.goTotalRank();
            Global.showTotalRank = false;
        }

        if(Global.showFriendRank){
            this.goFriendRank();
            Global.showFriendRank = false;
        }

    },

    start () {

    },

    goMainMenu(){
        this.mainMenu.active = true;
        this.friendRank.active = false;
        this.totalRank.active = false;
        this.bottleStore.active = false;
        this.arrowStore.active = false;
    },

    goArrowStore(){
        this.mainMenu.active = false;
        this.friendRank.active = false;
        this.totalRank.active = false;
        this.bottleStore.active = false;
        this.arrowStore.active = true;

    },

    goBottleStore(){
        this.mainMenu.active = false;
        this.friendRank.active = false;
        this.totalRank.active = false;
        this.bottleStore.active = true;
        this.arrowStore.active = false;
    },

    goFriendRank(){
        this.mainMenu.active = false;
        this.friendRank.active = true;
        this.totalRank.active = false;
        this.bottleStore.active = false;
        this.arrowStore.active = false;
        if (window.wx != undefined) {
            // 发消息给子域
            window.wx.postMessage({
                messageType: 1,
                MAIN_MENU_NUM: "score"
            });
        } else {
            cc.log("获取好友排行榜数据。score");
        }
    },

    goTotalRank(){
        this.mainMenu.active = false;
        this.friendRank.active = false;
        this.totalRank.active = true;
        this.bottleStore.active = false;
        this.arrowStore.active = false;
    },

    shareGame(){
        // wx.request()
        let title = this.shareTitle[Math.floor(Math.random()*6)];
        let imageUrl = this.shareUrls[Math.floor(Math.random()*6)];
        this.shareDialog.active = true;
        this.shareDialog.getComponent('ShareModal').button.on('touchend',  ()=> {
            this.shareDialog.active = false;
            if(wx!=undefined){
                console.log('share uid: '+Global.user.uid);
                wx.shareAppMessage({
                    title: title,
                    imageUrl: imageUrl,
                    query:"uid="+Global.user.uid
                })
            }
        }, this);
    },

    startGame(){
        cc.director.loadScene("Game");
    },

    friendButtonFunc(event) {
        if (window.wx != undefined) {
            // 发消息给子域
            window.wx.postMessage({
                messageType: 1,
                MAIN_MENU_NUM: "score"
            });
        } else {
            cc.log("获取好友排行榜数据。score");
        }
    },

    groupFriendButtonFunc: function (event) {
        if (window.wx != undefined) {
            window.wx.shareAppMessage({
                success: (res) => {
                    if (res.shareTickets != undefined && res.shareTickets.length > 0) {
                        window.wx.postMessage({
                            messageType: 5,
                            MAIN_MENU_NUM: "score",
                            shareTicket: res.shareTickets[0]
                        });
                    }
                }
            });
        } else {
            cc.log("获取群排行榜数据。score");
        }
    },

    start () {
        if (window.wx != undefined) {
            // window.wx.showShareMenu({withShareTicket: true});//设置分享按钮，方便获取群id展示群排行榜
            this.tex = new cc.Texture2D();
            // var openDataContext = wx.getOpenDataContext();
            // var sharedCanvas = openDataContext.canvas;
            // if (sharedCanvas) {
            //     sharedCanvas.width = cc.game.canvas.width * 2;
            //     sharedCanvas.height = cc.game.canvas.height * 2;
            // }
            window.sharedCanvas.width = 1334;
            window.sharedCanvas.height = 750;
            // window.wx.postMessage({
            //     messageType: 3,
            //     MAIN_MENU_NUM: "score",
            //     score: 10
            // });
        }
    },
    
    _updateSubDomainCanvas () {
        if (!this.tex) {
            return;
        }
        this.tex.initWithElement(sharedCanvas);
        this.tex.handleLoadedTexture();
        this.rankingScrollView.spriteFrame = new cc.SpriteFrame(this.tex);
    },
    update(dt) {
        this._updateSubDomainCanvas();
    },

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