// https://www.youtube.com/watch?v=q9bfJksggOY&feature=youtu.be&t=426

import Quest from "./quest";

const inBound = (...args) => {
	const [x, y, x0, y0, w, h] = args
	return x > x0
		&& x < x0 + w
		&& y > y0
		&& y < y0 + h
}

const channels = [
	[400, 370, 40, 40],
	[450, 370, 40, 40],
	[500, 370, 40, 40],
	[550, 370, 40, 40],
	[400, 420, 40, 40],
	[450, 420, 40, 40],
	[500, 420, 40, 40],
	[550, 420, 40, 40],
]

const buttons = [
	[60, 400, 130, 50],
	[220, 400, 130, 50]
]

const exitButton = [500, 20, 100, 40]

export default class TuneAntenna extends Quest {

	private ang: number = 0
	private angDest: number = 0

	private selectedChannel: number = 0

	private statusTexts = [
		'Сигнала нет',
		"Связь\nустановлена\n049", // yellow 5
		"Связь\nустановлена\n840", // blue 2
		"Связь\nустановлена\n216", // 8
	]

	private status: number = 0

	private scanTimeout: number = 0

	private vars: any

	private ready: boolean = false

	init(vars) {
		if (vars.tune_antena_settings) {
			this.ang = vars.tune_antena_settings.ang
			this.angDest = vars.tune_antena_settings.ang
			this.selectedChannel = vars.tune_antena_settings.selectedChannel
		}

		this.vars = vars
	}

	public draw(ctx: CanvasRenderingContext2D) {

		ctx.lineWidth = 3

		ctx.strokeStyle = '#aaa'
		ctx.beginPath()
		ctx.arc(200, 200, 150, 0, 2 * Math.PI);
		ctx.stroke()

		ctx.strokeStyle = '#555'
		ctx.beginPath()
		ctx.arc(200, 200, 100, 0, 2 * Math.PI);
		ctx.stroke()

		ctx.beginPath()
		ctx.arc(200, 200, 30, 0, 2 * Math.PI);
		ctx.stroke()

		ctx.beginPath()
		ctx.strokeStyle = 'yellow'
		ctx.lineWidth = this.ang === 300 ? 3 : 1
		ctx.moveTo(200, 200)
		ctx.lineTo(200 + 0.34202014332 * 150, 200 - 0.93969262078 * 150)
		ctx.stroke()

		ctx.beginPath()
		ctx.strokeStyle = 'blue'
		ctx.lineWidth = this.ang === 180 ? 3 : 1
		ctx.moveTo(200, 200)
		ctx.lineTo(200 + -0.98480775301 * 150, 200 - 0.17364817766 * 150)
		ctx.stroke()

		ctx.beginPath()
		ctx.strokeStyle = 'red'
		ctx.lineWidth = this.ang === 90 ? 3 : 1
		ctx.moveTo(200, 200)
		ctx.lineTo(200 - 0.17364817766 * 150, 200 + 0.98480775301 * 150)
		ctx.stroke()

		ctx.fillStyle = 'rgba(255,255,255, .2)'
		ctx.beginPath()
		ctx.moveTo(200, 200)
		ctx.arc(200, 200, 150, (this.ang - 15) * Math.PI / 180, (this.ang + 15) * Math.PI / 180);
		ctx.closePath()
		ctx.fill()


		ctx.fillStyle = '#6ab3b4'

		buttons.forEach(([x, y, w, h]) => {
			ctx.fillRect(x, y, w, h)
		})

		channels.forEach(([x, y, w, h]) => {
			ctx.fillRect(x, y, w, h)
		})

		// @ts-ignore
		ctx.fillRect(...exitButton)

		const ch = channels[this.selectedChannel]
		ctx.fillStyle = 'red'
		// @ts-ignore
		ctx.fillRect(...ch)

		ctx.fillStyle = '#fff'
		ctx.textAlign = 'center'
		ctx.font = '20px sans-serif'
		channels.forEach((item, i) => {
			ctx.fillText(`${i + 1}`, item[0] + 20, item[1] + 28)
		})

		ctx.fillText('Выйти', exitButton[0] + exitButton[2] / 2, exitButton[1] + exitButton[3] * (2 / 3))

		ctx.textAlign = 'left'
		ctx.font = '24px sans-serif'
		if (this.ang !== this.angDest) {
			ctx.fillText('...', 400, 200)
		} else {
			if (this.scanTimeout > 0) {
				ctx.fillText('Сканирование...', 400, 200)
			} else {
				const texts = this.statusTexts[this.status].split('\n')
				texts.forEach((text, i) => ctx.fillText(text, 400, 200 + i * 30))
			}
		}

		ctx.font = '20px sans-serif'
		ctx.fillText('Канал', 400, 350)

		const offset = 15

		ctx.strokeStyle = '#fff'
		ctx.lineWidth = 3
		ctx.beginPath()

		ctx.moveTo(buttons[0][0] + buttons[0][2] - offset * 2, buttons[0][1] + buttons[0][3] / 2)
		ctx.lineTo(buttons[0][0] + offset * 2, buttons[0][1] + buttons[0][3] / 2)

		ctx.moveTo(buttons[0][0] + 40, buttons[0][1] + offset)
		ctx.lineTo(buttons[0][0] + offset * 2, buttons[0][1] + buttons[0][3] / 2)
		ctx.lineTo(buttons[0][0] + 40, buttons[0][1] + buttons[0][3] - offset)


		ctx.moveTo(buttons[1][0] + buttons[1][2] - offset * 2, buttons[1][1] + buttons[1][3] / 2)
		ctx.lineTo(buttons[1][0] + offset * 2, buttons[1][1] + buttons[1][3] / 2)

		ctx.moveTo(buttons[1][0] + buttons[1][2] - 40, buttons[1][1] + offset)
		ctx.lineTo(buttons[1][0] + buttons[1][2] - offset * 2, buttons[1][1] + buttons[1][3] / 2)
		ctx.lineTo(buttons[1][0] + buttons[1][2] - 40, buttons[1][1] + buttons[1][3] - offset)

		ctx.stroke()
	}

