import { WxApi } from "./Sdk";
import {GuangGao} from "./GuangGao";
import { GameUtils } from "../utils/GameUtils";
import { PlayerAccount } from "./PlayerAccount";

/*
* 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.onWeiXin)
         {
            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[GameUtils.randRangeint(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 = PlayerAccount.CurrentAccount.ServerTime;
        var _title = PlayerAccount.CurrentAccount.CurAccountData.sharecontent;
        _title = (_title == "" || !_title) ? "你能跟得上我的速度吗！？" : _title;
        var _image = PlayerAccount.CurrentAccount.CurAccountData.ShareImage;
        _image = (_image == "" || !_image) ? "texture/share.png" : _image;
        console.log("ShareMessage");
        Laya.Browser.window.wx.onShareAppMessage(() => ({
            title: _title,
            imageUrl: _image,
            query: "shareid=" + PlayerAccount.CurrentAccount.CurAccountData.UseId + "_" + 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;
        }
        if (!PlayerAccount.CurrentAccount.CurAccountData.share) {
            WxApi.ShowTxt("暂未开放");
            return;
        }
        
        var t: number = PlayerAccount.CurrentAccount.ServerTime;
        var _title = PlayerAccount.CurrentAccount.CurAccountData.sharecontent;
        _title = (_title == "" || !_title) ? "你能跟得上我的速度吗！？" : _title;
        var _image = PlayerAccount.CurrentAccount.CurAccountData.ShareImage;
        _image = (_image == "" || !_image) ? "texture/share.png" : _image;
        if (WxApi.sceneValue == "1037" || WxApi.sceneValue == "1058"|| WxApi.sceneValue == "1067" || WxApi.sceneValue == "1095") 
        {
            _title = "2020全新工资走势，你跟上节奏了吗？";
            _image = "texture/share.png.png";
        }
        

        Laya.Browser.window.wx.aldShareAppMessage({
            title: _title,
            imageUrl: _image,
            query: "userId=" + PlayerAccount.CurrentAccount.CurAccountData.UseId + "_" + shareType + "_" + t + "_" + Math.floor(9999 * Math.random()).toString()// + "&shareTitle=" + _title,
        });
        if (callback) {
            ShareUtil.Instance.beginShare(callback);
        }
    }
}