export default (modules, q, qa) => {
	let self = {
		vidContainer: q(".video-container-inner")
	}

	// Load thumb stream when ready
	if (modules.globals.awaitingThumb) {
		const hoverTimeVideo = q(".videoControls .timeline .hoverTimeContainer .hoverTimeVideo")
		modules.cacheInfo.pingCache(`${modules.globals.data.videoId}-thumb`, () => hoverTimeVideo.setAttribute("src", `/getVideo?v=${data.videoId}&q=thumb&dl=1`), 1000 * 2)
	}

	// Doing it this way allows sponsorblock popup to display in fullscreen.
	function onFS() {
		if (!document.fullscreen) {
			modules.globals.video.parentNode.parentNode.classList.remove("fullscreen")
			modules.controls.fullscreenBtn.classList.remove("active")
		} else {
			modules.globals.video.parentNode.parentNode.classList.add("fullscreen")
			modules.controls.fullscreenBtn.classList.add("active")
		}
	}
	modules.globals.video.parentNode.parentNode.addEventListener("fullscreenchange", onFS)
	modules.globals.video.parentNode.parentNode.addEventListener("webkitfullscreenchange", onFS)
	modules.globals.video.parentNode.parentNode.addEventListener("mozfullscreenchange", onFS)
	modules.globals.video.parentNode.parentNode.addEventListener("msfullscreenchange", onFS)

	self.vidContainer.addEventListener("click", event => {
		event.preventDefault()
		self.togglePlaying()
	})

	self.vidContainer.addEventListener("dblclick", event => {
		event.preventDefault()
		self.toggleFullScreen()
	})

	modules.globals.video.addEventListener("contextmenu", event => event.preventDefault())
	self.vidContainer.addEventListener("contextmenu", event => event.preventDefault())

	self = {
		...self,
		startingTime: modules.globals.videoPath.split("#t=npt:")[1] ?? 0,
		videoLength: modules.globals.data.lengthSeconds,
		lastPlaying: modules.globals.video.hasAttribute("autoplay"),
		lastTrack: modules.globals.video.textTracks[0],

		relativeSeek: seconds => {
			// Also show video controls and volume bar
			modules.controls.hideControls(true)

			modules.globals.video.currentTime += seconds
		},
		togglePlaying: () => {
			// Also show video controls and volume bar
			modules.controls.hideControls(true)

			if (modules.globals.video.paused) modules.globals.video.play()
			else modules.globals.video.pause()
			self.lastPlaying = !modules.globals.video.paused
		},
		toggleFullScreen: () => {
			if (document.fullscreen) document.exitFullscreen()
			else modules.globals.video.parentNode.parentNode.requestFullscreen()
		},

		keydown: event => {
			if (["INPUT", "SELECT", "BUTTON"].includes(event.target.tagName)) return
			if (event.ctrlKey || event.shiftKey || event.altKey || event.metaKey) return
			let caught = true
			if (event.key === "j" || event.key === "n") {
				self.relativeSeek(-10)
			} else if (["k", "p", " ", "e"].includes(event.key)) {
				self.togglePlaying()
			} else if (event.key === "l" || event.key === "o") {
				self.relativeSeek(10)
			} else if (event.key === "ArrowLeft") {
				self.relativeSeek(-5)
			} else if (event.key === "ArrowRight") {
				self.relativeSeek(5)
			} else if (modules.globals.video.paused && event.key == ",") {
				self.relativeSeek(-1 / 30)
			} else if (modules.globals.video.paused && event.key == ".") {
				self.relativeSeek(1 / 30)
			} else if (event.key >= "0" && event.key <= "9") {
				// Also show video controls and volume bar
				modules.controls.hideControls(true)
				modules.globals.video.currentTime = modules.globals.video.duration * (+event.key) / 10
			} else if (event.key === "f") {
				self.toggleFullScreen()
			} else if (event.key === "c") {
				if (self.lastTrack.mode == "showing")
					modules.controls.clickCaptionBtn(q(`.videoControls .settingsPopout .settingsPage[data-name='cc'] .setting.caption[data-label="Off"]`))
				else
					modules.controls.clickCaptionBtn(q(`.videoControls .settingsPopout .settingsPage[data-name='cc'] .setting.caption[data-label="${self.lastTrack.label}"]`))
			} else if (event.key === "m") {
				modules.globals.video.muted = !modules.globals.video.muted
			} else if (event.key == "Home") {
				// Also show video controls and volume bar
				modules.controls.hideControls(true)
				modules.globals.video.currentTime = 0
			} else if (event.key == "End") {
				// Also show video controls and volume bar
				modules.controls.hideControls(true)
				modules.globals.video.currentTime = self.videoLength
			} else if (event.key == "ArrowDown") {
				modules.volume.setVolume(modules.volume.currentVolume - 0.05)
				modules.controls.hideControls(true)
				modules.controls.hideVolume(true)
			} else if (event.key == "ArrowUp") {
				modules.volume.setVolume(modules.volume.currentVolume + 0.05)
				modules.controls.hideControls(true)
				modules.controls.hideVolume(true)
			} else {
				caught = false
			}
			if (caught) event.preventDefault()
		}
	}

	modules.globals.video.currentTime = self.startingTime
	document.addEventListener("keydown", self.keydown)

	return self
}
