<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
			// #ifdef APP-PLUS
			this.createVoiceFab()
			// #endif
		},
		onShow: function() {
			console.log('App Show')
			// #ifdef APP-PLUS
			this.createVoiceFab()
			this.updateVoiceFabVisible()
			// #endif
		},
		onHide: function() {
			console.log('App Hide')
			// #ifdef APP-PLUS
			this.updateVoiceFabVisible(false)
			// #endif
		},
		methods: {
			// #ifdef APP-PLUS
			createVoiceFab() {
				if (this._voiceFabView) return
				const info = uni.getSystemInfoSync()
				const size = 54
				const right = 14
				const bottom = 38
				const left = (info.windowWidth || 375) - size - right
				const top = (info.windowHeight || 667) - size - bottom
				const view = new plus.nativeObj.View('global-voice-fab', {
					left: left + 'px',
					top: top + 'px',
					width: size + 'px',
					height: size + 'px'
				})

				view.draw([
					{
						tag: 'rect',
						id: 'bg',
						position: { left: '0px', top: '0px', width: size + 'px', height: size + 'px' },
						rectStyles: { color: '#1677ff', radius: size / 2 }
					},
					{
						tag: 'font',
						id: 'txt',
						text: '语音',
						position: { left: '0px', top: '0px', width: size + 'px', height: size + 'px' },
						textStyles: { color: '#ffffff', size: '14px', align: 'center', verticalAlign: 'middle' }
					}
				])

				view.addEventListener('click', () => {
					try {
						const pages = getCurrentPages ? getCurrentPages() : []
						const current = pages && pages.length ? pages[pages.length - 1] : null
						if (current && current.route === 'pages/voice/index') {
							const vm = current.$vm
							if (vm && typeof vm.startRecord === 'function' && !vm.recording && !vm.loading) {
								vm.startRecord()
								return
							}
						}
					} catch (e) {}
					uni.navigateTo({ url: '/pages/voice/index?autostart=1' })
				})

				view.show()
				this._voiceFabView = view
			},
			updateVoiceFabVisible(forceVisible) {
				if (!this._voiceFabView) return
				let visible = true
				if (typeof forceVisible === 'boolean') visible = forceVisible
				try {
					const pages = getCurrentPages ? getCurrentPages() : []
					const current = pages && pages.length ? pages[pages.length - 1] : null
					if (current && current.route === 'pages/voice/index') visible = false
				} catch (e) {}
				if (visible) this._voiceFabView.show()
				else this._voiceFabView.hide()
			}
			// #endif
		}
	}
</script>

<style>
	/*每个页面公共css */
	
	/* 隐藏底部tabBar */
	uni-tabbar {
		display: none !important;
	}
	
	.uni-tabbar {
		display: none !important;
	}
</style>
