"use strict"

let opt = {
	clearLog: false,
	debugLog: false,
	showDebug: true,
	showOnlyOrders: false,
	showOnlyWarns: false,
	showOnlyErrors: false,
	showOnlyBalance: false,
	showOnlyNotify: false,
	scrollLog: true,

	instName: '',
	disableAll: false,
	defExchangeUse: false,
	defExchange: 'BYBIT-TESTNET',
	defSymbolUse: false,
	defSymbol: 'BTCUSDT',
	defAccountUse: false,
	defAccount: '',
	ignPerm: false,
	ignAcc: false,
	runNoConfirm: false,
	runShowLog: true,
	keepApp: false,
	loadSync: false,
	alertLocal: true,
	accLocal: true,
	wildName: true,
//	childReplace: true,
	recvNow: false,
	retryBal: false,
	refreshAll: false,
	infoBadge: true,
	badgeAll: false,
	badgeAuto: false,
	badgeReset: true,
	statManual: true,
	statMinTime: 0.1,
	maxParallel: 0,
	maxTime: 60,
	autoRestart: 0,
	autoRestartWait: 3,
	logExpire: 3,
	fireOffset: 0,
	timeSync: 8,
	splitDelay: 0,
	defaultRes: 60,
	cmcAPI: "",

	priceExpire: 0,
	priceUpNoCache: false,
	infoExpire: 72,
	idExpire: 168,
	cmcExpire: 5,

	expConfAutoInt: "0 3",
	expLogAutoInt: "5 3",
	expLogInc: true,
	expBalAutoInt: "10 3",
	expBalMailInt: "10 3",
	expPosAutoInt: "15 3",
	expPosMailInt: "15 3",

	prefixTV: false,
	prefixTVmsg: "",
	onlyStream: true,
	autoTV: false,
	forceTV: false,
	forceTVmin: 5,
	timeoutTV: false,
	timeoutTVmin: 480,
	reconnectTV: false,
	suppressTV: true,
	noWarnTV: true,
	reloadTV: false,
	fixTV: true,
	newEx: true,
	screenFirst: false,
	screenRes: 60,
	tvScale: 0,

	legacyNoShort: true,
	legacyIgn: false,
	legacyIgnErr: false,
	legacyBarOnce: false,
	legacyBarSolo: false,
	legacyNoRepeat: false,
	legacyRepReset: false,
	legacyRepResetMin: 180,
	legacyBarOpen: true,
	legacyBarStart: true,
	legacyBarMid1: true,
	legacyBarMid2: true,
	legacyBarEnd: true,
	legacyBarClose: true,
	legacyBarPos: 0,
	legacyBarPA: false,
	legacyBarAmount: 0,

	posSym: "",
	posRate: 15,
	posAuto: true,
	posNone: true,
	posEmpty: false,
	posEx: "",
	posTest: true,
	posLog: true,
	posPar: true,
	posOnlyErr: false,
	posP: 0,
	posPA: 0,
	posPC: 0,
	posQ: 10,
	posQA: 50,
	posL: 10,
	posSL: 0,
	posTP: 0,
	posLmt: 0.1,
	posStp: 0.2,
	posStop: true,
	posTime: 1,
	posPost: true,

	onlyActAlert: false,
	onlyAutoAlert: false,
	onlyOneAlert: false,
	showFilter: true,
	popupMax: false,

	emailSubject: "TradeBot {action} Notification",
	emailQty: true,
	emailId: true,
	discordQty: true,
	discordEmo: true,
	discordBuy: ":arrow_up_small:",
	discordSell: ":arrow_down_small:",
	discordExit: ":orange_circle:",
	discordId: true,
	telegramQty: true,
	telegramEmo: true,
	telegramBuy: "⬆",
	telegramSell: "⬇",
	telegramExit: "⭘",
	telegramId: true,
	telebot: false,
	telebotStat: true,
	telebotAppCtrl: true,
	telebotAlrtCtrl: true,
	telebotAppRest: true,
	telebotAcc: true,
	telebotPos: true,
	telebotAlrt: true,
	telebotRunCmd: true,
	telebotRunAlrt: true,
	telebotOver: true,
	telebotCh: false,
	telebotChWL: "",
	telebotChRep: "",
	telebotChRunCmd: true,
	telebotChRunAlrt: true,
	telebotLog: true,
	telebotWL: "",
	telebotEx: "",
	telebotSym: "",
	telebotRes: 1,
	telebotEcho: 2,
	telebotSum: true,
	iftttHook: "https://maker.ifttt.com/trigger/{action}/with/key/insertYourKeyHere",
	twilioId: true,
	twilioBuy: "⬆",
	twilioSell: "⬇",
	twilioExit: "⭘",
}

const defExchangeOpt = {
	timeout: 3.5,
	retries: 5,
	retryorder: true,
	retrycancel: true
}

const isWin = navigator.platform.startsWith('Win')
const newLine = isWin ? '\r\n' : '\n'

function upgradeOptions() {
	delete opt.filterRepeat
	delete opt.filterFirst
	delete opt.filterLast
	delete opt.filterOffset
	opt.telegramBuy = opt.telegramBuy == ":arrow_up_small:" ? "⬆" : opt.telegramBuy
	opt.telegramSell = opt.telegramSell == ":arrow_down_small:" ? "⬇" : opt.telegramSell

	opt.legacyBarAmount = (opt.legacyBarTime && opt.legacyBarTimePerc) || opt.legacyBarAmount
	delete opt.legacyBarTime
	delete opt.legacyBarTimePerc

	opt.legacyBarMid1 = opt.legacyBarMid1 || opt.legacyBarMid || false
	delete opt.legacyBarMid

	opt.legacyRepReset = opt.legacyRepReset || opt.legacyRepeatReset || false
	opt.legacyRepResetMin = opt.legacyRepResetMin || opt.legacyRepeatResetMin || 0
	delete opt.legacyRepeatReset
	delete opt.legacyRepeatResetMin

	opt.emailBal = opt.emailBal || opt.emailBalance || false
	opt.discordBal = opt.discordBal || opt.discordBalance || false
	opt.telegramBal = opt.telegramBal || opt.telegramBalance || false
	delete opt.emailBalance
	delete opt.discordBalance
	delete opt.telegramBalance

	opt.keepApp = opt.keepApp || opt.keepPV || false
	opt.telebotPVCtrl !== undefined && (opt.telebotAppCtrl = opt.telebotPVCtrl)
	opt.telebotPVRest !== undefined && (opt.telebotAppRest = opt.telebotPVRest)
	delete opt.keepPV
	delete opt.telebotPVCtrl
	delete opt.telebotPVRest
}

function loadOptions() {
	try {
		if (localStorage.opt !== undefined) {
			opt = Object.assign(opt, JSON.parse(localStorage.opt))
//			upgradeOptions()
			updateIcon()
		}
	} catch(ex) {}
}
loadOptions()
upgradeOptions()

function saveOptions() {
	const nonce = Date.now()
	try {
		localStorage.opt = JSON.stringify(Object.without(opt, ['crc', 'sum']))
		localStorage.opt_ = nonce
		updateIcon()
	} catch(ex) {}
	return nonce
}

function updateIcon() {
	const hasAddon = bg.tvTabs && bg.tvLastPing && bg.tvLastPing < Date.now()
	const state = opt.disableAll ? 'd' : !bg.tvStreamStatus && !hasAddon ? 'w' : ''
	if (state != window.iconState) {
		window.iconState = state
		chrome.browserAction.setIcon({path: {"19": `img/icons/${state}19.png`, "38": `img/icons/${state}38.png`}})
	}

	const num = opt.badgeAll ? localStorage.badgeRcv : localStorage.badgeRun
	opt.infoBadge && num && chrome.browserAction.setBadgeBackgroundColor({
		color: [localStorage.badgeErr ? 128 : 0, localStorage.badgeErr ? 0 : 128, 0, 255]
	})
	chrome.browserAction.setBadgeText({text: opt.infoBadge && num || ''})
	chrome.browserAction.setTitle({
		title: `${pvName||''} ${pvVersion||''}${opt.disableAll ? " (PAUSED)":''}${num ? ` (${num} new)`:''}` + 
		(localStorage.badgeEvt || localStorage.badgeEvtLast ? `\nlast run: ${localStorage.badgeEvt || localStorage.badgeEvtLast}`:'') + 
		(bg.tvStreamStatus ? "\nConnected (Stream)" : hasAddon ? "\nConnected (Addon)" : "\nDisconnected!") +
		(bg.UUIDD ? ` – ${bg.UUIDD}`:'')
	})
}

