
import { defConf, UxConf } from "./def.uts"
import { ColorBase } from "./color.uts"
import { SizeBase } from "./size.uts"
import { RadiusBase } from "./radiu.uts"
import { SpacingBase } from "./spacing.uts"

// #ifdef APP-ANDROID
import Configuration from 'android.content.res.Configuration';
import Context from 'android.content.Context';
import UiModeManager from 'android.app.UiModeManager';
// #endif

/**
 * UxConf 全局配置
 */
export class Conf {
	
	/**
	 * 开启debug打印
	 */
	debugLog = ref(true)
	
	/**
	 * 单位
	 */
	unit = ref('px')
	
	/**
	 * 深色模式
	 */
	darkMode = ref(false)
	
	/**
	 * 跟随系统
	 */
	followSys = ref(false)
	
	/**
	 * 强调色
	 */
	primaryColor: ColorBase
	
	/**
	 * 次要色
	 */
	secondaryColor: ColorBase
	
	/**
	 * 成功提示色
	 */
	successColor: ColorBase
	
	/**
	 * 错误提示色
	 */
	errorColor: ColorBase
	
	/**
	 * 警告提示色
	 */
	warningColor: ColorBase
	
	/**
	 * 信息提示色
	 */
	infoColor: ColorBase
	
	/**
	 * 背景色
	 */
	backgroundColor: ColorBase
	
	/**
	 * 文字色
	 */
	fontColor: ColorBase
	
	/**
	 * 占位色
	 */
	placeholderColor: ColorBase
	
	/**
	 * 取消色
	 */
	cancelColor: ColorBase
	
	/**
	 * 边框色
	 */
	borderColor: ColorBase
	
	/**
	 * 按压色
	 */
	hoverColor: ColorBase
	
	/**
	 * 标题色
	 */
	titleColor: ColorBase
	
	/**
	 * 二级标题色
	 */
	subtitleColor: ColorBase
	
	/**
	 * 段落色
	 */
	paragraphColor: ColorBase
	
	/**
	 * 分割线色
	 */
	lineColor: ColorBase
	
	/**
	 * 背景透明度
	 */
	maskAlpha = ref(0.5)
	
	/**
	 * 文字大小
	 */
	fontSize: SizeBase
	
	/**
	 * 圆角
	 */
	radius: RadiusBase
	
	/**
	 * 水平外边距
	 */
	hmargin: SpacingBase
	
	/**
	 * 垂直外边距
	 */
	vmargin: SpacingBase
	
	/**
	 * 水平内边距
	 */
	hpadding: SpacingBase
	
	/**
	 * 垂直内边距
	 */
	vpadding: SpacingBase
	
	/**
	 * 缓存key
	 */
	private cacheKey: string = 'UxFrameConf'
	
	constructor() {
		
		// 初始化配置
		this.debugLog.value = defConf.debugLog!
		this.unit.value = defConf.unit!
		this.darkMode.value = defConf.darkMode!
		this.followSys.value = defConf.followSys!
		this.primaryColor = new ColorBase(defConf.primaryColor!)
		this.secondaryColor = new ColorBase(defConf.secondaryColor!)
		this.successColor = new ColorBase(defConf.successColor!)
		this.errorColor = new ColorBase(defConf.errorColor!)
		this.warningColor = new ColorBase(defConf.warningColor!)
		this.infoColor = new ColorBase(defConf.infoColor!)
		this.backgroundColor = new ColorBase(defConf.backgroundColor!)
		this.fontColor = new ColorBase(defConf.fontColor!)
		this.placeholderColor = new ColorBase(defConf.placeholderColor!)
		this.cancelColor = new ColorBase(defConf.cancelColor!)
		this.hoverColor = new ColorBase(defConf.hoverColor!)
		this.borderColor = new ColorBase(defConf.borderColor!)
		this.titleColor = new ColorBase(defConf.titleColor!)
		this.subtitleColor = new ColorBase(defConf.subtitleColor!)
		this.paragraphColor = new ColorBase(defConf.paragraphColor!)
		this.lineColor = new ColorBase(defConf.lineColor!)
		this.maskAlpha.value = defConf.maskAlpha!
		this.fontSize = new SizeBase(defConf.fontSize!)
		this.radius = new RadiusBase(defConf.radius!)
		this.hmargin = new SpacingBase(defConf.hmargin!)
		this.vmargin = new SpacingBase(defConf.vmargin!)
		this.hpadding = new SpacingBase(defConf.hpadding!)
		this.vpadding = new SpacingBase(defConf.vpadding!)
		
		// 加载缓存配置
		this.loadCached()
		
		// 初始系统深色模式
		this.setSysDarkMode()
		
		// 监听系统深色模式
		this.onDarkModeListener()
		
		if(this.debugLog.value) {
			console.log('[UxFrame] 感谢使用UxFrameUI框架！如有任何问题可以加我们QQ群[450704209]或者访问我们的官网[https://www.uxframe.cn]进行反馈！配置[debugLog=false]可关闭此日志。');
		}
	}
	