	public update(dt) {
		if (this.ang !== this.angDest) {
			let sign = Math.sign(this.angDest - this.ang)
			if (Math.abs(this.angDest - this.ang) > 180) {
				sign = -sign
			}
			this.ang = (this.ang + sign + 360) % 360
		} else {
			if (this.scanTimeout <= 0) {
				if (this.ang === 300 && this.selectedChannel === 4) this.status = 1
				else if (this.ang === 180 && this.selectedChannel === 1) this.status = 2
				else if (this.ang === 90 && this.selectedChannel === 7) this.status = 3
				else this.status = 0

				this.vars.has_con_049 = this.status === 1
				this.vars.has_con_840 = this.status === 2
				this.vars.has_con_216 = this.status === 3
			} else {
				this.scanTimeout -= dt
			}
		}
	}

	public click(x, y) {
		if (inBound(x, y, 60, 400, 130, 50)) {
			this.angDest = (this.angDest - 30 + 360) % 360
			this.scanTimeout = 500
			return
		}

		if (inBound(x, y, 220, 400, 130, 50)) {
			this.angDest = (this.angDest + 30) % 360
			this.scanTimeout = 500
			return
		}

		const n = channels.findIndex(ch => inBound(x, y, ...ch))
		if (n > -1) {
			this.selectedChannel = n
			this.scanTimeout = 500
			return
		}

		if (inBound(x, y, ...exitButton)) {
			this.vars.tune_antena_settings = {
				ang: this.ang,
				selectedChannel: this.selectedChannel
			}
			this.ready = true
			return
		}
	}

	public getCursor(x, y) {
		if (buttons.some(bound => inBound(x, y, ...bound))) {
			return 'pointer'
		}

		if (channels.some(channel => inBound(x, y, ...channel))) {
			return 'pointer'
		}

		if (inBound(x, y, ...exitButton)) {
			return 'pointer'
		}

		return 'default'
	}

	public get result() {
		return this.ready
	}
}