Object.assignDeep = function() {
	let extended = {}
	function merge(obj) {
		for (const prop in obj) {
			if (Object.prototype.hasOwnProperty.call(obj, prop)) {
				if (Object.prototype.toString.call(obj[prop]) === '[object Object]') {
					extended[prop] = Object.assignDeep(extended[prop], obj[prop])
				} else {
					extended[prop] = obj[prop]
				}
			}
		}
	}
	for (const obj of arguments) {
		merge(obj)
	}
	return extended
}

function getObject(value, ...keys) {
	for (const key of keys) {
		if (value === null || !value.hasOwnProperty(key)) {
			return null
		}
		value = value[key]
	}
	return value
}

function setObject(...args) {
	let value = args.length ? args.pop() : null
	while (args.length) {
		let tmp = {}
		tmp[args.pop()] = value
		value = tmp
	}
	return value
}

function removeObject(value, ...keys) {
	const key = keys.shift() || null
	if (!key || !value.hasOwnProperty(key)) {
		return false
	}
	if (keys.length === 0) {
		delete value[key]
		return
	}
	removeObject(value[key], ...keys)
}

function run(generator, ...args) {
	let it = generator(...args)

	function next(err, arg) {
		let result
		try {
			result = err ? it.throw(err) : it.next(arg)
		} catch (err) {
			return it.throw(err)
		}

		if (result.done) {
			return arg
		}
		if (typeof result.value === 'function') {
			return result.value(next)
		}
		return next(null, result.value)
	}
	next()
}

function safeJSON(str) {
	if (typeof str === 'object' && str !== null) {
		return str
	}
	try {
		const json = JSON.parse(str)
		if (typeof json === 'object' && json !== null) {
			return json
		}
	} catch(ex){}
	return {}
}

function serialize(obj, prefix, doSort = false, doEncode = true) {
	let ret = []
	let keys = (obj && Object.keys(obj)) || []
	doSort && (keys = keys.sort())

	for (let key of keys) {
		const val = obj[key]
		key = prefix ? prefix+'['+key+']' : key
		ret.push(typeof val === 'object' ? serialize(val, key, doSort, doEncode) : 
			doEncode ? encodeURIComponent(key)+'='+encodeURIComponent(val) : key+'='+val)
	}
	return ret.join('&')
}

function _(data, utf8) {
	try {
		return utf8 ? decodeURIComponent(escape(atob(data))) : atob(data)
	} catch(ex) {
		//const location = ex.stack && ex.stack.match(/[^/]+?:\d+/g) || []
		//log.warn(`Decode error at ${location[1] || 'Unknown'}`)
	}
}

function utf8(data) {
	return unescape(encodeURIComponent(data))
}

function decimals(n) {
	const parts = (parseFloat(n) + 1).toString().split('.')
	return (parts[1] && parts[1].length) || 0
}

function randomFloat(min, max, precision = -1) {
	const decs = Math.max(decimals(min), decimals(max))
	const multi = Math.pow(10, decs + 1)
	min *= multi
	max *= multi
	const rnd = Math.floor(Math.random() * (max - min + 1)) + min
	return (rnd / multi).toFixed(precision < 0 ? decs : precision)
}

function clipboardWrite(format, data, cb) {
	const origCopy = document.oncopy
	document.oncopy = function(ev) {
		ev.clipboardData.setData(format, data)
		ev.preventDefault()
	}
	document.execCommand("copy", false, null)
	document.oncopy = origCopy

	cb = cb || (typeof toastr === 'object' ? toastr.success : alert)
	cb("Data copied to clipboard!")
}

function calculate_checksum(salt, bacon) {
	const p1 = "2E2ODJiZg"
	const p2 = "MjVhNWI"
	const p3 = "ZTNmZGQ3N2RlM"
	const p4 = "cfd8dc3000a"

	try {
		bacon = _(bacon.replace(/_/g, '/').replace(/-/g, '+'))
		salt = str_crc(salt, p4 + _(p2), p3 + p1)

		const eggs = bacon.substr(27, bacon.length-35) + bacon.substr(8, 3)
		bacon = bacon.substr(0, 8) + bacon.substr(11, 16) + bacon.substr(-8)

		if (bacon === salt) {
			return parseInt(eggs)
		}
	} catch(ex) {}
}

function shuffle(array) {
	let m = array.length
	while (m) {
		let i = Math.floor(Math.random() * m--)
		let t = array[m]
		array[m] = array[i]
		array[i] = t
	}
	return array
}

function timeoutSeconds(seconds, cb) {
	setTimeout(function(){
		cb(null, seconds)
	}, seconds * 1000)
}

function* sleep(seconds) {
	if (typeof seconds !== 'number' || isNaN(seconds) || seconds <= 0) {
		log.error("Invalid delay specified: " + seconds)
		return 0
	}
	return yield timeoutSeconds.bind(this, seconds)
}

function sortByIndex(array, index, reverse) {
	if (typeof index === 'string') {
		index = [index]
	}
	const up = reverse ? -1 : 1
	const down = reverse ? 1 : -1

	array.sort((a, b) => {
		let c = a
		let d = b
		index.forEach(key => {
			if (!(c = c[key])) {
				throw new ReferenceError("A Index was not found: " + index.join("."))
			}
			if (!(d = d[key])) {
				throw new ReferenceError("B Index was not found: " + index.join("."))
			}
		})
		return c === d ? 0 : c < d ? down : up
	})
}

function str_crc(salt, hash, blob) {
	return md5(_(blob) + salt + hash)
}

function ucfirst(str) {
	return str && str[0].toUpperCase() + str.substr(1)
}

function ucfirstx(str) {
	let offset = str && str[0] === '_' ? 1 : 0
	return str && str[offset++].toUpperCase() + str.substr(offset)
}

function ucfirsts(str) {
	return str && str.split(/[_ ]/).map(ucfirst).join(' ')
}

function urldecode(str) {
	str = str.replace(/%(?![\da-f]{2})/gi)
	str = str.replace(/\+/g, "%20")
	return decodeURIComponent(str)
}

function simpleHash(s) {
	let a = 1, c = 0, h, o
	if (s) {
		a = 0
		for (h = s.length - 1; h >= 0; h--) {
			o = s.charCodeAt(h)
			a = (a << 6 & 0xFFFFFFF) + o + (o << 14)
			c = a & 0xFE00000
			a = c !== 0 ? a ^ c >> 21 : a
		}
	}
	return a
}

const maxStringify = 10 * 1024

function stringify(obj, dec) {
	if (dec) {
		obj = Object.map(obj, (i, val) => isNaN(val) ? val : toDecimal(val, 10))
	}
	return JSON.stringify(obj, null, 1).
		replace(/\"/g, '').
		replace('[\n {', '{').
		replace(' }\n]', '}').
		replace(/,\n    /g, ', ').
		replace(/},\n /g, '},').
		substr(0, maxStringify)
}

function stringifyList(obj, dec) {
	if (dec) {
		obj = Object.map(obj, (i, val) => isNaN(val) ? val : toDecimal(val, 10))
	}
	return JSON.stringify(obj, null, 1).
		replace(/\"/g, '').
		replace('[\n {', '{').
		replace(' }\n]', '}').
		replace(/,\n    /g, ', ').
		replace(/},\n /g, '},').
		replace(/,\n  /g, ', ').
		replace(/\[\n  /g, '').
		replace(/\n \]/g, '').
		replace(/,\n custom(\d+): [^\n]*/g, ' (#$1)').
		substr(0, maxStringify)
}

function stringifyPretty(obj) {
	return JSON.stringify(obj, null, '\t').replace(/\n/g, newLine)
}

function changeCSS(name, value, cond) {
	let css = document.querySelector('#css-container')
	if (!css) {
		css = document.createElement('div')
		css.id = 'css-container'
		css.style.display = 'none'
		document.body.appendChild(css)
	}

	const sane = alnum(name, '-')
	let rule = css.querySelector(`[data-class="${sane}"]`)
	if (!rule) {
		rule = document.createElement('div')
		rule.dataset.class = sane
		css.appendChild(rule)
	}
	if (cond) {
		name = cond+'{'+name
		value += '}'
	}
	rule.innerHTML = `<style>${name}{${value}}</style>`
}

