import Ember from 'ember';
var computed = Ember.computed;

export default Ember.Controller.extend({
	purecloud: Ember.inject.service(),
	storageService: Ember.inject.service(),
	queueService: Ember.inject.service(),
	org: null,
	name: '',
	phone: '',
	queue: '',
	time: new Date().toTimeString().split(' ')[0],
	date: new Date().toISOString().split('T')[0],
	callbackCreated: false,
	customAttributes: [],

	init() {
		this._super(...arguments);

		let futureTime = new Date();
		futureTime.setHours(futureTime.getHours() + 1);
		this.set('time', futureTime.toTimeString().split(' ')[0]);

		let storage = this.get('storageService');
		let savedData = storage.localStorageGet('callbackParams');

		if (savedData) {
			this.set('name', savedData.name);
			this.set('queue', savedData.queue);
			this.set('phone', savedData.phone);
			this.set('customAttributes', savedData.customAttributes);
		}
	},

	queues: computed('queueService.queues', function() {
		return this.get('queueService').get('queues');
	}),
	actions: {
		addCustomAttribute() {
			let customAttributes = this.get('customAttributes');
			customAttributes.pushObject({
				name: '',
				value: ''
			});
			this.set('customAttributes', customAttributes);
		},
		deleteCustomAttribute(attribute) {
			this.get('customAttributes').removeObject(attribute);
		},
		createCallback() {
			try {
				this.set('callbackError', null);
				let time = new Date(this.get('date') + ' ' + this.get('time')).toISOString();
				var data = {
					// name of the individual we are calling back
					callbackUserName: this.get('name'),
					// time at which we will callback the individual
					callbackScheduledTime: time,
					// number(s) to try to call to reach individual
					callbackNumbers: [this.get('phone')],
					// queueId of the queue onto which this callback will be placed
					queueId: this.get('queue'),
					// Should use default script if `null`
					scriptId: null,
					data: {}
				};

				const customAttributes = this.get('customAttributes');
				for (let attribute of customAttributes) {
					if (attribute.name != '') {
						data.data[attribute.name] = attribute.value;
					}
				}

				let self = this;
				let conversationsApi = this.get('purecloud').conversationsApi();
				conversationsApi
					.postConversationsCallbacks(data)
					.then(function() {
						self.set('callbackCreated', true);

						setTimeout(function() {
							self.set('callbackCreated', false);
						}, 5000);
					})
					.catch((error) => {
						console.log(error);
						self.set('callbackError', error.body ? error.body.message : error);
					});

				let savedData = {
					name: this.get('name'),
					queue: this.get('queue'),
					phone: this.get('phone'),
					customAttributes: this.get('customAttributes')
				};

				let storage = this.get('storageService');
				storage.localStorageSet('callbackParams', savedData);
			} catch (ex) {
				this.set('callbackError', ex);
			}
		},
		selectQueue(queue) {
			console.log('select queue ' + queue);
			this.set('queue', queue);
		}
	}
});

@@ -2003,24 +2003,74 @@
 tributes');%0A
+%09%09%09%09if(typeof customAttributes !== %22undefined%22)%7B%0A%09
 %09%09%09%09for (let
@@ -2108,16 +2108,17 @@
  %7B%0A%09%09%09%09%09
+%09
 if (attr
@@ -2134,15 +2134,17 @@
 e !=
+=
  '') %7B%0A
+%09
 %09%09%09%09
@@ -2190,16 +2190,24 @@
 .value;%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09
@@ -2211,16 +2211,20 @@
 %7D%0A%09%09%09%09%7D%0A
+%09%09%09%09
 %0A%09%09%09%09let

check if custom attributes is defined before trying to access it