const StdLib = require('@doctormckay/stdlib');
const Helpers = require('./helpers.js');
const SteamUser = require('../index.js');

/**
 * Get authorized devices.
 * @param {boolean} [inclTokens=false] - If true, include canceled devices
 * @param {function} [callback]
 * @returns {Promise}
 */
SteamUser.prototype.getOwnAuthorizedDevices = function(includeCanceled, callback) {
	if (typeof includeCanceled !== 'boolean'){
		includeCanceled = false;
	}

	return StdLib.Promises.callbackPromise(null, callback, true, (resolve, reject) => {
		this._sendUnified('DeviceAuth.GetOwnAuthorizedDevices#1', {
			'steamid': this.steamID.getSteamID64(),
			includeCanceled
		}, (body, hdr) => {
			let err = Helpers.eresultError(hdr.proto);
			if (err) {
				return reject(err);
			}
			resolve(body.devices);
		});
	});
};

/**
 * Authorize local device.
 * @param {string} deviceName
 */
SteamUser.prototype.authorizeLocalDevice = function(deviceName){
	if(!deviceName){
		throw new Error('The \'deviceName\' argument is required.');
	}

	this._send(SteamUser.EMsg.ClientAuthorizeLocalDeviceRequest, {
		device_description: deviceName,
		owner_account_id: this.steamID.accountid
	});
}

/**
 * Deauthorize local device.
 * @param {object} device
 */
SteamUser.prototype.deauthorizeDevice = function(device){
	if(!device){
		throw new Error('The \'device\' argument is required.');
	}

	this._send(SteamUser.EMsg.ClientDeauthorizeDeviceRequest, {
		deauthorization_account_id: this.steamID.accountid,
		deauthorization_device_token: device.authed_device_token
	});
}

/**
 * Use local device authorizations to allow the family share.
 * @param {SteamID|string} ownerSteamID
 * @param {object[]} devices
 * @param {object[]} authorization_accounts
 * @param {function} [callback]
 * @returns {Promise}
 */
SteamUser.prototype.useLocalDeviceAuthorizations = function(ownerSteamID, devices, authorization_accounts, callback){
	return StdLib.Promises.callbackPromise(null, callback, true, (resolve, reject) => {
		if(!ownerSteamID){
			return reject(new Error('The \'ownerSteamID\' argument is required.'));
		}

		if (!Array.isArray(devices)) {
			return reject(new Error('The \'devices\' argument must be an array'));
		}

		if (!Array.isArray(authorization_accounts)) {
			return reject(new Error('The \'authorization_accounts\' argument must be an array'));
		}

		let device_tokens = [];
		devices.forEach((device) => {
			device_tokens.push({
				'owner_account_id': Helpers.steamID(ownerSteamID).accountid,
				'token_id': device.authed_device_token
			});
		})

		let authorization_account_id = [];
		authorization_accounts.forEach((auth_acc_id) => {
			authorization_account_id.push(Helpers.steamID(auth_acc_id).accountid)
		});

		this._send(SteamUser.EMsg.ClientUseLocalDeviceAuthorizations, {
			authorization_account_id,
			device_tokens
		});

		resolve();
	});
}

// Handlers

SteamUser.prototype._handlerManager.add(SteamUser.EMsg.ClientAuthorizeLocalDeviceResponse, function(body) {
	this.emit("authorizedLocalDevice", body);
});

SteamUser.prototype._handlerManager.add(SteamUser.EMsg.ClientDeauthorizeDevice, function(body) {
	this.emit("deauthorizedDevice", body);
});

@@ -184,14 +184,20 @@
 incl
-Tokens
+udeCancelled
 =fal
@@ -225,16 +225,17 @@
 e cancel
+l
 ed devic
@@ -363,16 +363,17 @@
 deCancel
+l
 ed, call
@@ -405,16 +405,17 @@
 deCancel
+l
 ed !== '
@@ -440,16 +440,17 @@
 deCancel
+l
 ed = fal
@@ -647,16 +647,16 @@
 ID64(),%0A
-
 %09%09%09inclu
@@ -663,16 +663,17 @@
 deCancel
+l
 ed%0A%09%09%7D, 

change "inclTokens" to "includeCancelled" and "canceled" to "cancelled"