function yesno(msg, cb) {
	bootbox.confirm({
		message: msg || 'Are you sure?',
		title: '<span class="glyphicon glyphicon-question-sign"></span>  Confirm',
		buttons: {
			confirm: {
				label: '<span class="glyphicon glyphicon-ok"></span> Yes',
				className: 'btn-success'
			},
			cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> No',
				className: 'btn-danger'
			}
		},
		onShown: ev => ev.currentTarget.querySelector('.bootbox-cancel').focus(),
		swapButtonOrder: true,
		callback: cb ? function(result) {
			cb(null, result)
		} : null
	})
}

function noyes(msg, cb) {
	bootbox.confirm({
		message: msg || 'Are you sure?',
		title: '<span class="glyphicon glyphicon-question-sign"></span>  Confirm',
		buttons: {
			confirm: {
				label: '<span class="glyphicon glyphicon-ok"></span> Yes',
				className: 'btn-success'
			},
			cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> No',
				className: 'btn-danger'
			}
		},
		callback: cb ? function(result) {
			cb(null, result)
		} : null
	})
}

function onetwo(msg, one, two, cb) {
	bootbox.dialog({
		message: msg || 'Are you sure?',
		title: '<span class="glyphicon glyphicon-question-sign"></span>  Confirm',
	    onEscape: true,
		buttons: {
			one: {
				label: '<span class="glyphicon glyphicon-ok"></span> '+(one || 'Yes'),
				className: 'btn-success',
				callback: function() { cb(null, 1) }
			}, two: {
				label: '<span class="glyphicon glyphicon-ban-circle"></span> '+(two || 'No'),
				className: 'btn-warning',
				callback: function() { cb(null, 2) }
			}, cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> Cancel',
				className: 'btn-danger',
				callback: function() { cb(null, false) }
			}
		},
		onShown: ev => ev.currentTarget.querySelector('.bootbox-cancel').focus()
	})
}

function confirm(title, msg, cb) {
	bootbox.alert({
		message: msg,
		title: '<span class="glyphicon glyphicon-info-sign"></span>  '+title,
		onEscape: true,
		backdrop: true,
		buttons: {
			ok: {
				label: '<span class="glyphicon glyphicon-ok"></span> OK',
				className: 'btn-success'
			}
		},
		callback: cb ? function(result) { cb(null, result) } : null
	})
}

function ok(msg, cb) {
	confirm("Note!", msg, cb)
}

function error(msg, cb) {
	bootbox.alert({
		message: msg,
		title: '<span class="glyphicon glyphicon-alert"></span>  Error!',
		buttons: {
			ok: {
				label: '<span class="glyphicon glyphicon-remove"></span> OK',
				className: 'btn-danger'
			}
		},
		callback: cb ? function(result) { cb(null, result) } : null
	})
}

function ask(msg, title, value, cb) {
	bootbox.prompt({
		message: msg,
		title: '<span class="glyphicon glyphicon-question-sign"></span>  '+title,
		value: value,
		buttons: {
			confirm: {
				label: '<span class="glyphicon glyphicon-ok"></span> OK',
				className: 'btn-success'
			}, cancel: {
				label: '<span class="glyphicon glyphicon-remove"></span> Cancel',
				className: 'btn-danger'
			}
		},
		swapButtonOrder: true,
		callback: function(result) { cb(null, result) }
	})
}

function getLegacyPermissions(perm, merge) {
	let loose = {origins:[]}
	let legacy = {origins:[]}

	if (perm.origins) {
		for (const origin of perm.origins) {
			loose.origins.push(origin.replace(/\.(com|us|io)\/.*/, ".$1/*"))
			legacy.origins.push(origin.replace(/\/\/.*\.(.*).(com|us|io)\/.*/, "//*.$1.$2/*"))
		}
	}
	return merge ? {origins: [...perm.origins, ...loose.origins, ...legacy.origins]} : [loose, legacy]
}

function downloadAs(data, name, type = 'text/plain') {
	name = name.replace('%d', formatDateFN())

	const blobUrl = URL.createObjectURL(data.constructor.name === 'Blob' ? data : new Blob([data], {type: type}))
	setTimeout(function(){
		URL.revokeObjectURL(blobUrl)
	}, 40 * 1000)

	const link = document.createElement('a')
	link.href = blobUrl
	link.download = name
	link.style.display = 'none'
	document.body.appendChild(link)
	link.click()
	document.body.removeChild(link)
}

function openFile(type, cb) {
	const file = document.createElement('input')
	file.type = 'file'
	file.accept = type

	file.onchange = function(ev) {
		const fileObj = file.files[0]
		if (!fileObj) {
			console.log("openFile: no file object found - aborting...")
			cb(null, null)
			try { document.body.removeChild(file) }catch(ex){}
			return
		}
		const reader = new FileReader()
		reader.onload = function(ev) {
			const content = ev.target.result
			console.log(`openFile: got file contents! (${content.length} chars)`)
			cb(null, content)
			try { document.body.removeChild(file) }catch(ex){}
		}
		console.log(`openFile: trying to read contents of ${fileObj.name} (${fileObj.size} bytes)...`)
		reader.readAsText(fileObj)
	}
	file.style.display = 'none'
	document.body.appendChild(file)

	document.body.onfocus = function(ev) {
		document.body.onfocus = null
		setTimeout(function(){
			if (file.files && file.files.length < 1) {
				console.log("openFile: window focused and no file object - aborting...")
				cb(null, null)
				try { document.body.removeChild(file) }catch(ex){}
			}
		}, 500)
	}
	console.log("openFile: input element added - waiting for select...")
	file.click()
}

function incStat(name, add = 1, last, msg) {
	ls['statSession'+name] = Number(ls['statSession'+name] || 0) + add
	ls['statTotal'+name] = Number(ls['statTotal'+name] || 0) + add
	last && (ls['statLast'+name] = Date.now())
	msg && (ls[`statLast${name}Msg`] = msg)
}

function incStatAlt(name, add = 1) {
	ls['stat'+name] = Number(ls['stat'+name] || 0) + add
	ls['statTotal'+name] = Number(ls['statTotal'+name] || 0) + add
	ls['stat'+name+'Last'] = Date.now()
}

function minMaxStat(name, value, last) {
	ls['statSession'+name+'Min'] = Math.min(Number(ls['statSession'+name+'Min'] || Number.MAX_VALUE), value)
	ls['statSession'+name+'Max'] = Math.max(Number(ls['statSession'+name+'Max'] || 0), value)
	ls['statSession'+name+'Avg'] = (Number(ls['statSession'+name+'Avg'] || value) + value) / 2
	ls['statTotal'+name+'Min'] = Math.min(Number(ls['statTotal'+name+'Min'] || Number.MAX_VALUE), value)
	ls['statTotal'+name+'Max'] = Math.max(Number(ls['statTotal'+name+'Max'] || 0), value)
	ls['statTotal'+name+'Avg'] = (Number(ls['statTotal'+name+'Avg'] || value) + value) / 2
	last && (ls['statLast'+name] = value)
}

const discordAccess = {origins:["https://discord.com/api/*"], permissions:[]}
const telegramAccess = {origins:["https://api.telegram.org/*"], permissions:[]}
const twilioAccess = {origins:["https://api.twilio.com/*"], permissions:[]}
const iftttAccess = {origins:["https://maker.ifttt.com/*"], permissions:[]}
const addons = {Discord: discordAccess, Telegram: telegramAccess, Twilio: twilioAccess, IFTTT: iftttAccess}

