import {defineStore} from 'pinia';
import {reactive} from 'vue';
import { read_setting_data, recharge_list_data } from '../data/data';
import { config } from '../config/config';
import { util } from '../framework/util';
import { http } from '../framework/http';
export const ReadSetting = defineStore('read-setting',()=>{
	let data:read_setting_data = reactive(new read_setting_data())
	let recharge_data:recharge_list_data[] = reactive(null)
	async function updateReadSetting(d:read_setting_data){
		data.fontSizeIndex=d.fontSizeIndex;
		data.colorBgIndex = d.colorBgIndex;
		data.autoBuyNextChpater = d.autoBuyNextChpater
		util.setStorage(config.storage_key.READ_SETTING,JSON.stringify(data))
	}
	
	async function changeFontSize(index:number){
		data.fontSizeIndex=index;
		util.setStorage(config.storage_key.READ_SETTING,JSON.stringify(data))
	}
	
	async function changeBgColor(index:number){
		data.colorBgIndex=index;
		util.setStorage(config.storage_key.READ_SETTING,JSON.stringify(data))
	}
	
	async function changeAutoBuyNextChapter(isAuto:boolean){
		data.autoBuyNextChpater = isAuto
		util.setStorage(config.storage_key.READ_SETTING,JSON.stringify(data))
	}
	
	async function changeReadMode(mode:number){
		data.readMode = mode
		util.setStorage(config.storage_key.READ_SETTING,JSON.stringify(data))
	}
	
	function getReadSetting():read_setting_data{
		if(data==null||data ==undefined||data.colorBgIndex==undefined){
			// util.removeStorageForKey(config.storage_key.READ_SETTING)
			let obj = util.getStorage(config.storage_key.READ_SETTING)
			if(obj) {		
				Object.assign(data,JSON.parse(obj))
			}else{
				data.colorBgIndex = 0;
				data.fontSizeIndex = 4;
				data.readMode = config.read_config.readMode.Bright
				data.autoBuyNextChpater = false
				util.setStorage(config.storage_key.READ_SETTING,JSON.stringify(data))
			}
		}
		return data
	}
	
	function getRechargeList(stype:number,cb:Function){
		if(recharge_data==null){
			http.StaticRequest(config.url_confg.Static.get_recharge_list(stype),(err,d)=>{
				if(d.code==config.url_confg.StatesCode.SUCCESS){
					recharge_data = d.content
					cb(recharge_data)
				}
			})
		}else{
			cb(recharge_data)
		}
	}
	return {data,updateReadSetting,getReadSetting,changeFontSize,changeBgColor,changeAutoBuyNextChapter,changeReadMode,getRechargeList}
})