	/**
	 * 加载缓存配置
	 */
	private loadCached() {
		this.clearCache()
		let data = uni.getStorageSync(this.cacheKey)
		
		if(data instanceof UTSJSONObject) {
			let conf = JSON.parseObject<UxConf>(JSON.stringify(data))
			
			if(conf != null) {
				this.setConf(conf)
			}
		}
	}
	
	/**
	 * 设置缓存
	 */
	private setCache() {
		uni.setStorageSync(this.cacheKey, {
			debugLog: this.debugLog.value,
			unit: this.unit.value,
			darkMode: this.darkMode.value,
			followSys: this.followSys.value,
			primaryColor: this.primaryColor.color.value,
			secondaryColor: this.secondaryColor.color.value,
			successColor: this.successColor.color.value,
			errorColor: this.errorColor.color.value,
			warningColor: this.warningColor.color.value,
			infoColor: this.infoColor.color.value,
			backgroundColor: this.backgroundColor.color.value,
			fontColor: this.fontColor.color.value,
			placeholderColor: this.placeholderColor.color.value,
			cancelColor: this.cancelColor.color.value,
			hoverColor: this.hoverColor.color.value,
			borderColor: this.borderColor.color.value,
			titleColor: this.titleColor.color.value,
			subtitleColor: this.subtitleColor.color.value,
			paragraphColor: this.paragraphColor.color.value,
			lineColor: this.lineColor.color.value,
			maskAlpha: this.maskAlpha.value,
			fontSize: this.fontSize.normal.value,
			radius: this.radius.normal.value,
			hmargin: this.hmargin.normal.value,
			vmargin: this.vmargin.normal.value,
			hpadding: this.hpadding.normal.value,
			vpadding: this.vpadding.normal.value,
		} as UTSJSONObject)
	}
	
	/**
	 * 清理缓存
	 */
	clearCache() {
		uni.removeStorageSync(this.cacheKey)
	}
	
	/**
	 * 设置配置
	 */
	setConf(conf: UxConf) {
		if(conf.debugLog != null) this.setDebugLog(conf.debugLog!)
		if(conf.unit != null) this.setUnit(conf.unit!)
		if(conf.darkMode != null) this.setDarkMode(conf.darkMode!)
		if(conf.followSys != null) this.setFollowSys(conf.followSys!)
		if(conf.primaryColor != null) this.setPrimaryColor(conf.primaryColor!)
		if(conf.secondaryColor != null) this.setSecondaryColor(conf.secondaryColor!)
		if(conf.successColor != null) this.setSuccessColor(conf.successColor!)
		if(conf.errorColor != null) this.setErrorColor(conf.errorColor!)
		if(conf.warningColor != null) this.setWarningColor(conf.warningColor!)
		if(conf.infoColor != null) this.setInfoColor(conf.infoColor!)
		if(conf.backgroundColor != null) this.setBackgroundColor(conf.backgroundColor!)
		if(conf.fontColor != null) this.setFontColor(conf.fontColor!)
		if(conf.placeholderColor != null) this.setPlaceholderColor(conf.placeholderColor!)
		if(conf.cancelColor != null) this.setCancelColor(conf.cancelColor!)
		if(conf.hoverColor != null) this.setHoverColor(conf.hoverColor!)
		if(conf.borderColor != null) this.setBorderColor(conf.borderColor!)
		if(conf.titleColor != null) this.setTitleColor(conf.titleColor!)
		if(conf.subtitleColor != null) this.setSubtitleColor(conf.subtitleColor!)
		if(conf.paragraphColor != null) this.setParagraphColor(conf.paragraphColor!)
		if(conf.lineColor != null) this.setLineColor(conf.lineColor!)
		if(conf.maskAlpha != null) this.setMaskAlpha(conf.maskAlpha!)
		if(conf.fontSize != null) this.setFontSize(conf.fontSize!)
		if(conf.radius != null) this.setRadius(conf.radius!)
		if(conf.hmargin != null) this.setHmargin(conf.hmargin!)
		if(conf.vmargin != null) this.setVmargin(conf.vmargin!)
		if(conf.hpadding != null) this.setHpadding(conf.hpadding!)
		if(conf.vpadding != null) this.setVpadding(conf.vpadding!)
		
		this.setCache()
	}
	
	/**
	 * 开启打印日志
	 */
	setDebugLog(debugLog: boolean) {
		this.debugLog.value = debugLog
		this.setCache()
	}
	
	/**
	 * 设置单位
	 */
	setUnit(unit: string) {
		this.unit.value = unit
		this.setCache()
	}
	
