import CanvaRenderMgr from "./CanvaRenderMgr";
import { Main } from "../Main";

export default class CanvaRenderController {
    public  CanvaInit() {
        if(!Main.IsWx)return;
        this.weixin = Laya.Browser.window.wx;
        this.shezhiWH();
        CanvaRenderMgr.Get_Instance().ChuShiHUa();
    }
    private static instance: CanvaRenderController;
    public  rightPage() {
        this.sendMessage("", "rightPage");
}

    public static get Instance(): CanvaRenderController {
        return CanvaRenderController.instance || (CanvaRenderController.instance = new CanvaRenderController());
    }
    private  weixin: any
 
    private  shezhiWH() {
        let width =  Laya.stage.width;
        let height = Laya.stage.height;
        let data = { width: width, height: height }
        this.sendMessage("", "canva_init", data);
    }
    public  xianshiFriend(keyName: string="score") {
            this.sendMessage(keyName, "show_friend_list");
    }
  

  
   
    public  xianshiCanva(){
        CanvaRenderMgr.Get_Instance().xianshi();
    }
    public  yincangCanva(){
        CanvaRenderMgr.Get_Instance().yincang();
    }
  

    private  sendMessage(keyName: string, action: string, data?: any) {
        this.weixin.postMessage({
            keyName: keyName,
            action: action,
            data: data
        });
    }
    public  shangchaunData(value: number,callBack:Function=null,keyName: string="score" ) {
        this.setUserCloudStorage(keyName, value.toString(),callBack);
}

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