import initCtx from "@/utils/ctx.js";

export default function () {
	const {utils} = initCtx(this)

	this.moreOptionsVisible = false
	this.globalMessageBadge = 0

	if (typeof this.leftMenuItemIsDisabled == "undefined") {
		this.leftMenuItemIsDisabled = false
	}

	this.menuItems = this.teaModules
		.filter(item => {
			return item.isOn !== false
		})
		.map((item) => {
			return {
				code: item.code,
				name: item.name,
				icon: "icon " + item.icon,
				url: (item.subItems == null || item.subItems.length === 0) ? "/" + item.code.replace(".", "/") : "",
				active: item.code === this.teaMenu,
				items: item.subItems?.filter((subItem) => {
					return subItem.isOn !== false
				}).map((subItem) => {
					return {
						code: subItem.code,
						name: subItem.name,
						url: subItem.url,
						active: item.code == this.teaMenu && subItem.code === this.teaSubMenu,
					}
				})
			}
		})

	this.$delay(function () {
		// 检查消息
		this.checkMessages()
	})

	/**
	 * 左侧子菜单
	 */
	this.showSubMenu = function (menu) {
		if (menu.alwaysActive) {
			return
		}
		if (this.teaSubMenus.menus != null && this.teaSubMenus.menus.length > 0) {
			this.teaSubMenus.menus.$each(function (k, v) {
				if (menu.id == v.id) {
					return
				}
				v.isActive = false
			})
		}
		menu.isActive = !menu.isActive
	};

	/**
	 * 检查消息
	 */
	this.checkMessages = function () {
		this.$post("/messages/badge")
			.params({})
			.success(function (resp) {
				this.globalMessageBadge = resp.data.count

				// add dot to title
				let dots = "••• "
				if (typeof document.title == "string") {
					if (resp.data.count > 0) {
						if (!document.title.startsWith(dots)) {
							document.title = dots + document.title
						}
					} else if (document.title.startsWith(dots)) {
						document.title = document.title.substring(dots.length)
					}
				}

				// instant message
				if (resp.data.instantMessages && resp.data.instantMessages.length > 0) {
					utils.showInstantMessage(resp.data.instantMessages[0])
				}
			})
			.done(function () {
				let delay = 30000
				if (this.globalMessageBadge > 0) {
					delay = 60000
				}
				this.$delay(function () {
					this.checkMessages()
				}, delay)
			})
	}

	/**
	 * tabbar
	 */
	this.tabValue = ""
	if (this.teaTabbar != null) {
		for (const tabItem of this.teaTabbar) {
			if (tabItem.isActive) {
				this.tabValue = tabItem.url
			}
		}
	}

	this.clickTab = function (e, item) {
		if (item.isActive) {
			window.location = item.url
		}
	}

	this.changeTab = function (tab) {
		window.location = tab.value
	}

	/** color scheme **/
	this.currentColorScheme = localStorage.getItem("color-scheme")
	if (this.currentColorScheme == null || this.currentColorScheme.length == 0) {
		this.currentColorScheme = "light"
	}
	document.querySelector("html").className = this.currentColorScheme

	this.changeColorScheme = function () {
		if (this.currentColorScheme == "light") {
			this.currentColorScheme = "dark"
		} else {
			this.currentColorScheme = "light"
		}
		localStorage.setItem("color-scheme", this.currentColorScheme)
		document.querySelector("html").className = this.currentColorScheme
	}
}
