var lastTime = null

function clamp(value, min, max) {
	return min < max ?
		(value < min ? min : value > max ? max : value) :
		(value < max ? max : value > min ? min : value)
}

function touchstart(e, ins) {
	var containerWidth = ins.selectComponent('.zebra-saturation').getBoundingClientRect().width
	var containerHeight = ins.selectComponent('.zebra-saturation').getBoundingClientRect().height

	var xOffset = ins.selectComponent('.zebra-saturation').getBoundingClientRect().left
	var yOffset = ins.selectComponent('.zebra-saturation').getBoundingClientRect().top
	var pageX = e.pageX || (e.touches ? e.touches[0].clientX : 0)
	var pageY = e.pageY || (e.touches ? e.touches[0].clientY : 0)
	var left = clamp(pageX - xOffset, 0, containerWidth)
	var top = clamp(pageY - yOffset, 0, containerHeight)
	var saturation = left / containerWidth
	var bright = clamp(-(top / containerHeight) + 1, 0, 1)

	ins.selectComponent('.zebra-saturation-pointer').setStyle({
		left: (saturation * 100) + '%',
		top: ((-(bright * 100) + 1) + 100) + '%'
	})
	var currentTime = Date.now()
	if (currentTime - lastTime >= 50) {
		ins.callMethod("onChangeWxs", {
			saturation,
			bright
		})
		lastTime = currentTime
	}
	return false
}

function touchmove(e, ins) {
	var containerWidth = ins.selectComponent('.zebra-saturation').getBoundingClientRect().width
	var containerHeight = ins.selectComponent('.zebra-saturation').getBoundingClientRect().height

	var xOffset = ins.selectComponent('.zebra-saturation').getBoundingClientRect().left
	var yOffset = ins.selectComponent('.zebra-saturation').getBoundingClientRect().top
	var pageX = e.pageX || (e.touches ? e.touches[0].clientX : 0)
	var pageY = e.pageY || (e.touches ? e.touches[0].clientY : 0)
	var left = clamp(pageX - xOffset, 0, containerWidth)
	var top = clamp(pageY - yOffset, 0, containerHeight)
	var saturation = left / containerWidth
	var bright = clamp(-(top / containerHeight) + 1, 0, 1)

	ins.selectComponent('.zebra-saturation-pointer').setStyle({
		left: (saturation * 100) + '%',
		top: ((-(bright * 100) + 1) + 100) + '%'
	})
	var currentTime = Date.now()
	if (currentTime - lastTime >= 50) {
		ins.callMethod("onChangeWxs", {
			saturation,
			bright
		})
		lastTime = currentTime
	}

	return false
}

module.exports = {
	touchstart: touchstart,
	touchmove: touchmove
}
