import { reactive } from "vue";

export const FROM_PAGES_UNITTEST = "from.pages.unitTest"; // 从 单元测试 返回 岛 的路由参数
export const FROM_PAGES_SELECTGRADE = "from.pages.selectGrade"; // 从年级学期选择 跳转 岛 的路由参数

export function useStudyRouteParams() {
	const STUDY_ROUTE_PARAMS = 'STUDY_ROUTE_PARAMS';

	function setStudyStorage({
		nianji, // 年级
		xueqi, // 学期 
		zhangId, // 章Id
		jieId, // 节Id
		nextZhangId, // 下一章Id
	}) {
		uni.setStorageSync(STUDY_ROUTE_PARAMS, JSON.stringify(Object.assign({}, {
			nianji: null,
			xueqi: null,
			zhangId: null,
			jieId: null,
			nextZhangId: null,
		}, {
			nianji,
			xueqi,
			zhangId,
			jieId,
			nextZhangId,
		})));
	}

	function getStudyStorage() {
		const data = uni.getStorageSync(STUDY_ROUTE_PARAMS);
		return data ? JSON.parse(data) : data;
	}

	function removeStudyStorage() {
		uni.getStorageSync(STUDY_ROUTE_PARAMS)
	}

	return {
		setStudyStorage,
		getStudyStorage,
		removeStudyStorage
	}
}


const PAGE_TABBAR_KEY = "TAB_BAR";
const History_Obj = reactive({
	study: null,
	logout: null
})
export function useTabBarHistory () {
	function setTabBarStorage(data) {
		uni.setStorageSync(PAGE_TABBAR_KEY,JSON.stringify(data));
	}
	
	function getTabBarStorage() {
		const data = uni.getStorageSync(PAGE_TABBAR_KEY);
		return data ? JSON.parse(data): data;
	}
	
	function removeTabBarStorage() {
		uni.removeStorageSync(PAGE_TABBAR_KEY)
	}
	
	function setTabBarStorageCustom(page,callback) {
		const data = getTabBarStorage(page)
		function done(data) {
			setTabBarStorage(data)
		}
		if (data) {
			callback(data,done);
		}
	}
	
	return {
		setTabBarStorage,getTabBarStorage,removeTabBarStorage
	}
}