/* globals fileUpload popover chatMessages AudioRecorder */

import mime from 'mime-type/with-db';
import {VRecDialog} from 'meteor/rocketchat:ui-vrecord';

RocketChat.messageBox.actions.add('Create_new', 'Video_message', {
	id: 'video-message',
	icon: 'video',
	condition: () => (navigator.getUserMedia || navigator.webkitGetUserMedia) && RocketChat.settings.get('FileUpload_Enabled') && RocketChat.settings.get('Message_VideoRecorderEnabled') && (!RocketChat.settings.get('FileUpload_MediaTypeWhiteList') || RocketChat.settings.get('FileUpload_MediaTypeWhiteList').match(/video\/webm|video\/\*/i)),
	action({messageBox}) {
		return VRecDialog.opened ? VRecDialog.close() : VRecDialog.open(messageBox);
	}
});

RocketChat.messageBox.actions.add('Create_new', 'Audio_message', {
	id: 'audio-message',
	icon: 'mic',
	condition: () => (navigator.getUserMedia || navigator.webkitGetUserMedia) && RocketChat.settings.get('FileUpload_Enabled') && RocketChat.settings.get('Message_AudioRecorderEnabled') && (!RocketChat.settings.get('FileUpload_MediaTypeWhiteList') || RocketChat.settings.get('FileUpload_MediaTypeWhiteList').match(/audio\/wav|audio\/\*/i)),
	action({event, element}) {
		event.preventDefault();
		const icon = element;
		if (chatMessages[RocketChat.openedRoom].recording) {
			return AudioRecorder.stop(function(blob) {
				icon.style.color = '';
				icon.classList.remove('pulse');
				chatMessages[RocketChat.openedRoom].recording = false;
				return fileUpload([
					{
						file: blob,
						type: 'audio',
						name: `${ TAPi18n.__('Audio record') }.wav`
					}
				]);
			});
		}
		chatMessages[RocketChat.openedRoom].recording = true;
		return AudioRecorder.start(function() {
			icon.classList.add('pulse');
			icon.style.color = 'red';
		});
	}
});


RocketChat.messageBox.actions.add('Add_files_from', 'Computer', {
	id: 'file-upload',
	icon: 'computer',
	condition: () => RocketChat.settings.get('FileUpload_Enabled'),
	action() {
		setTimeout(() => {
			popover.close();
			const input = document.createElement('input');
			input.style.display = 'none';
			input.type = 'file';
			input.setAttribute('multiple', 'multiple');
			document.body.appendChild(input);

			input.click();

			input.addEventListener('change', function(e) {
				const filesToUpload = [...e.target.files].map(file => {
					Object.defineProperty(file, 'type', {
						value: mime.lookup(file.name)
					});
					return {
						file,
						name: file.name
					};
				});
				return fileUpload(filesToUpload);
			}, {once: true});

			input.remove();
		}, 100);
	}
});

RocketChat.messageBox.actions.add('Share', 'My_location', {
	id: 'share-location',
	icon: 'map-pin',
	condition: () => RocketChat.Geolocation.get() !== false,
	action({rid}) {
		const position = RocketChat.Geolocation.get();
		const latitude = position.coords.latitude;
		const longitude = position.coords.longitude;
		const text = `<div class="location-preview"><img style="height: 250px; width: 250px;" src="https://maps.googleapis.com/maps/api/staticmap?zoom=14&size=250x250&markers=color:gray%7Clabel:%7C${ latitude },${ longitude }&key=${ RocketChat.settings.get('MapView_GMapsAPIKey') }" /></div>`;
		return swal({
			title: t('Share_Location_Title'),
			text,
			showCancelButton: true,
			closeOnConfirm: true,
			closeOnCancel: true,
			html: true
		}, function(isConfirm) {
			if (isConfirm !== true) {
				return;
			}
			return Meteor.call('sendMessage', {
				_id: Random.id(),
				rid,
				msg: '',
				location: {
					type: 'Point',
					coordinates: [longitude, latitude]
				}
			});
		});
	}
});

@@ -1949,54 +1949,45 @@
 ion(
-) %7B%0A%09%09setTimeout(() =%3E %7B%0A%09%09%09popover.close
+%7Bevent%7D) %7B%0A%09%09event.preventDefault
 ();%0A
-%09
 %09%09co
@@ -2031,17 +2031,16 @@
 nput');%0A
-%09
 %09%09input.
@@ -2061,25 +2061,24 @@
 = 'none';%0A%09%09
-%09
 input.type =
@@ -2086,17 +2086,16 @@
 'file';%0A
-%09
 %09%09input.
@@ -2132,17 +2132,16 @@
 iple');%0A
-%09
 %09%09docume
@@ -2167,25 +2167,24 @@
 (input);%0A%0A%09%09
-%09
 input.click(
@@ -2183,25 +2183,24 @@
 t.click();%0A%0A
-%09
 %09%09input.addE
@@ -2239,17 +2239,16 @@
 e) %7B%0A%09%09%09
-%09
 const fi
@@ -2295,17 +2295,16 @@
 le =%3E %7B%0A
-%09
 %09%09%09%09Obje
@@ -2342,17 +2342,16 @@
  %7B%0A%09%09%09%09%09
-%09
 value: m
@@ -2372,17 +2372,16 @@
 e.name)%0A
-%09
 %09%09%09%09%7D);%0A
@@ -2384,17 +2384,16 @@
 %7D);%0A%09%09%09%09
-%09
 return %7B
@@ -2402,16 +2402,14 @@
 %09%09%09%09
-%09
 file,%0A
-%09
 %09%09%09%09
@@ -2433,13 +2433,11 @@
 %09%09%09%09
-%09
 %7D;%0A
-%09
 %09%09%09%7D
@@ -2438,25 +2438,24 @@
 ;%0A%09%09%09%7D);%0A%09%09%09
-%09
 return fileU
@@ -2478,17 +2478,16 @@
 oad);%0A%09%09
-%09
 %7D, %7Bonce
@@ -2497,17 +2497,16 @@
 rue%7D);%0A%0A
-%09
 %09%09input.
@@ -2519,19 +2519,8 @@
 ();%0A
-%09%09%7D, 100);%0A
 %09%7D%0A%7D

fix on android