<template>
	<view class="content skipping">
		<!--自由训练  -->
		<view class="title">{{info.active==1?'自由训练':info.active==2?'定时训练':'定数训练'}}</view>
		<view class="skiptop">
			<view class="item">
				<view class="item-ite">{{info.active==1?'自由次数':info.active==2?"个数":'目标次数'}}<text>{{weight}}</text>
				</view>
				<view class="item-ite">
					{{info.active==2?'目标时长 分:秒':"分:秒"}}<text>{{time_m?time_m:'00'}}:{{time_s?time_s:'00'}}</text>
				</view>
				<view class="item-ite">消耗/kcal<text>{{Math.floor(kcal)}}</text></view>
			</view>
		</view>
		<view class="image">
			<image src="../../static/t01.gif"></image>
		</view>
		<view class="end" @longpress="onlongpress">长按结束</view>
		<!-- 报告 -->
		<view class="wrapper" v-if="iswrapper">
			<view class="bg">
				<view class="edit">
					<view class="editem">
						<view>平均速度：<text class="cyello Blue size20 mr-5">{{bpm.toFixed(1)}}</text>bpm</view>
						<view class="size12 c999 ">(bpm=个/分钟)</view>
					</view>
					<view class="center">
						<view class="left">
							<image src="../../static/duan.png"></image>
							<view class="name">
								<view>
									中断次数
								</view>
								<view>
									<text class="cyello Blue size20 mr-5">{{Bcount}}</text>次
								</view>
							</view>
						</view>
						<!--  -->
						<view class="left">
							<image src="../../static/xu.png"></image>
							<view class="name">
								<view>
									最长连续
								</view>
								<view>
									<text class="cyello Blue size20 mr-5">{{continuous}}</text>个
								</view>
							</view>
						</view>
					</view>
					<view class="item">
						<view class="item-ite"><text>{{weight}}</text>训练个数</view>
						<view class="item-ite"><text>{{time_m?time_m:'00'}}:{{time_s?time_s:'00'}}</text>分:秒</view>
						<view class="item-ite"><text>{{Math.floor(kcal)}}</text>消耗/kcal</view>
					</view>
					<view class="btn" @click="handleTarget">完成</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from "vuex";
	const innerAudioContext = uni.createInnerAudioContext();
	export default {
		data() {
			return {
				bpm: 0,
				isend: false,
				weight: 0,
				Ycount: 0,
				time_m: "",
				time_s: "",
				time: 0,
				kcal: 0,
				Bcount: 0,
				continuous: 0,
				info: {},
				isToggle: false,
				isEnd: false,
				iswrapper: false,
				isStart: false,

			}
		},
		computed: {
			...mapState(["isConnected", "isBluetoothTyle"]),
		},
		onLoad(options) {
			let that = this
			if (options && options.info) {
				let info = options.info
				that.info = JSON.parse(info)
				that.notifyBLECharacteristicValue()
				setTimeout(function() {
					that.handleStart('开始')
				}, 900)
				setTimeout(function() {
					that.handleStart('连续')
				}, 1200)
			}
			that.$Bluetooth.onBLEConnectionStateChange()
			uni.onBluetoothAdapterStateChange(function(res) {
				that.$store.commit("changeBluetooth", res.available);
			})
		},
		onBackPress() {
			let that = this
			that.handleEnd()
			innerAudioContext.stop();
			console.log("页面返回onBackPress")
		},
		watch: {
			isConnected: function() {
				let that = this
				if (!that.isConnected) {
					uni.showModal({
						title: '连接已断开',
						content: '训练过程中已与设备连接中断，请重新连接设备再开始训练',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								that.handleTarget()
							}
						}
					})
					console.log("蓝牙是否连接", that.isConnected)
				}
			},
			isBluetoothTyle: function() {
				let that = this
				if (!that.isBluetoothTyle) {
					console.log("蓝牙是否打开", that.isBluetoothTyle)
				}
			},
			// 切换模式
			isToggle: function() {
				let that = this
				if (that.isToggle) {
					uni.showModal({
						title: '友情提示',
						content: '训练过程中请勿切换训练，返回训练',
						showCancel: false,
						success: function(res) {
							if (res.confirm) {
								setTimeout(function() {
									that.handleStart('结束')
								}, 400)
								setTimeout(function() {
									that.handleTarget()
								}, 600)
							}
						}
					})
				}
			},
			// 模式被结束
			isEnd: function() {
				let that = this
				if (that.isEnd) {
					setTimeout(function() {
						that.handleStart('停止')
					}, 200)
					setTimeout(function() {
						that.handleGetMeasure()
					}, 400)
				}
			},
			// 开始跳绳
			isStart: function() {
				let that = this
				if (that.isStart) {
					that.handleAudio(1)
					console.log("11111111")
				}
			}
		},
		methods: {
			handleAudio(ind) {
				innerAudioContext.autoplay = true;
				innerAudioContext.loop = true;
				innerAudioContext.src = '/static/flight.mp3';
				innerAudioContext.play()

			},
			// 接收蓝牙数据
			notifyBLECharacteristicValue() {
				let that = this;
				uni.notifyBLECharacteristicValueChange({
					state: true, // 启用 notify 功能
					deviceId: that.info.deviceId,
					serviceId: that.info.serviceId,
					characteristicId: that.info.notify,
					success(res) {
						uni.onBLECharacteristicValueChange(function(res) {
							let value = that.$tools.ab2hex(res.value, "");
							let count = parseInt(value.substring(8, 12), 16)
							let Ycount = parseInt(value.substring(12, 16), 16) //设置次数
							let time = parseInt(value.substring(16, 20), 16) //运行时间/秒
							let timeDown = parseInt(value.substring(20, 24), 16) //倒计时时间
							let type = parseInt(value.substring(30, 32), 16) //当前状态
							let weight = parseInt(value.substring(32, 34), 16) //重量
							let kcal = parseInt(value.substring(34, 38), 16) //卡路里
							let minutes = null
							let seconds = null
							if (type == 0 || type == 4) {
								that.weight = count //个数
								that.Ycount = Ycount
								that.time = time
								that.kcal = kcal / 10
								if (count != 0 && !that.isStart) {
									that.isStart = true
								}
								if (that.info.active != 2 && count != 0) { //自由模式 +  计数
									minutes = Math.floor((time % 3600) / 60)
									seconds = time % 60
									that.time_m = minutes > 9 ? minutes : '0' + minutes
									that.time_s = seconds > 9 ? seconds : '0' + seconds
								}
								if (that.info.active == 2) { //计时
									let T = Number(timeDown) - Number(time)
									minutes = Math.floor((T % 3600) / 60)
									seconds = T % 60
									that.time_m = minutes > 9 ? minutes : '0' + minutes
									that.time_s = seconds > 9 ? seconds : '0' + seconds
								}
								that.Bcount = parseInt(value.substring(24, 26), 16) //绊绳个数
								that.continuous = parseInt(value.substring(26, 30), 16) //连续个数
								console.log("自由模式", count, kcal, time, that.time_m, that.time_s, weight)
							}
							if ((type == 2 || type == 3) && !that.isToggle) {
								setTimeout(function() {
									that.isToggle = true
									that.handleStart('停止')
								}, 200)
								console.log("当前状态被切换")

							}
							if (type == 4 && !that.isEnd) {
								that.isEnd = true
								console.log("当前状态被结束")
							}

						})
					},
					fail(res) {
						console.log("测量失败", res.value);
					}
				})
			},
			SendData(str) {
				let that = this
				let buf = new Uint8Array(str.match(/[\da-f]{2}/gi).map(function(h) {
					return parseInt(h, 16)
				}))
				uni.writeBLECharacteristicValue({
					deviceId: that.info.deviceId,
					serviceId: that.info.serviceId,
					characteristicId: that.info.write,
					value: buf.buffer,
					success: res => {
						console.log('下发指令成功', res.errMsg)
					},
					fail: res => {
						console.log("下发指令失败", res);
					},
				})
			},
			onlongpress() {
				let that = this
				uni.vibrateLong({
					success: function() {
						console.log('短震动');
						if (Number(that.time) < 10) {
							uni.showModal({
								title: '友情提示',
								content: '本次跳绳时间低于10秒，记录将不会被保存？',
								confirmText: "继续",
								cancelText: "返回",
								success: function(res) {
									if (res.cancel) {
										that.handleEnd()
									}
								},
							})
						} else {
							that.handleStart('停止')
							setTimeout(function() {
								that.handleStart('结束')
							}, 400)
							setTimeout(function() {
								that.handleGetMeasure()
							}, 800)

						}
					},
					fail: function(err) {
						console.error('震动失败：', err);
					},
				})
			},
			// 保存测量结果
			handleGetMeasure() {
				let that = this
				if (that.info.active == 3 && that.weight == 0 && that.Ycount != 0) {
					console.log("1", that.weight, that.Ycount)
					that.weight = that.Ycount
				} else if (that.info.active == 3 && that.weight != 0 && that.Ycount != 0) {
					console.log("2", that.weight, that.Ycount)
					that.weight = Number(that.Ycount) - Number(that.weight)
				}
				if (that.info.active == 2) {
					that.time_m = Math.floor((that.time % 3600) / 60)
					that.time_s = that.time % 60
				}
				console.log("111111保存", that.weight, that.Ycount, that.time_m, that.time_s)
				that.$model.getskipResult({
					aud_id: uni.getStorageSync('userid'),
					kcal: Math.floor(that.kcal),
					num: that.weight,
					time_m: Number(that.time_m),
					time_s: that.time_s,
					type: that.info.active == 1 ? 'free' : that.info.active == 2 ? 'time' : 'num'
				}).then(res => {
					console.log("保存", that.time, res)
					if (res.code == 0) {
						that.bpm = that.weight / (that.time / 60)
						that.iswrapper = true
						that.$store.dispatch('getUserInfo', {
							aud_id: uni.getStorageSync('userid')
						})
					} else {
						that.$tools.msg(res.msg)
					}
				})
			},
			// 开始指令
			handleStart(text) {
				let that = this
				let j = null
				let str = null
				if (text == '连续') {
					j = Number(165 + 5 + 3).toString(16)
					str = "A5050300" + j.substr(j.length - 2, 2)
					console.log("连续连续", str)
				}
				if (text == '停止') {
					j = Number(165 + 5 + 3 + 5).toString(16)
					str = "A5050305" + j.substr(j.length - 2, 2)
					console.log("停止连续", str)
				}
				if (text == '开始') {
					j = Number(165 + 5 + 5).toString(16)
					str = "A5050500" + j.substr(j.length - 2, 2)
				}
				if (text == '结束') {
					j = Number(165 + 5 + 5 + 1).toString(16)
					str = "A5050501" + j.substr(j.length - 2, 2)
					console.log("结束指令", str)
				}
				if (text == '继续') {
					j = Number(165 + 5 + 4).toString(16)
					str = "A5050400" + j.substr(j.length - 2, 2)
					console.log("继续指令", str)
				}
				if (text == '暂停') {
					j = Number(165 + 5 + 4 + 1).toString(16)
					str = "A5050401" + j.substr(j.length - 2, 2)
					console.log("暂停指令", str)
				}
				that.SendData(str)
			},
			handleTarget() {
				let that = this
				uni.$emit('updateData', JSON.stringify(that.info))
				uni.navigateBack({ //返回
					delta: 1
				})
			},
			// 
			handleEnd() {
				let that = this
				that.handleStart('停止')
				setTimeout(function() {
					that.handleStart('结束')
				}, 400)
				setTimeout(function() {
					uni.redirectTo({
						url: "/pages/skip/skip?info=" + JSON.stringify(that.info)
					})
				}, 600)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		min-height: 100vh;
		background-color: #fff;
	}

	.item-ite {
		line-height: 20PX;
		margin-bottom: 15px;
	}

	.title {
		height: 50px;
		line-height: 50px;
		text-align: center;
		font-weight: bold;
	}

	.image {
		width: 100%;
		text-align: center;
		margin-top: 30px;

		image {
			width: 200px;
			height: 252px;
			margin: auto;
		}
	}

	.end {
		color: #fff;
		position: absolute;
		bottom: 80px;
		width: 70px;
		height: 70px;
		line-height: 70px;
		background: $btncolor;
		margin-left: calc(50% - 35px);
		border-radius: 50px;
		text-align: center;
		box-shadow: 0px 1px 5px 2px #dfe2e1fc;
	}

	.wrapper {
		background-color: rgba(0, 0, 0, 0.6);

		.edit {
			width: 80% !important;
			background-color: #fff;

		}

		.editem {
			height: initial;
			display: flex;
			align-items: center;
			flex-direction: column;

			view {
				width: 100%;
				text-align: center;
				margin-bottom: 5px;
			}
		}

		.center {
			background-color: #eee;
			padding: 0 10px;
			display: flex;
			margin: 45px 0;
			font-size: 14px;
			border-radius: 5px;
			height: 65px;
			align-items: center;
			justify-content: space-between;

			image {
				width: 35px;
				height: 35px;
				margin-right: 10px;
				margin-top: 15px;
			}

			.left {
				display: flex;
				align-content: center;
			}

			.name {
				margin-top: 15px;
			}
		}

		.item {
			display: flex;
			justify-content: space-between;

			.item-ite {
				line-height: 20px;
				font-size: 14px;
				color: #999;
				text-align: center;

				text {
					display: block;
					width: 100%;
					font-size: 18px;
					color: #333;
					text-align: center;
					margin-bottom: 10px;
				}
			}
		}

		.btn {
			margin-top: 25px;
			height: 45px;
			line-height: 45px;
			background: $btncolor;
			margin-bottom: 25px;
		}
	}
</style>