function emailMessage(action, msg, dest, interactive, noRetry, attachData, attachName, attachType) {
	if (!localStorage.authToken || !perms.hasAll(bg.notify)) {
		!localStorage.authToken && log.debug("No Email auth token found!")
		return
	}
	if (!(dest || opt.emailAddress)) {
		(interactive ? error : log.warn)("No email addresses configured!")
		return false
	}
	let subject = action || ""
	subject = subject[0] === '-' ? subject.substr(1) : opt.emailSubject.replace('{action}', ucfirst(subject))

	const encodedMail = btoa(
		'Content-Type: multipart/mixed; boundary=foo_bar_baz\n'+
		'MIME-Version: 1.0\n'+
		`To: ${dest || opt.emailAddress}\n`+
		`Subject: ${subject}\n`+
		'\n'+
		'--foo_bar_baz\n'+
		'Content-Type: text/plain; charset="UTF-8"\n'+
		'Content-Transfer-Encoding: 7bit\n'+
		'\n'+
		utf8(msg)+'\n'+
		'\n'+
		(attachData ? '--foo_bar_baz\n'+
		`Content-Type: ${attachType}; name="${attachName}"\n`+
		`Content-Disposition: attachment; filename="${attachName}"\n`+
		'Content-Transfer-Encoding: base64\n'+
		'\n'+
		btoa(attachData)+'\n'+
		'\n' : '')+
		'--foo_bar_baz--'
	).replace(/\+/g, '-').replace(/\//g, '_')

	log.debug(`Sending Email notification for action/subject '${subject}'..`)
	fetch("https://www.googleapis.com/gmail/v1/users/me/messages/send", {
		method : 'POST',
		headers: {
			'Authorization': 'Bearer '+localStorage.authToken,
			'Content-Type': 'application/json'
		},
		body: JSON.stringify({raw: encodedMail})
	}).then(resp => {
		log.debug("Email server response code: "+resp.status)
		if (!resp.ok) {
			return resp.json().then(resp => {
				throw new Error(resp.error.code+" "+resp.error.message)
			})
		}
		incStatAlt('Email')
		if (typeof interactive === 'string') {
			ok(interactive)
		}
	}).catch(ex => {
		if (!noRetry && ex.message.startsWith('401')) {
			log.warn("Email token seems to have expired, retrying...")
			incStatAlt('EmailAuth')
			updateAuthToken(false, function(){
				emailMessage(action, msg, dest, interactive, true, attachData, attachName, attachType)
			})
		} else {
			incStatAlt('EmailErr')
			;(interactive ? error : log.error)("Error sending Email notification: "+ex.message)
		}
	})
	return true
}

function updateAuthToken(interactive, cb) {
	if (!interactive && localStorage.authToken) {
		chrome.identity.removeCachedAuthToken({token: localStorage.authToken})
	}
	if (interactive) {
		showSpinner("Requesting email permission...")
	}
	chrome.identity.getAuthToken({"interactive": interactive}, function(token) {
		void chrome.runtime.lastError

		if (localStorage.authToken && !token && (!chrome.runtime.lastError || !chrome.runtime.lastError.message.includes("Connection failed"))) {
			log.warn("Permission removed: Email")
			delete localStorage.authToken
		} else if (token) {
			if (!localStorage.authToken) {
				log.success("Permission granted: Email")
			}
			localStorage.authToken = token
		}

		try {
			const emailPanel = document.querySelector("[data-addon='Email']").closest(".panel")
			setButton(emailPanel, localStorage.authToken)
			localStorage.authToken && emailPanel.classList.remove("disabled")
			!localStorage.authToken && emailPanel.classList.add("disabled")
		} catch(ex) {}

		if (!token && chrome.runtime.lastError && !chrome.runtime.lastError.message.includes("OAuth2 not granted or revoked")) {
			const msg = "Error requesting Email auth token: "+chrome.runtime.lastError.message
			log.error(msg)
			if (interactive) {
				error(msg, hideSpinner)
			}
		} else if (interactive) {
			hideSpinner()
		}
		cb && cb(token)
	})
}

function discordMessage(action, msg, dest, interactive) {
	if (!perms.hasAny(getLegacyPermissions(discordAccess, true)) || !perms.hasAll(bg.notify)) {
		return
	}
	if (!opt.discordHook || !opt.discordHook.startsWithAny(["https://discordapp.com/api/", "https://discord.com/api/"])) {
		(interactive ? error : log.warn)("Discord Webhook URL not configured or incorrect!")
		return false
	}
	if (dest && !dest.startsWithAny(["https://discordapp.com/api/webhooks/", "https://discord.com/api/webhooks/"])) {
		dest = "https://discord.com/api/webhooks/"+dest
	}

	log.debug(`Sending Discord notification for action '${action}'..`)
	fetch((dest || opt.discordHook).replace("https://discordapp.com/", "https://discord.com/"), {
		method : 'POST',
		headers: {'Content-Type': 'application/json'},
		body: JSON.stringify({
			content: msg
		})
	}).then(resp => {
		log.debug("Discord server response code: "+resp.status)
		if (!resp.ok) {
			return resp.json().then(resp => {
				throw new Error(resp.message)
			})
		}
		incStatAlt('Discord')
		if (typeof interactive === 'string') {
			ok(interactive)
		}
	}).catch(ex => {
		incStatAlt('DiscordErr')
		;(interactive ? error : log.error)("Error sending Discord notification: "+ex.message)
	})
	return true
}

function telegramErr(resp) {
	return `${resp.statusText || ({
		502: 'Bad Gateway – connection error',
		500: 'Internal Telegram error',
		420: 'Flood – maximum allowed number of attempts has been exceeded',
		409: 'Conflict: terminated by other getUpdates request; make sure that only one bot instance is running',
		404: 'Bot Token missing or invalid',
		403: 'Privacy violation',
		401: 'Unauthorized attempt to use functionality available only to authorized users',
		400: 'The query contains errors',
		303: 'The request must be repeated, but directed to a different data center',
	}[resp.status] || 'Unknown connection error')} (${resp.status})`
}

function telegramMessage(action, msg, dest, interactive) {
	if (!perms.hasAny(getLegacyPermissions(telegramAccess, true)) || !perms.hasAll(bg.notify)) {
		return
	}
	if (!opt.telegramToken || !opt.telegramChat) {
		(interactive ? error : log.warn)("Telegram Bot Token / Chat ID not configured!")
		return false
	}

	log.debug(`Sending Telegram notification for action '${action}'..`)
	fetch(`https://api.telegram.org/bot${opt.telegramToken}/sendMessage`, {
		method : 'POST',
		headers: {'Content-Type': 'application/json'},
		body: JSON.stringify({
			chat_id: dest || opt.telegramChat,
			text: msg,
			parse_mode: 'Markdown'
		})
	}).then(resp => {
		log.debug("Telegram server response code: "+resp.status)
		if (!resp.ok) {
			throw new Error(telegramErr(resp))
		}
		incStatAlt('Telegram')
		if (typeof interactive === 'string') {
			ok(interactive)
		}
	}).catch(ex => {
		incStatAlt('TelegramErr')
		const msg = ex.message
		;(interactive ? error : log.error)("Error sending Telegram notification: "+msg+(msg.startsWith('400') ? " (escape all single underscores in custom messages or use dashes instead!)" : ""))
	})
	return true
}

function twilioMessage(action, msg, dest, interactive) {
	if (!perms.hasAny(getLegacyPermissions(twilioAccess, true)) || !perms.hasAll(bg.notify)) {
		return
	}
	if (!opt.twilioSID || !opt.twilioToken) {
		(interactive ? error : log.warn)("Twilio Account SID / Auth Token not configured!")
		return false
	}
	if (!opt.twilioFrom || !(dest || opt.twilioTo)) {
		(interactive ? error : log.warn)("Twilio From/To # not configured!")
		return false
	}

	log.debug(`Sending Twilio notification for action '${action}'..`)
	fetch(`https://api.twilio.com/2010-04-01/Accounts/${opt.twilioSID}/Messages.json`, {
		method : 'POST',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
			'Authorization': 'Basic '+btoa(opt.twilioSID+':'+opt.twilioToken)
		},
		body: serialize({
			To: dest || opt.twilioTo,
			From: opt.twilioFrom,
			Body: msg
		})
	}).then(resp => {
		log.debug("Twilio server response code: "+resp.status)
		if (!resp.ok) {
			throw new Error(resp.status+" "+resp.statusText)
		}
		incStatAlt('Twilio')
		if (typeof interactive === 'string') {
			ok(interactive)
		}
	}).catch(ex => {
		incStatAlt('TwilioErr')
		const msg = ex.message
		;(interactive ? error : log.error)("Error sending Twilio notification: "+ex.message)
	})
	return true
}

