import { Recommend } from "../Push/RecommendView";
import { ViewManager } from "../View/ViewManager";
import ChoutiPanView from "../Push/ChoutiPanView";

// import { Main } from "../Main";
// import ListItem from "../GameTools/ListItem";
// import { PanelManager, PanelType } from "../View/PanelManager";
// import GuangGao from "./GuangGao";
// import { WXSDK } from "./WXSDK";

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

    //是否为白名单用户
    public static iswhite:boolean;
    //是否为新用户
    public static isnewuser:boolean;

    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();
        // let white = Laya.LocalStorage.getItem("is_white");
        // if(white){
        //     FYHD_Sdk.iswhite = true;
        // }
        // else{
        //     FYHD_Sdk.getWhiteConfig();
        // }

        // let newuser = Laya.LocalStorage.getItem("is_newuser");
        // if(!newuser){
        //     FYHD_Sdk.isnewuser = true;
        //     Laya.LocalStorage.setItem("is_newuser","1");
        // }
        // else{
        //     FYHD_Sdk.isnewuser = false;
        // }
        // FYHD_Sdk.PushData();
    }

    //用户授权
    public static UserInfoAuth() {
        if (!Laya.Browser.onWeiXin) 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));
        });
          
    }
    private static getWhiteConfig(){
        Laya.Browser.window.sdk.getFyhd().whiteConfig(function (e) {
            FYHD_Sdk.iswhite = e.data.is_white;
            if(FYHD_Sdk.iswhite){
                Laya.LocalStorage.setItem("is_white","1")
            }
            console.log("白名单---",e);
        });
    }

    public static SetHuTuiList(list: Laya.List, scrol: number = 1, type: string = "") { //scrol 1:竖的  2横的
        let arr = this.pushData;
        if (!arr || arr.length == 0) return;
        list.selectEnable = false;
        scrol == 1 ? list.vScrollBarSkin = "" : list.hScrollBarSkin = "";
        list.renderHandler = Laya.Handler.create(this, this.updateItem, [type], false);
        list.array = arr;
        list.refresh();
    }
    private static updateItem(type: string, cell: Laya.Box, index: number) {
        // var aditem = cell.getComponent(ListItem) as ListItem;
        // aditem.Refresh(type);
    }


    //点击事件上报
    public static ClickAd(ad_type: string) {
        console.log("上报类型---",ad_type);
        if (!Laya.Browser.onWeiXin) return;
        var param = {
            ad_type: ad_type,
            //此处添加openid
            openid:FYHD_Sdk.openid
        }
        console.log("点击参数:" + param);
        Laya.Browser.window.sdk.getFyhd().advertisement(param, (data) => {
            console.log("点击结果:" + JSON.stringify(data));
        });
    }

   //互推信息
    public static PushData() {
        //console.log("pushdata--",Main.WX);
        if (!Laya.Browser.onWeiXin) return;
        var param = {};
        Laya.Browser.window.sdk.getFyhd().shareRecommend(param, (data) => {
            console.log("热门游戏数据--",data)
            if (data.data) {
                FYHD_Sdk.pushData = data.data;
                FYHD_Sdk.pushData.sort(FYHD_Sdk.ListSort);
                FYHD_Sdk.isHaveDate = true;
                //此处派发事件，展示互推
                //EventManager.event(FYHD_Sdk.ShowAd);
            }
            console.log("获取互推信息:" + JSON.stringify(FYHD_Sdk.pushData));
            new Recommend();
        });
    }
    public static ShowHuTuiJump(data,open) {
        if (!Laya.Browser.onWeiXin) 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);
                },
                fail(res){
                    if(open){
                        ViewManager.Instance.add( ChoutiPanView );
                    }
                }
            })
        }
        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);
                },
                fail(res){
                    if(open){
                        ViewManager.Instance.add( ChoutiPanView );
                    }
                }
            })
        }
    }
    //互推点击
    public static PushClick(to_appid) {
        if (!Laya.Browser.onWeiXin) 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;
        }
    }
}