import { AccountManager } from "./AccountManager";
import { GameTools } from "../GameUtils/GameTools";


/*
* name;
*/
 export class ShareUtil
{
    public static Instance: ShareUtil = new ShareUtil();
    
    public static shareSuccessTime:number = 0;
    public static beginShareTime:number;
    public static  isSharing:boolean = false;
    public static callback;

    public beginShare(callBack):void
    {
          ShareUtil.beginShareTime = new Date().getTime();
        console.log("分享点击时间=" + ShareUtil.beginShareTime);
          ShareUtil.isSharing = true;
          ShareUtil.callback = callBack;
    }
    public onRegister():void
    {
         if(Laya.Browser.window.wx == null)
         {
             return;
         }
         Laya.Browser.window.wx.onShow(function(option)
         {
             console.log("onshow share start");
             if(ShareUtil.isSharing)
             {
                 ShareUtil.isSharing = false;
                 let time = new Date().getTime();
                   console.log("分享回到游戏时间" + time);
                 let wait = (time -  ShareUtil.beginShareTime)/1000;
                 console.log("分享停留时间为：" + wait);
                 let shareSuccess:boolean = false;
                 let realSuccess:boolean = false;

                 if(wait < 3)
                 {
                    Laya.Browser.window.wx.showToast( {title:"请重试一次"} );
                    ShareUtil.callback(false);
                 }
                 else
                 {
                     var random = Math.random();
                     if(random <= 0.3)
                     {
                        var message = ["你已分享过该群","请重新换个群分享！","请分享到30人大群中！"];
                        var curmes = message[GameTools.RandomANumber(0,2)];
                        Laya.Browser.window.wx.showToast( {title:curmes} );
                        ShareUtil.callback(false);
                     }
                     else
                     {
                        Laya.Browser.window.wx.showToast( {title:"分享成功!"} );
                        ShareUtil.callback(true);
                     }
                 }

                //  if(wait >= 2)
                //  {
                //     if(wait < 4)
                //     {
                //         shareSuccess = (Math.random() > 0.5)?false:true;
                //     }
                //     else
                //     {
                //         shareSuccess = true;
                //     }
                //  }
                //  else{
                //     shareSuccess = false;
                //  }
                //  if(shareSuccess == false)
                //  {
                //      Laya.Browser.window.wx.showToast( {title:"请重试一次"} );
                //      ShareUtil.callback(false);
                //  }
                //  else{
                //     Laya.Browser.window.wx.showToast( {title:"分享成功"} );
                //     ShareUtil.callback(true);
                //  }
             }
        });
    }

    public static ShareMessage() {
        if (!Laya.Browser.onWeiXin) return;
        var t: number = AccountManager.Instance.ServerTime;
        var _title = AccountManager.Instance.curplayerData.ShareTxt;
        _title = (_title == "" || !_title) ? "有人@你，快来比一比枪法" : _title;
        var _image = "subpackage/res/share/1.jpg";
        Laya.Browser.window.wx.onShareAppMessage(() => ({
            title: _title,
            imageUrl: _image,
            query: "shareid=" + AccountManager.Instance.curplayerData.userId + "_" + t + "_" + Math.floor(9999 * Math.random()).toString()
        }));
    }

    public static shareImage(channel: string, callback: Function = null, shareType: string): void {
        if (Laya.Browser.window.wx == null) {
            return;
        }
        var t: number = AccountManager.Instance.ServerTime;
        var _title = AccountManager.Instance.curplayerData.ShareTxt;
        _title = (_title == "" || !_title) ? "有人@你，快来比一比枪法" : _title;
        var _image =  "subpackage/res/share/1.jpg";
        // if (WxApi.sceneValue == "1037" || WxApi.sceneValue == "1058"|| WxApi.sceneValue == "1067" || WxApi.sceneValue == "1095") 
        // {
        //     _title = "2020全新工资走势，你跟上节奏了吗？";
        //     _image = "bg/share2.png";
        // }
        
        Laya.Browser.window.wx.aldShareAppMessage({
            title: _title,
            imageUrl: _image,
            query: "userId=" + AccountManager.Instance.curplayerData.userId + "_" + shareType + "_" + t + "_" + Math.floor(9999 * Math.random()).toString()// + "&shareTitle=" + _title,
        });
        if (callback) {
            ShareUtil.Instance.beginShare(callback);
        }
    }
}