function iftttMessage(action, msg, dest, interactive) {
	if (!perms.hasAny(getLegacyPermissions(iftttAccess, true)) || !perms.hasAll(bg.notify)) {
		return
	}
	if (!opt.iftttHook || !opt.iftttHook.startsWith("https://maker.ifttt.com/")) {
		(interactive ? error : log.warn)("IFTTT Webhook URL not configured or incorrect!")
		return false
	}

	action = pvShortU+ucfirst(action)
	log.debug(`Sending IFTTT notification for action '${action}'..`)
	msg && (msg = msg.split(';'))
	fetch(opt.iftttHook.replace('{action}', encodeURIComponent(action.replace(/ /g, '_'))), {
		method : 'POST',
		headers: {'Content-Type': 'application/json'},
		body: JSON.stringify(msg ? {
			value1: msg[0] || '',
			value2: msg[1] || '',
			value3: msg[2] || ''
		} : {})
	}).then(resp => {
		log.debug("IFTTT server response code: "+resp.status)
		if (!resp.ok) {
			return resp.json().then(resp => {
				throw new Error(resp.errors[0].message)
			})
		}
		incStatAlt('IFTTT')
		if (typeof interactive === 'string') {
			ok(interactive)
		}
	}).catch(ex => {
		incStatAlt('IFTTTErr')
		;(interactive ? error : log.error)("Error sending IFTTT notification: "+ex.message)
	})
	return true
}

function* checkNotifications() {
	try {
		const result = yield request.bind(this, 'POST-', _('aHR0cHM6Ly9wcm9maXR2aWV3LmFwcC9pcG4vcGluZy8'), __(ID, UUIDD, GUID, accountNum, 
			Math.ceil(ls.statBootupFirst / 86400000), Math.round((Number(ls.statUptimeTotal) + Number(ls.statUptime)) / 86400), 
			ls.statTotalAlerts, perms.count(), exNum, pvVersion, localStore.get().perms.crc || 0), {})

		if (!result || result.code !== 200) {
			throw new Error(result.message || "Unknown")
		}
		if (result.payload) {
			const ops = [], size = 43, data = result.payload, crc = parseInt(data)
			let i = crc.toString().length, left = data.length - i, msg = null
			while (left >= size) {
				const len = left < 2 * size ? left : size
				const op = _(data.substr(i, len))
				if (op[0] === '-') (msg = perms.filter().save(true))
				else ops.push(op)
				i += len; left -= len
			}
			localStore.get().perms.crc = crc
			ops.length && (msg = perms.grant(ops).save())
			msg && chrome.runtime.sendMessage(null, {method: 'refresh'})
		}
		opt.sum = result.nonce
		ls.statPing = Date.now()
	} catch(ex){
		incStat('PingErr', 1, true, ex.message)
	}
}