	/**
	 * 设置深色模式
	 */
	setDarkMode(dark: boolean) {
		this.darkMode.value = dark
		this.setGlobalBackgroundColor()
		this.setCache()
	}
	
	/**
	 * 设置跟随系统
	 */
	setFollowSys(followSys: boolean) {
		this.followSys.value = followSys
		
		this.setSysDarkMode()
		this.setCache()
	}
	
	/**
	 * 设置主色调
	 */
	setPrimaryColor(color: string) {
		this.primaryColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置次要色
	 */
	setSecondaryColor(color: string) {
		this.secondaryColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置成功提示色
	 */
	setSuccessColor(color: string) {
		this.successColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置错误提示色
	 */
	setErrorColor(color: string) {
		this.errorColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置警告提示色
	 */
	setWarningColor(color: string) {
		this.warningColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置信息提示色
	 */
	setInfoColor(color: string) {
		this.infoColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置背景色
	 */
	setBackgroundColor(color: string) {
		this.backgroundColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置文本色
	 */
	setFontColor(color: string) {
		this.fontColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置占位色
	 */
	setPlaceholderColor(color: string) {
		this.placeholderColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置取消色
	 */
	setCancelColor(color: string) {
		this.cancelColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置按压色
	 */
	setHoverColor(color: string) {
		this.hoverColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置边框色
	 */
	setBorderColor(color: string) {
		this.borderColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置标题色
	 */
	setTitleColor(color: string) {
		this.titleColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置二级标题色
	 */
	setSubtitleColor(color: string) {
		this.subtitleColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置段落色
	 */
	setParagraphColor(color: string) {
		this.paragraphColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置分割线色
	 */
	setLineColor(color: string) {
		this.lineColor.reset(color)
		this.setCache()
	}
	
	/**
	 * 设置背景透明度
	 */
	setMaskAlpha(alpha: number) {
		this.maskAlpha.value = alpha
		this.setCache()
	}
	
	/**
	 * 设置文字大小
	 */
	setFontSize(fontSize: number) {
		this.fontSize.reset(fontSize)
		this.setCache()
	}
	
	/**
	 * 设置圆角
	 */
	setRadius(radius: number) {
		this.radius.reset(radius)
		this.setCache()
	}
	
	/**
	 * 水平外边距
	 */
	setHmargin(margin: number) {
		this.hmargin.reset(margin)
		this.setCache()
	}
	
	/**
	 * 垂直外边距
	 */
	setVmargin(margin: number) {
		this.vmargin.reset(margin)
		this.setCache()
	}
	
	/**
	 * 水平内边距
	 */
	setHpadding(padding: number) {
		this.hpadding.reset(padding)
		this.setCache()
	}
	
	/**
	 * 垂直内边距
	 */
	setVpadding(padding: number) {
		this.vpadding.reset(padding)
		this.setCache()
	}
	
	/**
	 * 监听深色模式
	 */
	setSysDarkMode() {
		let setOsTheme = (dark: boolean) => {
			if(this.followSys.value) {
				this.setDarkMode(dark)
			}
		}
		
		// #ifdef APP-ANDROID
		const uiModeManager: UiModeManager = UTSAndroid.getUniActivity()!.getSystemService(Context.UI_MODE_SERVICE) as UiModeManager
		const isDarkMode = uiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES
		setOsTheme(isDarkMode)
		// #endif
		
		// #ifdef APP-IOS
		// 暂不支持
		setOsTheme(false)
		// #endif
		
		// #ifdef WEB
		// 媒体查询
		const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
		setOsTheme(isDarkMode)
		// #endif
	}
	
	/**
	 * 监听深色模式
	 */
	onDarkModeListener() {
		let setOsTheme = (dark: boolean) => {
			if(this.followSys.value) {
				this.setDarkMode(dark)
			}
		}
		
		// #ifdef APP-ANDROID
		UTSAndroid.onAppConfigChange((res) => {
			let uiMode = res.getNumber('uiMode') as Int
			const isDark = uiMode & Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
			setOsTheme(isDark)
		})
		// #endif
		
		// #ifdef APP-IOS
		// 暂不支持
		setOsTheme(false)
		// #endif
		
		// #ifdef WEB
		const themeChange = (e) => {
			setOsTheme(e.matches)
		}
		window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', themeChange)
		// #endif
		
		this.setGlobalBackgroundColor()
	}
	
	/**
	 * 设置全局背景色
	 */
	setGlobalBackgroundColor() {
		const color = this.darkMode.value ? this.backgroundColor.dark.value : this.backgroundColor.color.value
		
		// @ts-expect-error
		__uniConfig.globalStyle['backgroundColor'] = color
		// @ts-expect-error
		__uniConfig.globalStyle['backgroundColorContent'] = color
	}
}