

import ShareCanvas from "./ShareCanva";
import { Main } from "../Main";
export default class JionTool {

    private constructor() {
    }

    private static _wx: any
    private static shareCanvas: ShareCanvas;
    public static Init() {
        if(!Main.OnWx)return;
        this._wx = Laya.Browser.window.wx;
        this.setWH();
        this.shareCanvas = new ShareCanvas();
        this.shareCanvas.InitData();
    }

    public static showFriendRank(keyName: string="score") {
        if (Main.OnWx)
            this.postMessage(keyName, "show_friend_rank");
    }

    public static showWorldRank(keyName: string, data: any) {
        if (Main.OnWx)
            this.postMessage(keyName, "show_world_rank", data);
    }

    public static upPage() {
        if (Main.OnWx)
            this.postMessage("", "show_up_page");
    }

    public static nextPage() {
        if (Main.OnWx)
            this.postMessage("", "show_next_page");
    }

    public static settlement(value: number,callBack:Function=null,keyName: string="score" ) {
        if (Main.OnWx)
            this.setUserCloudStorage(keyName, value.toString(),callBack);
    }

    public static showShareCanvas(){
        if (Main.OnWx)
            this.shareCanvas.show();
    }
    public static hideShareCanvas(){
        if (Main.OnWx)
        this.shareCanvas.hide();
    }
    public static sendMoveDelta(delta: number){
        if (Main.OnWx)
            this.postMessage("", "move_rank", delta);
    }

    public static updateCtx() {
        if (Main.OnWx) this.postMessage("", "update_ctx");
    }

    private static setWH() {
        if (!Main.OnWx)return;
        let width =  Laya.stage.width;
        let height = Laya.stage.height;
        let data = { width: width, height: height }
        console.error(width, height);
        this.postMessage("", "canvase_width_height", data);
    }
    private static postMessage(keyName: string, action: string, data?: any) {
        this._wx.postMessage({
            keyName: keyName,
            action: action,
            data: data
        });
    }

    private static setUserCloudStorage(keyName: string, value: string,callBack:Function=null) {
        let obj = [{
            key: keyName,
            value: value
        }];
        this._wx.setUserCloudStorage({
            KVDataList: obj,
            success: function (res) {
                console.log("数据上传成功"+res);
                if(callBack){
                    callBack();
                }
            },
            fail: function (res) {
                console.log("数据上传失败"+res);
                if(callBack){
                    callBack();
                }
            }
        });
    }
}
