import { _decorator, Component, log, Node, sys } from 'cc';
import { statistics_collect_rewardVideo_data_item } from '../data/server_play_list_data';
import { config } from '../config';
import { gameManager } from '../gameManager';
import { http } from '../http/http';
const { ccclass, property } = _decorator;

@ccclass('StatisticsManager')
export class StatisticsManager extends Component {

    private static _instance: StatisticsManager;

    public static get instance() {
        if (this._instance) {
            return this._instance;
        }

        this._instance = new StatisticsManager();
        return this._instance;
    }

    /**
     * 获取收集数据
     * 
     * @param {number} category_id
     * @param {number} level_id
     * @param {number} coin_number
     * @param {number} actionType
     * @returns
     * @memberof StatisticsManager
     */
    public static get_collect_data(category_id: number, level_id: number, coin_number: number, ad_res:any, actionType: number): statistics_collect_rewardVideo_data_item {
        if(sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
            return null
        }
        if(gameManager.havNoAllAd) { 
            return null
        }
        if (actionType == config.STATISTICS_ACTION_TYPE.UNKNOWN) {
            log('统计失败 - 未设置统计类型')
            return null
        }

        let collect_data = new statistics_collect_rewardVideo_data_item()
        collect_data.user_id = gameManager.userInfo.id
        collect_data.category_id = category_id
        collect_data.level_id = level_id
        if(category_id > 0) {
            collect_data.level_number = gameManager.get_cur_level_by_categoryid(category_id).level
        }
        collect_data.coin = gameManager.userInfo.coin + coin_number
        collect_data.ads_status = ad_res.isEnded ? 1 : 0
        if(ad_res.isEnded==false) {
            if(ad_res.errorString==undefined||ad_res.errorString==null) {
                collect_data.ads_remarks = '未观看完视频广告'
            } else {
                collect_data.ads_remarks = ad_res.errorString
            }
        }
        collect_data.ads_action = actionType
        return collect_data
    }

    /**
       * 请求收集激励视频广告数据
       * 
       * @param {statistics_collect_rewardVideo_data_item} data_item
       * @param {Function} call_back
       * @returns
       * @memberof StatisticsManager
       */

    public static request_collect_rewardVideoData(data_item: statistics_collect_rewardVideo_data_item, call_back: any = null) {
        if(sys.platform != sys.Platform.BYTEDANCE_MINI_GAME) {
            return
        }
        if(gameManager.havNoAllAd) { 
            return
        }
        if (data_item == null) {
            return
        }
        if (data_item.ads_action == config.STATISTICS_ACTION_TYPE.UNKNOWN) {
            console.log('统计失败 - 未设置统计类型')
            return
        }
        // console.log('收集激励视频数据: ', data_item)

        let data_string = JSON.stringify(data_item)

        let request_data: any = null
        if (sys.platform == sys.Platform.BYTEDANCE_MINI_GAME) {
            request_data = { 'adsdata': data_string }
        } else {
            let form_Data = new FormData();
            form_Data.append('adsdata', data_string)
            request_data = form_Data
        }
        
        http.post_statistics(config.statistics_url.rewardVideo_data, request_data, (_err, c_data) => {
            console.log('_err=', _err, 'c_dat=', c_data)
            if (call_back != null) {
                call_back()
            }
        })
    }

}

