import UserModel from "./UserModel";
import { Main } from "../Main";
import { EventManager } from "./EventManager";

export class FYHD_Sdk {
    public static Ad_ClickType = {
        Video: "video_click",
        Watch: "watch",
        Share: "share",
        Banner: "banner",
        Push: "push"
    }

    public static pushData = [];
    private static openid: string;

    public static isHaveDate = false;

    public static ShowAd = "ShowAd";
    //初始化sdk
    public static InitSdk() {
        Laya.Browser.window.sdk.getFyhd().startSdk();
        FYHD_Sdk.UserInfoAuth();
    }

    //用户授权
    public static UserInfoAuth() {
        if (!Main.IsWx) return;
        Laya.Browser.window.sdk.getFyhd().getFyhdUserInfo(null, false, (data) => {
            console.log("无授权回调:" + JSON.stringify(data));
            if (data.result.user) {
                FYHD_Sdk.openid = data.result.user.openid;
                FYHD_Sdk.AuthInfo();
            }
        });
    }
    private static AuthInfo() {
        var args = {
            openid: FYHD_Sdk.openid,
        }
        Laya.Browser.window.sdk.getFyhd().loginData(args, (data) => {
            console.log("授权上报回调:" + JSON.stringify(data));
            FYHD_Sdk.ValueUser();
        });
    }
    private static ValueUser() {
        var args = {
            openid: FYHD_Sdk.openid,
        }
        Laya.Browser.window.sdk.getFyhd().valueuser(args, (data) => {
            console.log("有效用户上报:" + JSON.stringify(data));
        });
    }

    //创角色
    public static RoleData() {
        if (!Main.IsWx) return;
        var args = {
            openid: FYHD_Sdk.openid,
            role_id: UserModel.Instance.CurModel.userid,
            create_time: UserModel.Instance.CurModel.ServerTime
        }
        Laya.Browser.window.sdk.getFyhd().roleData(args, (data) => {
            console.log("创建角色:" + JSON.stringify(data));
            FYHD_Sdk.Login();
        });
    }
    //登录信息上报
    public static Login() {
        if (!Main.IsWx) return;
        var openid = FYHD_Sdk.openid ? FYHD_Sdk.openid : UserModel.Instance.CurModel.openid;
        var args = {
            openid: openid,
            role_id: UserModel.Instance.CurModel.userid,
            create_time: UserModel.Instance.CurModel.ServerTime.toString()
        }
        console.log("登录参数:" + JSON.stringify(args));
        Laya.Browser.window.sdk.getFyhd().uploginData(args, (data) => {
            console.log("登录结果:" + JSON.stringify(data));
            // FYHD_Sdk.PushData();
        });
    }

    //点击事件上报
    public static ClickAd(ad_type: string) {
        if (!Main.IsWx) return;
        var param = {
            ad_type: ad_type,
        }
        console.log("点击参数:" + param);
        Laya.Browser.window.sdk.getFyhd().advertisement(param, (data) => {
            console.log("点击结果:" + JSON.stringify(data));
        });
    }

    public static PushData() {
        if (!Main.IsWx) return;
        var param = {};
        Laya.Browser.window.sdk.getFyhd().shareRecommend(param, (data) => {
            if (data.data) {
                FYHD_Sdk.pushData = data.data;
                FYHD_Sdk.pushData.sort(FYHD_Sdk.ListSort);
                EventManager.event(FYHD_Sdk.ShowAd);
                FYHD_Sdk.isHaveDate = true;
            }
            console.log("获取互推信息:" + JSON.stringify(FYHD_Sdk.pushData));
        });
    }
    public static ShowHuTuiJump(data) {
        if (!Main.IsWx) return;
        if (data.jump_type == 2) //二维码跳转方法
        {
            Laya.Browser.window.wx.previewImage({ current: data.qrcode, urls: [data.qrcode] });
        }
        else if (data.jump_type == 1) //直跳方法
        {
            Laya.Browser.window.wx.navigateToMiniProgram({
                appId: data.to_appid,
                path: data.jump_path,
                success(res) {
                    // 打开成功
                    console.log("上报成功:" + JSON.stringify(res));
                    FYHD_Sdk.PushClick(data.to_appid);
                }
            })
        }
        else if (data.jump_type == 0)//平台跳转方法
        {
            Laya.Browser.window.wx.navigateToMiniProgram({
                appId: data.platform_appid,
                path: data.jump_path,
                success(res) {
                    // 打开成功
                    console.log("上报成功:" + JSON.stringify(res));
                    FYHD_Sdk.PushClick(data.to_appid);
                }
            })
        }
    }
    public static PushClick(to_appid) {
        if (!Main.IsWx) return;
        var param = {
            openid: FYHD_Sdk.openid,
            to_appid: to_appid,
        }
        Laya.Browser.window.sdk.getFyhd().sharePlay(param, (data) => {
            console.log("点击上报结果:" + JSON.stringify(data));
        });
    }

    private static ListSort(a, b): number {
        if (Number(a["panel"]) < Number(b["panel"])) {
            return -1;
        }
        if (Number(a["panel"]) == Number(b["panel"])) {
            return 0;
        }
        if (Number(a["panel"]) > Number(b["panel"])) {
            return 1;
        }
    }
}