"use strict"

const {remote} = require('electron')
const EventEmitter = require('events')
const i18n = require('../node_modules/i18n-nodejs')

const ToolUI = require('./toolui')
const ToolSettings = require('../lib/settings')
const TwitchTv = require('./twitchtv')
const TwitchHelix = require('./twitchhelix')
const TTVLogin = require('../lib/auth')
const Chat = require('../lib/chat')

const Channel = require('../var/channel')
const Follows = require('../var/follows')
const Subscriptions = require('../var/subscriptions')

const Addons = require('../lib/addons')
//const EAU = remote.require('electron-asar-updater')
const {app, autoUpdater} = remote.require('electron')
const spawn = require('child_process').spawn;

/**
 * This module initializes pretty much everything and other modules have access to other modules over here.
 * 
 * @class TTVTool
 * @fires TTVTool#load
 * @fires TTVTool#exit
 * @extends {EventEmitter}
 */
class TTVTool extends EventEmitter {

	constructor() {
		super()
		const self = this

		this.setMaxListeners(50)

		this._settings = new ToolSettings(this)
		this._i18n = new i18n(this._settings.language, './../../language.json')
		this._twitchapi = new TwitchTv({
			clientid: '11vhuxdssb9pc3s2uqpa7s3s0252hyk',
			redirecturi: 'http://localhost:8086/',
			scope: [
				'user_read',
				'channel_check_subscription',
				'channel_editor',
				'channel_read',
				'channel_stream',
				'channel_subscriptions',
				'chat_login'
			]
		})
		this._twitchhelix = new TwitchHelix({
			clientid: '11vhuxdssb9pc3s2uqpa7s3s0252hyk',
			redirecturi: 'http://localhost:8086/',
			scope: []
		})
		this._auth = new TTVLogin(this)
		this._chat = new Chat(this)

		this._ui = new ToolUI(this)
		this._ui.addPage(this._settings)
		
		this._channel = new Channel(this)
		this._follows = new Follows(this)
		// All subscriptions are coming via chat now
		//this._subscriptions = new Subscriptions(this)

		this._addons = new Addons(this)

		this.on('load', () => {
			if(self._settings.lightDesignMode) {
				document.querySelector('body').classList.add('invert')
			}

			/*EAU.check((e) => {
				if(e != null && e.length > 0) {
					if(e === 'no_update_available') return
					self.ui.showErrorMessage(new Error(e))
				} else {
					let errormsg_download = self.ui.showErrorMessage(new Error(self.i18n.__('There is a new update. Downloading now...')))
					EAU.download((er) => {
						if(e != null && e.length > 0) {
							self.ui.showErrorMessage(new Error(e))
							return
						}
						try { errormsg_download.parentElement.removeChild(errormsg_download) } catch(e) {}

						let errormsg_restart = self.ui.showErrorMessage(new Error(self.i18n.__('Update ready. Program will restart automatically!')))
						errormsg_restart.onclick = () => {}
						setTimeout(() => {
							let exPa = process.execPath
							if(exPa.startsWith('\\')) exPa = exPa.substr(1)
							if(exPa.indexOf(' ') > 0) exPa = '"' + exPa + '"'
							spawn('cmd', ['/Q', '/C', 'copy resources\\update.asar resources\\app.asar /Y & del resources\\update.asar & start ' + exPa], {'detached': true})
							app.quit()
						}, 5000);
					})
				}
			})*/
		})

		window.onload = (e) => {
			//Riot.compile(function() {
				/**
				 * Fires after the document was loaded. Basically window.onload. Use this instead
				 * of window.onload to not overwrite the onload function and breaking the entire
				 * application. Thank you. :)
				 * 
				 * @event TTVTool#load
				 */
				self.emit('load')
			//})
		}
		window.onbeforeunload = (e) => {
			/**
			 * Fires before the application is exited. Once again just window.onbeforeunload.
			 * Don't overwrite window.onbeforeunload please.
			 * 
			 * @event TTVTool#exit
			 */
			self.emit('exit')
		}

		
	}


	/**
	 * Gives you the UI modules
	 * 
	 * @type {ToolUI}
	 * @readonly
	 */
	get ui() {
		return this._ui
	}

	/**
	 * Gives you the {@link https://www.npmjs.com/package/i18n-nodejs|i18n-nodejs} module.
	 * 
	 * @type {i18n-nodejs}
	 * @see {@link https://www.npmjs.com/package/i18n-nodejs}
	 * @readonly
	 */
	get i18n() {
		return this._i18n
	}

	/**
	 * Gives you the settings module.
	 * 
	 * @type {ToolSettings}
	 * @readonly
	 */
	get settings() {
		return this._settings
	}

	/**
	 * Gives you the Twitch-API module.
	 * 
	 * @type {TwitchTv}
	 * @readonly
	 */
	get twitchapi() {
		return this._twitchapi
	}

	/**
	 * Gives you the new Twitch API module
	 * 
	 * @type {TwitchHelix}
	 * @readonly
	 */
	get twitchhelix() {
		return this._twitchhelix
	}
	
	/**
	 * Gives you the auth module.
	 * 
	 * @type {TTVLogin}
	 * @readonly
	 */
	get auth() {
		return this._auth
	}

	/**
	 * Gives you the chat module.
	 * 
	 * @type {Chat}
	 * @readonly
	 */
	get chat() {
		return this._chat
	}

	/**
	 * Gives you the cockpit module.
	 * 
	 * @type {Cockpit}
	 * @readonly
	 */
	get cockpit() {
		return this._ui.findPage('Cockpit')
	}

	/**
	 * Gives you the overlay module.
	 * 
	 * @type {TTVLogin}
	 * @readonly
	 */
	get overlays() {
		return this._ui.findPage('Overlays')
	}

	/**
	 * Gives you the channel data module.
	 * 
	 * @type {Channel}
	 * @readonly
	 */
	get channel() {
		return this._channel
	}

	/**
	 * Gives you the follow data module.
	 * 
	 * @type {Follows}
	 * @readonly
	 */
	get follows() {
		return this._follows
	}

	/**
	 * Gives you a subscription module.
	 * 
	 * @type {undefined}
	 * @readonly
	 * @deprecated Subscription are now shared with everyone via chat.
	 */
	get subscriptions() {
		return this._subscriptions
	}

	/**
	 * Gives you the addons module.
	 * 
	 * @type {Addons}
	 * @readonly
	 */
	get addons() {
		return this._addons
	}

}

const tool = new TTVTool()
module.exports = tool