// AppShowReportParams, AppHideReportParams, PageReportParams,
import { StatDefault } from '../../interface.uts'

import { AppShowParamsKeys, AppHideParamsKeys, PageShowParamsKeys, PushParamsKeys, EventParamsKeys, ErrorParamsKeys,CrashParamsKeys } from '../core/stat-type.uts'

/**
 * 序列化url参数
 * @param {Object} obj
 * @example  
 */
export function Serialize(obj : UTSJSONObject) : string {
	let str : string[] = [];
	for (let p in obj) {
		if (obj.hasOwnProperty(p)) {
			let key = obj[p]
			if (obj[p] == null) {
				key = ''
			}
			const text = p + "=" + key
			str.push(text);
		}
	}
	return "?" + str.join("&");
}

export function IsNumber(value : any | null) : boolean {
	return typeof value === 'number';
	// if (value.trim() === "") {
	// 	return false;
	// }
	// const num = parseInt(value);
	// if (isNaN(num)) {
	// 	return false;
	// }
	// return true;
}

/**
 * 上报参数过滤
 */
export function FilterParam<T>(keys : string[] = [], data : StatDefault) : T {
	let result = {};
	keys.forEach(key => {
		result[key] = data[key];
	});
	let formdata : T = result as T
	return formdata;
}


/**
 * 日志输出
 * @param {StatDefault} data 统计数据
 * @param {Boolean} type 打印类型，如果是type=true ，则是最终上报数据
 */
export function Log(data : StatDefault, type : Boolean = false) {

	let logData = getEventData(data.lt!, data)
	let msg_type : string = ''

	switch (data.lt) {
		case '1':
			msg_type = '应用启动'
			break
		case '3':
			msg_type = '应用进入后台'
			break

		case '11':
			msg_type = '页面切换'
			break
		case '21':
			msg_type = '事件触发'
			break
		case '31':
			msg_type = '应用错误'
			break
		case '41':
			msg_type = '应用崩溃'
			break
		case '101':
			msg_type = 'PUSH'
			break
	}

	if (type) {
		console.log(`=== 统计队列数据上报 :`, logData)
		// console.log(logData)
		// console.log(`=== 上报结束 ===`)
		return
	}

	if (msg_type != '') {
		console.log(`=== 统计数据采集：${msg_type} :`,logData)
		// console.log(logData)
		// console.log(`=== 采集结束 ===`)
	}
}

/**
 * map 转 string
 */
export function Map2String(statData : Map<string, StatDefault[]>) : string {
	let statDataJson = {}
	statData.forEach((rd : StatDefault[], key : string) => {
		statDataJson[key] = rd
	});
	return JSON.stringify(statDataJson)
}

export function Map2Json(statData : Map<string, StatDefault[]>) : UTSJSONObject {
	let statDataJson = {}
	statData.forEach((rd : StatDefault[], key : string) => {
		let arr : object[] = [];
		rd.forEach((elm : StatDefault) => {
			let data = getEventData(key, elm)

			arr.push(data)
		})

		statDataJson[key] = arr

	});

	return statDataJson
}



/**
 * 处理上报参数
 * @param {Object}  statData 需要处理的数据
 */
export const handle_data = (statData : Map<string, StatDefault[]>) : string => {
	let firstArr : any[] = []
	let contentArr : any[] = []
	let lastArr : any[] = []
	statData.forEach((rd : StatDefault[], key : string) => {
		rd.forEach((elm : StatDefault) => {
			let data = getEventData(key, elm)
			if (key == '1') {
				firstArr.push(data)
			} else if (key == '4') {
				lastArr.push(data)
			} else {
				contentArr.push(data)
			}
		})
	});
	firstArr.push(...contentArr, ...lastArr)
	return JSON.stringify(firstArr)
}

/**
 * 获取具体的上报参数对象
 */
function getEventData(lt : string, elm : StatDefault) : any {
	let data : any = {}
	switch (lt) {
		case '1'://应用启动
			data = FilterParam(AppShowParamsKeys, elm)
			break
		case '3': // 应用进入后台
			data = FilterParam(AppHideParamsKeys, elm)
			break
		case '11': //页面切换
			data = FilterParam(PageShowParamsKeys, elm)
			break
		case '21': // 事件触发
			data = FilterParam(EventParamsKeys, elm)
			break
		case '31': // 应用错误
			data = FilterParam(ErrorParamsKeys, elm)
			break
		case '41': // 崩溃
			data = FilterParam(CrashParamsKeys, elm)
			break
		case '101': // PUSH
			data = FilterParam(PushParamsKeys, elm)
			break
	}

	return data
}