import { dbGet } from "./db"
import { PAGE_PVER_TIME, APP_PVER_TIME } from '../config.uts';
import { ResidenceTimeReturn } from '../../interface.uts'
import { dbSet, dbRemove } from "./db";

// 首次访问时间
const FIRST_VISIT_TIME_KEY = '__first__visit__time'
// 最后访问时间
const LAST_VISIT_TIME_KEY = '__last__visit__time'
// 访问总数
const TOTAL_VISIT_COUNT = '__total__visit__count'
const FIRST_TIME = '__first_time'

// 页面停留时间记录key
const PAGE_RESIDENCE_TIME = '__page__residence__time'
let First_Page_Residence_Time = 0
let Last_Page_Residence_Time = 0

/**
 * 获取当前时间
 */
export const get_time = () : number => {
  return Math.floor(new Date().getTime() / 1000)
}


/**
 * 设置页面首次访问时间，用户获取页面/应用停留时常
 */
export const set_first_time = () : number => {
	// 获取当前时间 ，以下代码获取到是毫秒级时间戳 ，实际上用到是秒级时间戳，所以需要除以1000
	// const time = new Date().getTime()
	let time = get_time()
	dbSet(FIRST_TIME, time)
	return time
}



/**
 * 获取首次访问时间
 */
export const get_first_visit_time = () : number => {
	const timeStorge = dbGet<number>(FIRST_VISIT_TIME_KEY)
	let time : number
	if (timeStorge != null && timeStorge != 0) {
		time = timeStorge as number
	} else {
		time = get_time()
		dbSet(FIRST_VISIT_TIME_KEY, time)
		// 首次访问需要 将最后访问时间置 0
		dbRemove(LAST_VISIT_TIME_KEY)
	}
	return time
}

/**
 * 最后访问时间
 */
export const get_last_visit_time = () : number => {
	const timeStorge = dbGet<number>(LAST_VISIT_TIME_KEY)
	let time = 0
	if (timeStorge != null && timeStorge != 0) {
		time = timeStorge as number
	}
	
	dbSet(LAST_VISIT_TIME_KEY, get_time())
	return time
}


/**
 * 获取总访问次数
 */
export const get_total_visit_count = () : number => {
	const timeStorge = dbGet<number>(TOTAL_VISIT_COUNT)
	let count = 1
	if (timeStorge != null) {
		count = timeStorge as number
		count++
	}
	dbSet(TOTAL_VISIT_COUNT, count)
	return count
}

/**
 * 获取页面 \ 应用停留时间
 */
export const get_residence_time = (type : string) : ResidenceTimeReturn => {
	let residenceTime = 0
	const last_time = get_time()
	const first_time = (dbGet(FIRST_TIME) ?? last_time) as number
	if (first_time != 0) {
		residenceTime = last_time - first_time
	}
	// 将毫秒级时间戳转换为秒级时间戳，因为直接获取的是秒级时间戳，所以不需要转换
	// residenceTime = parseInt(residenceTime / 1000)
	residenceTime = residenceTime < 1 ? 1 : residenceTime
	let timeData : ResidenceTimeReturn = {
		residenceTime: residenceTime,
		overtime: false,
	}
	if (type === 'app') {
		let overtime = residenceTime > APP_PVER_TIME ? true : false
		timeData.overtime = overtime
		return timeData
	}
	if (type === 'page') {
		let overtime = residenceTime > PAGE_PVER_TIME ? true : false
		timeData.overtime = overtime
		return timeData
	}
	return timeData
}

/**
 * 设置页面停留时间
 */
export const set_page_residence_time = () : number => {
	First_Page_Residence_Time = get_time()
	dbSet(PAGE_RESIDENCE_TIME, First_Page_Residence_Time)
	return First_Page_Residence_Time
}

/**
 * 获取页面停留时间
 */
export const get_page_residence_time = () : number => {
	Last_Page_Residence_Time = get_time()
	First_Page_Residence_Time = dbGet<number>(PAGE_RESIDENCE_TIME) ?? 0
	const diff : number = Last_Page_Residence_Time - First_Page_Residence_Time
	return diff
}
