// 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;
        //     }
        // },
        top_camera: {
            default: null,
            type: cc.Camera
        },
        main_camera: {
            default: null,
            type: cc.Camera
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        var self =this;
        // console.log(cc.HQ.PlayerMgr.info);
        this.node.getChildByName("name").getComponent(cc.Label).string = cc.HQ.PlayerMgr.info.name;
        // cc.loader.load(cc.HQ.PlayerMgr.info.pic,cc.SpriteFrame,function (err, spriteFrame) {
        //     if (err) {
        //         cc.error(err.message || err);
        //         return;
        //     }
        //     self.node.getChildByName("head_img").getComponent(cc.Sprite).spriteFrame = spriteFrame;
        // });
        this.showSpHead(self.node.getChildByName("img_mask").getChildByName("head_img"),cc.HQ.PlayerMgr.info.pic);
        this.node.getChildByName("score").getComponent(cc.Label).string = cc.sys.localStorage.getItem("top_score");
    },

    start () {

    },

    close_xuanyao(){
        console.log("close xuanyao");
        this.top_camera.active = false;
        this.node.active = false;
        // return false;
    },
    open_xuanyao(){
        // this.node.active = false;
        // cc.director.loadScene("jietu");

        var self = this;
        // self.main_camera.active = true;
        this.node.active = true;
        self.top_camera.active = true;
        // setTimeout(function(){
            sdk.Screenshot(self.top_camera,(d)=>{
                // self.main_camera.active = false;
                self.top_camera.active = false;
                self.node.active = false;
                console.log("自动关闭");
            });
        // },1000)
        

    },
    //显示玩家头像
    showSpHead: function(node,avatarUrl){
        if (CC_WECHATGAME) {
            try {
                let image = wx.createImage();
                image.onload = () => {
                    try {
                        let texture = new cc.Texture2D();
                        texture.initWithElement(image);
                        texture.handleLoadedTexture();
                        node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                    } catch (e) {
                        cc.log(e);
                        node.active = false;
                    }
                };
                image.src = avatarUrl;
            }catch (e) {
                cc.log(e);
                node.active = false;
            }
        } else {
            cc.loader.load({
                url: avatarUrl, type: 'jpg'
            }, (err, texture) => {
                node.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
            });
        }
    },

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