/*
 * @Descripttion: 
 * @version: 1.0.0
 * @Author: YeeChan
 * @Date: 2020-07-23 16:59:58
 */
import RYSDK from "./RYSDK";
import { TimerUtils } from "../Mgr/TimerUtils";

export enum RYSTAT_Event {
    RYSTAT_E_INIT_custom = "init",
    RYSTAT_E_LOGIN_custom = "login",
    RYSTAT_E_ENTRY_SOURCE_custom = "entrysource",
    RYSTAT_E_CLICK_AD_custom = "clickad",
    RYSTAT_E_CLICK_ALLOW_custom = "clickad",
}

export default class RYSTAT {
    public static readonly domain_custom

    public SendEvent_custom(event: string, data?: object | string, s?: Function, f?: Function) {
        if (null != RYSDK.Instance_custom.rysdk_custom && null != RYSDK.Instance_custom.rysdk_custom.ry_SendEvent) {
            RYSDK.Instance_custom.rysdk_custom.ry_SendEvent(event, data, null, s, f);
        }
    }

    protected reportInitFail_custom() {
        if (null != RYSDK.Instance_custom.rysdk_custom && null != RYSDK.Instance_custom.rysdk_custom.ry_ReportFail) {
            RYSDK.Instance_custom.rysdk_custom.ry_ReportFail();
        }
    }

    public reportInit_custom() {
        let tryCounter = 0;
        let s = () => { console.log("init 上报成功!!"); }
        let f = () => {
            console.log("init 上报失败!!");
            if (tryCounter >= 10) {
                console.log("init 上报重试次数超过 10次,放弃上报");
                this.reportInitFail_custom();
            }
            else {
                ++tryCounter;
                TimerUtils.once_custom(() => {
                    this.SendEvent_custom(RYSTAT_Event.RYSTAT_E_INIT_custom, null, s, f);
                    console.log("init 第 " + tryCounter + " 次重新上报");
                }, 5)
                // Laya.timer.once(5000, this, () =>  {
                //     this.SendEvent(RYSTAT_Event.RYSTAT_E_INIT,null,s,f);
                //     console.log("init 第 " + tryCounter +" 次重新上报");
                // })
            }
        }
        this.SendEvent_custom(RYSTAT_Event.RYSTAT_E_INIT_custom, null, s, f);
    }

    public reportLogin_custom() {
        this.SendEvent_custom(RYSTAT_Event.RYSTAT_E_LOGIN_custom);
    }

    public reportEntrySource_custom() {
        this.SendEvent_custom(RYSTAT_Event.RYSTAT_E_ENTRY_SOURCE_custom);
    }

    public reportClickAd_custom(adv_id: string) {
        this.SendEvent_custom(RYSTAT_Event.RYSTAT_E_CLICK_AD_custom,
            {
                adv_id: adv_id,
                timelog: Date.now(),
                type: 0
            });
    }

    public reportClickAdAllow_custom(adv_id: string) {
        this.SendEvent_custom(RYSTAT_Event.RYSTAT_E_CLICK_ALLOW_custom,
            {
                adv_id: adv_id,
                timelog: Date.now(),
                type: 1
            });
    }
}