
import HttpUtil from '../crypto/HttpUtil';
import GameData from '../GameData';




var shareConfig = {
    gameId: "100001",
    shareLine: "zDLsruVI",
    EK:"hui231%1"
  };

// 定义微信配置数据的接口
interface IWeChatConfig {
    appId: string;
    timestamp: number;
    nonceStr: string;
    signature: string;
    jsApiList: [];
}

// 微信操作类
export class WeChat {
    static setShare(url) {
        var urlTemp = this.removeQueryParams(url);
        shareConfig.shareLine = urlTemp;
        WeChat.getSignature(url);
    }

    static getResult(res){
        if(res){
            var data = res.data;
            wx.config({
                debug: false,
                appId: data.appId,
                timestamp: data.timestamp,
                nonceStr: data.nonceStr,
                signature: data.signature,
                jsApiList: ['onMenuShareTimeline','updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareAppMessage']
            });

            wx.checkJsApi({
                jsApiList: ['updateAppMessageShareData'], // 需要检测的JS接口列表，所有JS接口列表见附录2,
                success: function(res) {
                    console.log("检查api",res);
                // 以键值对的形式返回，可用的api值true，不可用为false
                // 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
                }
  
            });

            setTimeout(() => {  
                WeChat.changeShare();
            }, 100);
            setTimeout(() => {
                WeChat.changeShare();
            }, 200);
        }

    }

    static changeShare(){
        wx.ready(() => {
            wx.updateAppMessageShareData({ 
                title: '手眼协调练习', // 分享标题
                desc: '脑雾导致你的手眼协调变慢和估测不准吗？', // 分享描述
                link: shareConfig.shareLine, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: 'https://static.sparkus.cn/public/flyup.jpg', // 分享图标
                success: function () {
                  // 设置成功
                  console.log("分享好友成功回调");
                }
            });
            wx.updateTimelineShareData({ 
                title: '手眼协调练习', // 分享标题
                link: shareConfig.shareLine, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: 'https://static.sparkus.cn/public/flyup.jpg', // 分享图标
                success: function () {
                  // 设置成功
                  console.log("分享朋友圈成功回调");
                }
              })
        }); 
    }

    static getSignature(url: string): Promise<IWeChatConfig> {
        return new Promise((resolve) => {
            WeChat.getShareInfo((encodeURIComponent(url)),WeChat.getResult);
        });
    }
    static async getShareInfo(shareUrl: string, callback:Function): Promise<any> {
        const time = Math.floor((new Date().getTime()) / 1000)
        const url = HttpUtil.apiSign(`/api/share/cfg?gameId=${GameData._instance.GM_INFO.gameId}&time=${time}&url=${shareUrl}`,{})
        return HttpUtil.httpGet(url,callback)
    }

    
    static containsNanana(str) {
        return /test/i.test(str);
    }

    static removeQueryParams(url) {
        return url.replace(/\?.*$/, '');
    }

}
