import AppPlatform from "../Util/AppPlatform";
import { LogUtils } from "../Util/LogUtils";
import EventMgr from "../Event/EventMgr";
import { ryw_Event } from "../Event/EventEnum";


/**
 * 框架的统一事件,游戏事件自行定义在自己代码中,这里只管框架的
 */
enum StatsFrameWorkEvent {

    ReportAdClickSuccess_custom = "ReportAdClickSuccess",//点击广告成功
    ReportAdClickFail_custom = "ReportAdClickFail",//点击广告失败
    ReportAdClickStart_custom = "ReportAdClickStart",//点击广告
    ReportLaunchOptions_custom = "ReportLaunchOptions",//场景值ip
    LogReportInfo_custom = "LogReportInfo",//上报日志
    LogReportError_custom = "LogReportError",//上报的异常日志
    LoginReportInfo_custom = "LoginReportInfo",//登录日志
    GameEventLVInto_custom = "Event_LVInto", //关卡进入人数    level
    GameEventLVFinish_custom = 'Event_LVFinish', //完成关卡    level status    关卡状态    字符    win,lose   duration

    Event_Commerce_custom = "Event_Commerce",//商业行为
    Event_Custom_custom = "Event_Custom",//个性事件


}


/*
 * @Author: YeeChan
 * @Date: 2020-11-24 19:30:12
 * @Description: 统计管理器
 */
export class UmengMgr {



    /**
     *  统计事件
     *  仅统计事件,无属性时,使用如下方法:StatsMgr.sendEvent("事件ID")
     *  统计带属性的事件时,使用如下方法:
     *      StatsMgr.sendEvent('事件ID', { '属性1':'属性值1','属性2':'属性值2' }); // 字符型属性值
     *      StatsMgr.sendEvent('事件ID', { 'PayAmount':6999 });// 数值型属性值
     * @param event 事件 ID
     * @param params 属性 
     */
    public static sendEvent_custom(event: string, params?: { [key: string]: string | number }) {
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            UmengMgr.uma_trackEvent(event, params);
        } else if (AppPlatform.is_Iphone_custom() || AppPlatform.is_Android_custom()) {//ios
            // FMApp.uma_trackEvent_custom(event, params);
        }
        EventMgr.emitEvent_custom(ryw_Event.ryw_Umeng_custom, { event: event });
    }

    /**
     * 场景值 IP 地区
     * @param scene 
     * @param dqip 
     * @param ipxq 
     */
    public static sendReportLaunchOptions_custom(scene: string, dqip: string, ipxq: string) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.ReportLaunchOptions_custom, {
            "scene": scene,
            "dqip": dqip,
            "ipxq": ipxq,
        })
    }


    /**
     * 导出 成功
     * @param title 
     * @param appid 
     */
    public static sendReportAdClickSuccess_custom(title: string, appid: string | number) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.ReportAdClickSuccess_custom, {
            "title": title,
            "appid": String(appid)
        })
    }


    /**
     * 导出 失败
     * @param title 
     * @param appid 
     */
    public static sendReportAdClickFail_custom(title: string, appid: string | number) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.ReportAdClickFail_custom, {
            "title": title,
            "appid": String(appid)
        })
    }

    /**
     * 点击广告
     * @param title 
     * @param appid 
     */
    public static sendReportAdClickStart_custom(title: string, appid: string | number) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.ReportAdClickStart_custom, {
            "title": title,
            "appid": String(appid)
        })
    }


    /**
     * 关卡进入统计
     * @param level 关卡id
     */
    public static sendGameEventLvInto_custom(level: string) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.GameEventLVInto_custom, {
            "level": level
        })

        //关卡开始
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            window["wx"].uma.stage.onStart({
                stageId: level + '',//该字段为固定字段不可修改,必传
                stageName: '第' + level + '关',// 付费金额 (该字段必传) 格式:数值型,number;请注意值格式,传错不能进行金额计算
            })
        }
    }

    /**
     * 完成关卡的统计 统计  没有胜负的游戏,均提交win
     * @param level 关卡id
     * @param isWin 是否胜利 true/false
     * @param duration 关卡时长(秒)
     * @param custom 自定义参数
     *          isHome:0没有 1返回
     *          
     */
    public static sendGameEventLvFinish_custom(level: string, isWin: boolean, duration: number, custom?: { [key: string]: string | number }) {
        let status = "win";
        if (!isWin) {
            status = "lose";
        }
        let params = {};
        params["level"] = level;
        params["status"] = status;
        params["duration"] = duration;
        if (custom) {
            for (const key in custom) {
                if (Object.prototype.hasOwnProperty.call(custom, key)) {
                    const element = custom[key];
                    if (key == "status") {
                        if (element) {
                            params["status"] = "home";
                        }
                    } else {
                        params[key] = element;
                    }
                }
            }
        }
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.GameEventLVFinish_custom, params)

        //关卡结束
        if (AppPlatform.is_WECHAT_GAME_custom()) {
            let _um_sdu = 0
            if (duration) {
                _um_sdu = duration * 1000;//秒转毫秒
            }
            window["wx"].uma.stage.onEnd({
                stageId: level + '',//该字段为固定字段不可修改,必传
                stageName: '第' + level + '关',// 付费金额 (该字段必传) 格式:数值型,number;请注意值格式,传错不能进行金额计算
                event: isWin ? "complete" : "fail",//关卡完成/关卡失败)
                _um_sdu: _um_sdu //关卡耗时(毫秒)
            })
        }
    }


    /**
     * 商业行为
     */
    public static sendEvent_Commerce_custom(params: { [key: string]: string | number }) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.Event_Commerce_custom, params)
    }
    /**
     * 个性事件
     */
    public static sendEvent_Custom_custom(params: { [key: string]: string | number }) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.Event_Custom_custom, params)
    }


    /**
     * 上报日志
     * @param info 具体的内容
     */
    public static sendLogReportInfo_custom(info: string) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.LogReportInfo_custom, {
            "info": info
        })
    }

    /**
     * 上报的异常日志
     * @param info 具体的内容
     */
    public static sendLogReportError_custom(info: string) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.LogReportError_custom, {
            "info": info
        })
    }

    /**
     * 上报登录日志
     * @param type 平台登录/登录/用户数据
     * @param state 0成功,1网络异常,2业务异常
     * @param info 具体的内容
     */
    public static sendLoginReportInfo_custom(type: string, state: number, info: string) {
        UmengMgr.sendEvent_custom(StatsFrameWorkEvent.LoginReportInfo_custom, {
            "type": type,
            "state": state,
            "info": info,
        })
    }


    /**
     * 友盟的统计
     * @param event 
     * @param params 
     */
    private static uma_trackEvent(event: string, params?: { [key: string]: string | number }) {
        if (AppPlatform.is_WECHAT_GAME_custom()) {//需要增加合法域名 https://umini.shujupie.com
            if (window["wx"].uma) {
                window["wx"].uma.trackEvent(event, params);
            } else {
                LogUtils.error_custom("统计事件 无友盟");
            }
        }
    }

}