function wildcardExp(wildcard = "", startsWith, endsWith) {
	return new RegExp((endsWith ? '' : '^') + 
		wildcard.split('*').map(str => str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1')).join('.*') + (startsWith ? ''/*'.*$'*/ : '$'), 'i')
}

Object.filter = function(obj, check) {
	let result = {}
	for (const key in obj) {
		if (obj.hasOwnProperty(key) && check(key, obj[key])) {
		    result[key] = obj[key]
		}
	}
	return result
}

Object.map = function(obj, check) {
	let result = {}, ret
	for (const key in obj) {
		if (obj.hasOwnProperty(key) && (ret = check(key, obj[key])) != null) {
		    result[key] = ret
		}
	}
	return result
}

Object.each = function(obj, cb) {
	for (const key in obj) {
		obj.hasOwnProperty(key) && cb(key, obj[key])
	}
}

Object.without = (obj, list) => Object.filter(obj, key => !list.includes(key))

Object.filterAsArray = function(obj, check) {
	let result = []
	for (const key in obj) {
		if (obj.hasOwnProperty(key) && check(key, obj[key])) {
			result.push(obj[key])
		}
	}
	return result
}

Object.mapAsArray = function(obj, check) {
	let result = [], ret
	for (const key in obj) {
		if (obj.hasOwnProperty(key) && (ret = check(key, obj[key]))/* != null*/) {
			result.push(ret)
		}
	}
	return result
}

Object.isEmpty = function(obj) {
	if (obj) for (const key in obj) {
		return false
	}
	return true
}

Object.sort = obj => Object.keys(obj).sort().reduce((res, key) => (res[key] = obj[key], res), {})

Object.defineProperty(Object.prototype, 'getProp', {
	value: function (prop, def = null) {
		prop = prop.toLowerCase()
		if (Array.isArray(this)) {
			for (const item of this) {
				if (item.toLowerCase() === prop) {
					return item
				}
			}
		} else {
			for (const key in this) {
				if (key.toLowerCase() === prop) {
					return this[key]
				}
			}
		}
		return def
	},
	enumerable: false
})

Object.defineProperty(Object.prototype, 'getAny', {
	value: function (list, def = null) {
		if (list) {
			for (const item of list) {
				if (this.hasOwnProperty(item) && this[item] !== null) {
					return this[item]
				}
			}
		}
		return def
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'part', {
	value: function (filter) {
		let pass = [], fail = []
		this.forEach((e, idx, arr) => (filter(e, idx, arr) ? pass : fail).push(e))
		return [pass, fail]
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'includesNoCase', {
	value: function (key, wild) {
		// wild -1 ==> key: wildcards array: literal
		// wild  0 ==> key: literal   array: literal
		// wild  1 ==> key: literal   array: wildcards
		// wild  2 ==> key: literal   array: both (quotes = literal)
		(!wild || wild > 1) && (key = key && key.toLowerCase())
		wild < 0 && (key = wildcardExp(key))
		for (const item of this) {
			if (wild < 0 ? key.test(item) : 
				(!wild && item.toLowerCase() === key) || 
				(wild > 1 && item[0] === `"` && item.slice(1, -1).toLowerCase() === key) || // $$$ unquote?
				(wild && wildcardExp(item).test(key))) {
				return true
			}
		}
		return false
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'includesAny', {
	value: function (list) {
		if (list) {
			let i = 1
			for (const item of list) {
				if (this.includes(item)) {
					return i
				}
				++i
			}
		}
		return 0
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'contains', {
	value: function (str) {
		return this.find(item => item && item.includes && item.includes(str))
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'without', {
	value: function (list) {
		if (list && list.includes) {
			return this.filter(el => !list.includes(el))
		}
		return this
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'last', {
	value: function () {
		return this[this.length - 1]
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, '_next', {
	value: function () {
		this._ = this._ || 0
		return this._ >= this.length ? undefined : this[this._++]
	},
	enumerable: false
})
Object.defineProperty(Array.prototype, '_prev', {
	value: function () {
		this._ = this._ || 0
		return this._ > 0 ? this[--this._] : undefined
	},
	enumerable: false
})
Object.defineProperty(Array.prototype, 'remain', {
	value: function () {
		return this.length - (this._ || 0)
	},
	enumerable: false
})
Object.defineProperty(Array.prototype, 'isStart', {
	value: function () {
		return (this._ || 0) < 1
	},
	enumerable: false
})
Object.defineProperty(Array.prototype, 'isEnd', {
	value: function () {
		return (this._ || 0) >= this.length
	},
	enumerable: false
})
Object.defineProperty(Array.prototype, 'gotoEnd', {
	value: function (offset) {
		this._ = Math.max(this.length + offset, 0)
		return this
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'reset', {
	value: function () {
		delete this._
		return this
	},
	enumerable: false
})

Object.defineProperty(Array.prototype, 'numbers', {
	value: function (split) {
		return this.map(e => !isNaN(e) ? Number(e) : split ? e.trim().splitOne(/[:=]/, true).numbers() : e.trim())
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'startsWithAny', {
	value: function (list, wild) {
		if (list) {
			let i = 1
			for (const item of list) {
				if ((!wild && (item.exec && item.test ? item.test(this) : this.startsWith(item))) || 
					(wild && wildcardExp(item, true).test(this))) {
					return i
				}
				++i
			}
		}
		return false
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'includesNoCase', {
	value: function (str) {
		return str && str.toLowerCase && this.toLowerCase().includes(str.toLowerCase())
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'contains', {
	value: function (str) {
		return this.includes(str)
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'count', {
	value: function (ch, from, to) {
		let count = 0
		for (let i = from; i < to; ++i) {
			this[i] === ch && ++count
		}
		return count
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'trimSep', {
	value: function () {
		return this.replace(/^[\s,;]*/, '').replace(/[\s,;]*$/, '')
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'trimList', {
	value: function () {
		return unquote(this.trim()).split(',')
	},
	enumerable: false
})

Object.defineProperty(String.prototype, 'splitOne', {
	value: function (sep, delist) {
		let i1 = -1, i2 = sep.length
		if (sep.constructor === RegExp) {
			const res = this.match(sep)
			if (res) {
				i1 = res.index
				i2 = res[0].length
			}
		} else {
			i1 = this.indexOf(sep)
		}
		let first = i1 < 0 ? this : this.slice(0, i1)
		let second = []
		if (i1 >= 0) {
			i2 += i1
			second = delist ? this.slice(i2).trimList() : [this.slice(i2)]
		}
		return [first, ...second]
	},
	enumerable: false
})

const tzo = new Date().getTimezoneOffset() * 60000

function formatDate(date, suffix, na = "n/a") {
	if (!date || date === "0") return na
	try {
		const str = new Date((!isNaN(date) || date.toISOString ? date : new Date(date)) - tzo).toISOString()
		return str.replace('T', ' ').slice(0, suffix ? -1 : -5)
	} catch(ex){}
	return na
}

function formatDateS(date, suffix, na = "n/a") {
	if (!date || date === "0") return na
	try {
		const str = new Date((!isNaN(date) || date.toISOString ? date : new Date(date)) - tzo).toISOString()
		return str.replace('T', ' ').slice(5, suffix ? -1 : -5)
	} catch(ex){}
	return na
}

function formatDateSP(date, suffix, na = "n/a") {
	const parts = formatDateS(date, suffix, na).split(' ')
	return `${parts[0]} <span class="detail">${parts[1]||''}</span>`
}

function formatDateFN(date = new Date()) {
	return formatDate(date).replace(/[^0-9]/g, '-')
}

function tillNow(date) {
	return (Date.now() - (date.toISOString ? date : new Date(date))) / 1000
}

function uniqueID() {
	return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,
		c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16))
}

function alnum(str, rep = '') {
	return (str || '').replace(/[^a-zA-Z0-9]/g, rep)
}

function alnumStart(str) {
	return str && str.match && str.match(/\w+/)[0]
}

function formatFields(obj, fields, format, pad, suffix = "", esc) {
	format = format || "\n %f: %v,"
	let result = ""
	let padding = 0

	if (obj && fields) {
		if (pad) {
			for (const field of fields) {
				if (obj.getProp(field)) {
					padding = Math.max(padding, field.length)
				}
			}
			padding += suffix.length
		}

		let prev = ""
		for (const field of fields) {
			const curr = field.toLowerCase()
			if (prev === curr) continue
			let val = obj.getProp(prev = curr)
			if (val && val != 'null') {
				val = (typeof val.toJSON === 'function') ? val.toJSON() : val
				result += format.replace(/%f/g, ucfirstx(field+suffix).padEnd(padding)).replace(/%v/g, esc && typeof val === 'string' ? esc(val) : val)
			}
		}
	}
	return result
}

function __(str) {
	const nonce = Math.floor(Math.random() * 0x7FFFFFFF)
	const pay = simpleHash(str) ^ nonce
	str = Array.prototype.slice.call(arguments).join('|')
	let result = ''
	for (let i=0; i < str.length; ++i) {
		result += String.fromCharCode(str.charCodeAt(i) ^ (pay & 0xFF))
	}
	result = nonce.toString(16).padStart(8, '0') + btoa(result).replace(/=+$/g, '').replace(/\//g, '_').replace(/\+/g, '-') + pay.toString(16).padStart(8, '0')
	return result
}

const telescape = str => str.replace(/\*/g, '⋆'/*✲*/).replace(/_/g, ' ̲ '/*＿*/)
//const telescape = str => str.replace(/[*_]/g, '\\$&')

function mustacheFields(obj, str, doEscape) {
	if (!obj || !str) {
		return str
	}
	return str.replace(/{([^{}]+)}/g, (match, field) => {
		let val
		if (field.length > 2 && /[+\-*/%^]/.test(field)) {
			field = field.split(':')
			val = Command.solveExp(field[0], obj)
		} else {
			field = field.split(/[.:]/)
			val = globalVars.ohlc(field[0], obj, null, true)
		}
		if (val === null || val === undefined) return match
		val = typeof val.toJSON === 'function' ? val.toJSON() : val
		if (field[1]) {
			if (!isNaN(val)) {
				val = Number(val).toFixed(field[1])
			} else if (val.split) {
				val = val.split(/[-\s]/).slice(0, field[1]).join('-')
			}
		} else if (!isNaN(val)) {
			val = toDecimal(val, 10)
		}
		return doEscape && typeof val === 'string' ? telescape(val) : val
    })
}

function intervalMatch(exp, time) {
	const checkOnly = time === undefined
	time = time || new Date()
	function err(msg) {
		if (!checkOnly) {
			log.warn(msg)
			return false
		}
		return msg
	}

	const parts = exp.toString().split(' ').filter(Boolean)
	if (!parts.length) {
		return err(`Syntax error in interval expression (no part given): "${exp}"`)
	}

	let i = 0
	for (const part of parts) {
		if (++i > 6) return err(`Syntax error in interval expression (more than 6 parts given): "${exp}"`)
		if (part === "*") continue

		let value = 
			i === 1 ? time.getMinutes() : 	// 0-59
			i === 2 ? time.getHours() : 	// 0-23
			i === 3 ? time.getDate() : 		// 1-31
			i === 4 ? time.getMonth()+1 : 	// 1-12
			i === 5 ? time.getDay() : 		// 0-6
					time.getFullYear()

		if (part.startsWith("*/") || ((i < 3 || i > 4) && part.startsWith("0/")) || ((i >= 3 && i <= 4) && part.startsWith("1/"))) {
			const every = Number(part.slice(2))
			if (!every || isNaN(every)) {
				return err(`Syntax error in interval expression (part ${i}, every): "${exp}"`)
			}
			i === 4 && --value
			if (value % every && !checkOnly) {
				return false
			}

		} else if (part.includes('-')) {
			const range = part.split('-').map(el => el !== '' ? Number(el) : NaN)
			if (range.length != 2 || range.includes(NaN)/* || range[0] > range[1]*/) {
				return err(`Syntax error in interval expression (part ${i}, range): "${exp}"`)
			}
			if ((range[0] > range[1] ? 
					(value < range[0] && value > range[1]) : 
					(value < range[0] || value > range[1])) 
					&& !checkOnly) {
				return false
			}

		} else if (part.includes(',')) {
			const list = part.split(',').map(el => Number(el))
			if (list.includes(NaN)) {
				return err(`Syntax error in interval expression (part ${i}, list): "${exp}"`)
			}
			if (!list.includes(value) && !checkOnly) {
				return false
			}

		} else if (part != value) {
			if (isNaN(part)) {
				return err(`Syntax error in interval expression (part ${i}, value): "${exp}"`)
			}
			if (!checkOnly) {
				return false
			}
		}
	}
	return !checkOnly
}

function interval2Text(exp) {
	let min = exp[0], hour = exp[1], day = exp[2], month = exp[3], dayof = exp[4]
	let hourDone = false
	let desc = ""

	if (min == '*') {
		desc += "Every minute"
	} else if (min.startsWith('*/')) {
		desc += `Every ${min.substr(2)} minutes`
	} else if (min.includes('-')) {
		min = min.split('-')
		if (hour && !isNaN(hour)) {
			hour = hour.padStart(2,'0')
			desc += `Every minute between ${hour}:${min[0].padStart(2,'0')} and ${hour}:${min[1].padStart(2,'0')}`
			hourDone = true
		} else {
			desc += `Minutes ${min[0]} through ${min[1]} past the hour`
		}
	} else {
		min = min.split(',')
		if (min.length == 1 && hour) {
			if (!isNaN(hour)) {
				desc += `At ${hour.padStart(2,'0')}:${min[0].padStart(2,'0')}`
				hourDone = true
			} else if (hour.includes(',')) {
				hour = hour.split(',')
				desc += "At "
				do {
					const val = hour._next().padStart(2,'0')
					desc += (hour._ < 2 ? "" : hour.isEnd() ? " and " : ", ") + `${val}:${min[0].padStart(2,'0')}`
				} while (!hour.isEnd())
				hourDone = true
			}
		}
		if (!hourDone) {
			desc += "At "
			do {
				const val = min._next()
				desc += (min._ < 2 ? "" : min.isEnd() ? " and " : ", ") + val
			} while (!min.isEnd())
			desc += " minutes past the hour"
		}
	}
	if (!hourDone && hour) {
		if (hour.startsWith('*/')) {
			desc += `, every ${hour.substr(2)} hours`
		} else if (hour.includes('-')) {
			hour = hour.split('-').map(e => e.padStart(2,'0'))
			desc += `, between ${hour[0]}:00 and ${hour[1]}:59`
		} else if (hour != '*') {
			hour = hour.split(',').map(e => e.padStart(2,'0'))
			if (hour.length == 1) {
				desc += `, between ${hour[0]}:00 and ${hour[0]}:59`
			} else {
				desc += ", at "
				do {
					const val = hour._next()
					desc += (hour._ < 2 ? "" : hour.isEnd() ? " and " : ", ") + val + ":00"
				} while (!hour.isEnd())
			}
		}
	}
	if (day && day != '*') {
		if (day.startsWith('*/')) {
			desc += `, every ${day.substr(2)} days`
		} else if (day.includes('-')) {
			day = day.split('-')
			desc += `, between day ${day[0]} and ${day[1]} of the month`
		} else {
			day = day.split(',')
			desc += ", on day "
			do {
				const val = day._next()
				desc += (day._ < 2 ? "" : day.isEnd() ? " and " : ", ") + val
			} while (!day.isEnd())
			desc += " of the month"
		}
	} else if (!dayof || dayof == "*") {
		desc += ", daily"
	}
	const dayName = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
	if (dayof && dayof != '*') {
		if (dayof.startsWith('*/')) {
			desc += `, every ${dayof.substr(2)} days of the week`
		} else if (dayof.includes('-')) {
			dayof = dayof.split('-')
			desc += `, ${dayName[dayof[0]]} through ${dayName[dayof[1]]}`
		} else {
			dayof = dayof.split(',')
			desc += ", only on "
			do {
				const val = dayof._next()
				desc += (dayof._ < 2 ? "" : dayof.isEnd() ? " and " : ", ") + dayName[val]
			} while (!dayof.isEnd())
		}
	}
	const monthName = ["", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
	if (month && month != '*') {
		if (month.startsWith('*/')) {
			desc += `, every ${month.substr(2)} months`
		} else if (month.includes('-')) {
			month = month.split('-')
			desc += `, ${monthName[month[0]]} through ${monthName[month[1]]}`
		} else {
			month = month.split(',')
			desc += ", only in "
			do {
				const val = month._next()
				desc += (month._ < 2 ? "" : month.isEnd() ? " and " : ", ") + monthName[val]
			} while (!month.isEnd())
		}
	}
	return desc
}

function unquote(str) {
	if (str) {
		if ((str[0] === '"' || str[0] === '\'') && (str[str.length-1] === '"' || str[str.length-1] === '\'')) {
			str = str.slice(1, -1)
		}
		str = str.replace(/\\n/g, '\n').replace(/\\t/g, '\t').replace(/\\z/g, '‌').replace(/\\\\/g, '\\')
	}
	return str
}

function getAlerts(id, def) {
	const store = localStore.get('hasAlerts') ? localStore.get() : syncStore.get()
	if (id !== undefined) {
		return store['alert_'+id] || def
	}
	return Object.filterAsArray(store, key => key.startsWith('alert_'))
}

function getAlertsByName(name) {
	if (isNaN(name)) {
		return name === '*' ? getAlerts() : getAlerts().filter(alert => alert.names && alert.names.includesNoCase(name, -1))
	} else {
		const result = getAlerts(name)
		return (result && [result]) || []
	}
}

function showSpinner(msg) {
	 try {
		$("#spinner").modal('show')
		q(".modal-backdrop:last-child").style.zIndex = 1999
	} catch(ex){}	
}

function hideSpinner() {
	 try {
		$("#spinner").modal('hide')
	} catch(ex){}	
}

function getTF(res, def = opt.defaultRes) {
	let val = parseFloat(res) || def
	switch(res && res.slice && res.slice(-1)) {
		case 'M':
			val *= 43800; break
		case 'W':
		case 'w':
			val *= 10080; break
		case 'D':
		case 'd':
			val *= 1440; break
		case 'H':
		case 'h':
			val *= 60; break
		case 'S':
		case 's':
			val /= 60; break
	}
	return val
}

function formatTF(res = 60) {
	if (res % 43800 === 0) {
		return (res / 43800) + "M"
	} else if (res % 10080 === 0) {
		return (res / 10080) + "w"
	} else if (res % 1440 === 0) {
		return (res / 1440) + "d"
	} else if (res % 60 === 0) {
		return (res / 60) + "h"
	} else if (res && res < 1) {
		return (res * 60).toFixed(0) + "s"
	}
	return res + "m"
}

function formatDur(dur, floats = 0, na, mark) {
	if (isNaN(dur) || (na !== undefined && dur == na)) return "n/a"
	let str = ""
	if (dur >= 86400) {
		str += Math.floor(dur / 86400) + "d "
	}
	if (dur >= 3600) {
		str += Math.floor(dur / 3600) % 24 + "h "
		if (mark) str += `<span class="detail">`
	}
	if (dur >= 60) {
		str += Math.floor(dur / 60) % 60 + "m "
		if (dur < 3600 && mark) str += `<span class="detail">`
	}
	if (floats < 0) {
		floats = dur < 60 ? -floats : 0
	}
	if (floats || !dur || dur % 60) {
		str += (floats ? (dur % 60).toFixed(floats) : Math.round(dur % 60)) + "s"
	} else if (str.length) {
		str = str.slice(0, -1)
	}
	if (mark) str += `</span>`
	return str
}

function formatDurP(dur, floats = 0, na) {
	return formatDur(dur, floats, na, true)
}

function alertSort(a, b) {
	a = Number(a.sort || a.id)
	b = Number(b.sort || b.id)
	return a < b ? -1 : a > b ? 1 : 0
}

function upgradeAlert(alert) {
	alert.barPos = alert.barPos || 0
	alert.barPA = alert.barPA || false
	alert.barAmount = (alert.barTime && alert.barTimePerc) || alert.barAmount || 0
	delete alert.barTime
	delete alert.barTimePerc

	alert.barMid1 = alert.barMid1 || alert.barMid || false
	delete alert.barMid

	alert.repReset = alert.repReset || alert.repeatReset || false
	alert.repResetMin = alert.repResetMin || alert.repeatResetMin || 0
	alert.recv = alert.recv || alert.received || ''
	alert.receivedNum = alert.recvNum || alert.receivedNum || 0
	delete alert.repeatReset
	delete alert.repeatResetMin
	delete alert.received
	delete alert.receivedNum

	alert.log = alert.log || (alert.noLog === false ? 15 : 0)
	delete alert.noLog
}

function timeAdd(ts, add) {
	return new Date(ts).getTime() + add * 1000
}

function minmax(val, min = 0, max = Number.MAX_VALUE) {
	return Math.max(Math.min(val, max), min)
}

function lineNumbers(str) {
	const lines = Array.isArray(str) ? str : str.split('\n')
	const digits = lines.length.toString().length
	return lines.map((line, i) => (i+1).toString().padStart(digits, ' ') + ": " + line).join('\n')
}

function findSyntax(what, syntax, startRow = 0) {
	!Array.isArray(syntax) && (syntax = syntax.split('\n'))
	const exp = RegExp('(^|\\s|#)('+what+')=([\\w\-_:.\*]*)', 'i')

	let row = startRow
	let match
	do {
		if ((match = syntax[row].match(exp))) {
			return match[3]
		}
	} while (--row >= 0)

	row = startRow
	while (++row < syntax.length) {
		if ((match = syntax[row].match(exp))) {
			return match[3]
		}
	}
	return null
}

function isEmail(str) {
	return str && /^\S+@\S+\.\S+$/.test(str) || false
}

function toPrecision(num, precision) {
	const f = 10 ** Math.floor(precision)
	return Math.round((Number(num) + Number.EPSILON) * f) / f
}

function toDecimal(num, maxPrecision = 8) {
	// x.toLocaleString('fullwide', {useGrouping: false, maximumSignificantDigits: 20}) ?
	if (!num || !num.toString) return num
	const str = num.toString()
	const parts = str.split('e-')
	if (parts.length > 1) {
		const exp = parts[0].length
		const power = +parts[1]
		return num.toFixed(Math.min(maxPrecision, power + exp - Math.min(exp, 2)))
	}
	return str
}

function chromeCB(cb, result) {
	return cb(chrome.runtime.lastError, result)
}

function chromeIdentity(cb) {
	try {
		chrome.identity.getProfileUserInfo({accountStatus: 'ANY'}, chromeCB.bind(this, cb))
	} catch(ex) {
		cb(null, null)
	}
}

function chroneCB(cb, result) {
	return !window[_('dHZMYXN0UGluZzI')] ? window[_('Y2hyb21lQ2hlY2s')](cb(result)) : cb(result)
}

function getPerms(cb) {
	chrome.permissions.getAll(chromeCB.bind(this, cb))
}

function requestPerms(perms, cb) {
	chrome.permissions.request(perms, chromeCB.bind(this, cb))
}

function removePerms(perms, cb) {
	chrome.permissions.remove(perms, chromeCB.bind(this, cb))
}

function tabsQuery(queryInfo, cb) {
	chrome.tabs.query(queryInfo, chromeCB.bind(this, cb))
}

function tabsReload(tabIds, noCache, cb) {
	chrome.tabs.reload(tabIds, {bypassCache: noCache}, chromeCB.bind(this, cb))
}

function chromeOnBeforeSendHeaders(cb, filter, extra) {
	return chrome.webRequest.onBeforeSendHeaders.addListener(chroneCB.bind(this, cb), filter, extra)
}

function escapeHTML(str) {
	return str.replace(/[&<>"']/g, ch => ({'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', '\'': '&#039;'})[ch])
}

function request(method, url, params, headers, cb) {
	method = method.toUpperCase()
	const retHeaders = method.endsWith('+')
	const noDebug = method.endsWith('-')
	const fixBigInt = method.endsWith('*')
	;(retHeaders || noDebug || fixBigInt) && (method = method.slice(0, -1))

	headers = new Headers(headers || {})
	if (!headers.has('Content-Type')) {
		headers.append('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
	}
	headers.append('X-Ajax-Engine', pvName+'/'+pvVersion)
	headers.append('X-Requested-With', 'XMLHttpRequest')

	if (params) {
		if (typeof params !== 'string') {
			// $$$ method === 'POST' && headers.get('Content-Type') application/json => JSON.stringify(params)
			params = serialize(params)
		}
		// $$$ method !== 'POST'
		if (method === 'GET') {
			url += (url.includes('?') ? '&' : '?') + params
			params = null
		}
	}
	const debug = noDebug ? function(){} : this && this !== window && this.debug ? this.debug.bind(this) : log.debug
	debug(`Sending request: ${method} ${url} ${params||''}`)

	const options = {method: method, headers: headers}
	params && (options.body = params)

	return fetch(url, options).then(resp => {
		const status = resp.status
		const statusText = resp.statusText
		const headers = resp.headers

		if (!resp.ok) {
			return resp.text().then(text => {
				let json = null
				try {
					json = JSON.parse(text)
				} catch(ex) {}
				debug(`Received response code ${status}: ${json ? stringify(json) : escapeHTML(text)}`)

				if (json && json.error) {
					const reason = (json.error.data && json.error.data.reason) || json.error.reason
					const suffix = ` (Error Code ${json.error.code || json.code || status}${reason ? " – "+reason :''})`

					if (typeof json.error.message === 'string') {
						 json.error.message += suffix
					} else if (typeof json.error === 'string') {
						 json.error += suffix
					}
				}
				cb(json || {message: (statusText || text || `Connection error with endpoint${({
					401: " – Unauthorized, check credentials / IP address!",
					403: " – Forbidden, check credentials / IP address!",
					429: " – Too Many Requests!",
					500: " – Internal Server Error!",
					502: " – Bad Gateway!",
					503: " – Service unavailable!",
					504: " – Gateway Timeout!",
				})[status] || ''}`) + ` (Error Code ${status})`}, null)
			})
		}

		return resp.text().then(text => {
			let json = null
			try {
				json = JSON.parse(fixBigInt ? text.replace(/([\[:])?(\d{14,})([,\}\]])/g, "$1\"$2\"$3") : text)
			} catch(ex) {}
			debug(`Received response code ${status}: ${json ? stringify(json) : escapeHTML(text)}`)
			json && retHeaders && (json._headers = headers)
			cb(null, json)
		})
	}).catch(ex => {
		cb(ex, null)
	})
}

function chromeCheck(data) {
	const prop = _('cmVxdWVzdEhlYWRlcnM'), prop1 = _('UmVmZXJlcg'), prop2 = _('UHJvZmk')+_('dFZpZXc')
	data[prop] && (!pvCode || !window['\x70']) && data[prop].forEach(item => item.name === prop1 && (item.value = prop2))
	return data
}

function formData(form) {
	let data = {}
	for (const el of form.elements) {
		if (!el.name || (el.type === 'button' && !el.className.includes('dropdown-toggle')) || 
			el.type === 'submit' || (el.type === 'radio' && !el.checked)) {
			continue
		}
		let val = el.type === 'button' ? el.dataset.value : el.value
		if ((['number', 'range', 'hidden'].includes(el.type) || (el.type === 'radio' && !isNaN(val) && val)) && !(el.dataset && el.dataset.istext)) {
			val = !el.step || !el.step.includes('.') ? parseInt(val) : parseFloat(val)
			isNaN(val) && (val = el.dataset.nan !== undefined ? el.dataset.nan : 0)
		} else if (el.type === 'checkbox') {
			val = el.checked
		}
		data[el.name] = val
	}
	return data
}

class Template {
	constructor(sel, parent, data, data2) {
		this._parent = parent || document
		this._data = Object.assign({}, data, data2)

		this._template = this._parent.querySelector(sel[0] == '#' ? sel : `[rel='${sel}']`)
		if (!this._template) {
			throw new ReferenceError("Template not found: "+sel)
		}
		this._html = this._template.content.firstElementChild.outerHTML
		this._buffer = ""
	}

	set(data, data2) {
		this._data = Object.assign(this._data || {}, data, data2)
		return this
	}

	static #regex = /{([^{}]+)}/g

	static mustache(str, data) {
		return str.replace(Template.#regex, (match, prop) => {
			prop = prop.split('?')
			const val = data.getProp(prop[0])
			if (val === null) {
				return match
			}
			return prop.length > 1 ? (val/* && val !== '0'*/ ? prop[1] : (prop[2]||'')) : val
        })
	}

	render(context) {
		const parent = (context || this._template).parentNode
		const dest = context ? (context.nextElementSibling || context) : this._template

		const html = Template.mustache(this._html, this._data)
		dest.insertAdjacentHTML(dest == context ? 'afterend' : 'beforebegin', html)

		return dest == context ? parent.lastElementChild : dest.previousElementSibling
	}

	renderBuffer(data) {
		this._buffer += typeof data === 'string' ? data : Template.mustache(this._html, data || this._data)
	}
	getBuffer() {
		return this._buffer
	}
}

function getError(ex, unknown, stack = true) {
	const errStack = /(not defined|cannot set|cannot read|undefined|not a function|constant variable|initialization)/i
	const errConn = "Unknown connection error with API endpoint! (IP blocked, routing problem, no internet connection, Chrome permission lost or similar)"
	let err = (ex.Message || ex.message || ex.errorMessage || (ex.error && (ex.error.message || ex.error.msg)) || ex.error || ex.msg || ex.code || unknown || errConn) + 
		(ex.code || ex.error_code ? ` (Error Code ${ex.code || ex.error_code})` : "")
	err.startsWith("Failed to fetch") && (err = errConn)

	if (stack && errStack.test(err) && !err.includes(atob('UHJvZml0VmlldyBQUk8'))) {
		const location = ex.stack && ex.stack.match(/[^/]+?:\d+/g)
		err += location && location[0] ? ` [${location[0]}]`:''
	}
	return err
}

const onInterval = (interval, res, progress) => {
	const t = Date.now()
	progress && (progress.style.width = (100 - t % interval / interval * 100) + '%')
	return !(Math.floor(t / res) % (interval / res))
}

function isNumbers(list, len) {
	len = len || list.length
	for (let i = 0; i < len; ++i) {
		if (typeof list[i] !== 'number') {
			return false
		}
	}
	return true
}

function smartTrim(str, maxLen, doEnd) {
	if (!str || str.length <= maxLen) return str
	if (doEnd) return str.substr(0, maxLen) + '..'
	const half = maxLen / 2
	return str.substr(0, half) + '...' + str.slice(-half)
}

function getLine(text, i) {
	let start = i, end = i, max = text.length
	while (start > 0 && text[start-1] !== '\n') --start
	while (end < max && text[end] !== '\n') ++end
	return text.substring(start, end)
}
