import { gameMethod } from "../common/gameMethod"
import GameDataCenter from "../data/GameDataCenter"
import { uiCommon } from "./UICommon"

// 创建微信用户授权按钮
export default class WechatUserBtn {
    // 创建
    static create(node: fgui.GObject, onUserClickFunction: (isSucc: boolean) => void): any {
        if (node == null || node.visible == false) { return null }
        let btn = null
        if (cc.sys.platform != cc.sys.WECHAT_GAME) { return null }
        if (GameDataCenter.plat.instance.hasAuthUserInfo) { return null }
        let param = uiCommon.getWxBtnPos(node)
        btn = wx.createUserInfoButton({
            type: "text",
            text: "",
            style: {
                left: param.left,
                top: param.top,
                width: param.width,
                height: param.height,
                lineHeight: param.height,
                backgroundColor: "#ff000000", // 00000000 ff0000
                color: "#00000000",
                textAlign: "center",
                fontSize: 16,
                borderRadius: 0,
            },
        });
        btn.onTap((res) => {
            if (res.errMsg == "getUserInfo:ok") {
                console.log("授权用户信息:", res)
                GameDataCenter.plat.getWechatUserInfo()
            } else {
                console.log("授权用户失败:", res)
                // this.userInfoBtn.hide() // 用户不授权,则不隐藏一直存在
            }
            onUserClickFunction(res.errMsg == "getUserInfo:ok" ? true : false)
        })
        return btn
    }
}