// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class ranking extends cc.Component {

    @property(cc.Node)
    subContextView: cc.Node = null;
    @property(cc.Node)
    closeButton: cc.Node = null;
    @property(cc.Node)
    btnRanks: cc.Node = null;
    private isShow: boolean = true;
    // LIFE-CYCLE CALLBACKS:
    onLoad() {

    }
    start() {
    }

    showRanks() {
        this.btnRanks.active = true;

        if (typeof wx === 'undefined') {
            return;
        }
        // 设置容器可见
        this.subContextView.active = true;
        // 设置随机数(把这个当做玩家每局结算时的分数)
        let score = Math.round(Math.random() * 10);
        this.subContextView.getComponent(cc.SubContextView).updateSubContextViewport();
        setTimeout(() => {
            wx.getOpenDataContext().postMessage({
                message: score
            });
            this.subContextView.getComponent(cc.SubContextView).updateSubContextViewport();
        }, 200);
    };


    onRanksBtnClicked() {
        if (typeof wx === 'undefined') {
            this.showRanks(); // 非微信环境直接显示
            return;
        }

        // 直接尝试获取用户信息
        wx.getUserInfo({
            success: (res) => {
                console.log('用户已授权或刚刚授权', res.userInfo);
                this.showRanks(); // ✅ 授权成功，显示排行榜
                this.isShow = false;
            },
            fail: (err) => {
                console.log('用户拒绝授权', err);
                // 可选：提示用户必须授权才能查看排行榜
            }
        });
    }
    protected update(dt: number): void {
        // if (this.isShow) {
        //     this.onRanksBtnClicked();

        // }
    }

    closeRanks() {
        console.log('关闭排行榜');
        this.subContextView.getComponent(cc.SubContextView).updateSubContextViewport();
        this.closeButton.active = false;
        // 设置容器不可见，即关闭排行榜，并让开放域清空排名信息
        this.subContextView.active = false;
        wx.getOpenDataContext().postMessage({
            message: 'close'
        });

    }


    initUserInfoButton() {
        // 微信授权，此代码来自Cocos官方
        if (typeof wx === 'undefined') {
            return;
        }

        let systemInfo = wx.getSystemInfoSync();
        let width = systemInfo.windowWidth;
        let height = systemInfo.windowHeight;
        let button = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
                left: 0,
                top: 0,
                width: width,
                height: height,
                lineHeight: 40,
                backgroundColor: '#00000000',
                color: '#00000000',
                textAlign: 'center',
                fontSize: 10,
                borderRadius: 4
            }
        });

        button.onTap((res) => {
            if (res.userInfo) {
                // 可以在这里获取当前玩家的个人信息，如头像、微信名等。
                console.log('授权成功！');
                this.showRanks()
                this.isShow = true;
            }
            else {
                console.log('授权失败！');
                this.showRanks()
            }

            button.hide();
            button.destroy();
        });

    }

    // update (dt) {}
}
