@if(!defined('LOAD_MIRADA'))

	@push('js')
		<script src="/assets/apps/Conferencing/js/mirada/socket.min.js"></script>
		<script>
			var mirada_port = @if (App\Repositories\Languages::getCurrent() == 'fr') "43002" @else "43001" @endif;
			var mirada_server = @if (App\Repositories\Languages::getCurrent() == 'fr') "https://fundglobam_fr.interactive-live.com" @else "https://fundglobam_en.interactive-live.com" @endif;
			var mirada_stream = @if (App\Repositories\Languages::getCurrent() == 'fr') "https://video.ibm.com/embed/23961510?autoplay=true&showtitle=false" @else "https://www.ustream.tv/embed/20618782?html5ui=1&autoplay=true&volume=90&showtitle=false" @endif;
			var mirada_fullname = "{{ $user['first_name'] ?? '' }} {{ $user['last_name'] ?? '' }} - {{ $user['company'] ?? '' }}";
			var mirada_nom = "{{ $user['last_name'] ?? '' }}";
			var mirada_prenom = "{{ $user['first_name'] ?? '' }}";
			var mirada_company = "{{ $user['company'] ?? '' }}";
			var mirada_email = "{{ $user['email'] ?? '' }}";
			var mirada_socket = null;
			var mirada_user_id = null;

			var mirada = {
				init: function() {
					console.log("Connection to Mirada");
					mirada_socket = io.connect(mirada_server + ':' + mirada_port, { 'reconnectionAttempts' : 10 });
					mirada_socket.emit('checkchat');
					this.handle_events();
					this.handle_submit();
				},
				handle_submit: function() {
					$('#mirada_send').click(function() {
						console.log('message sending');
						mirada.send_message();
					})
				},
				handle_events: function() {
					
					mirada_socket.on('logged',function() {
						console.log("logged");
					});

					mirada_socket.on('admin_etat_chat',function(etat) {
						console.log("admin_etat_chat");
						console.log(etat);
						mirada_socket.emit('inscrit', {
							insmail	: mirada_fullname
						});
					});

					mirada_socket.on('redirect_countdown',function(admin) {
						console.log("redirect_countdown");
						console.log(admin);
					});

					mirada_socket.on('redirect_chat',function(user) {
						console.log("redirect_chat");
						console.log(user);
						mirada_user_id = user.id;
						mirada.connect(); 
					});

					mirada_socket.on('false_inscrit',function(msg) {
						console.log("false_inscrit");
						console.log(msg);
					});

					mirada_socket.on('sw_manauto',function(mod) {
						console.log("sw_manauto");
						console.log(mod);
					});

					mirada_socket.on('newmsg', function(message) {
						console.log("newmsg");
						console.log(message);
						mirada.display_message_sent(message);
					});
					
					mirada_socket.on('recu_msg',function(message) {
						console.log("recu_msg");
						console.log(message);
						mirada.display_message_received(message);
					});

					/*
					mirada_socket.on('chat_admin', function(message) {
						console.log("chat_admin");
						console.log(message);
						mirada.display_admin_message(message);
					});
					*/

					mirada_socket.on('bloqueuser',function(user) {
						console.log("bloqueuser");
						console.log(user);
						$('#alert'+user.iduser).html("You have been temporarily blocked!");
						$( "#alert"+user.iduser ).dialog( "open" );
						$('#zone_chat'+user.iduser).fadeOut();
					});

					mirada_socket.on('debloqueuser', function(user) {
						console.log("debloqueuser");
						console.log(user);
						$('#alert'+user.iduser).html("Your account has been unlocked.");
						$( "#alert"+user.iduser ).dialog( "open" );
						$('#zone_chat'+user.iduser).fadeIn();
					});

					mirada_socket.on('banuser', function(user) {
						console.log("banuser");
						console.log(user);
						$('#site' + user.iduser).html("<div class='alert_ban'>You do not have the permissions to reach this page ! </div>");
					});

					mirada_socket.on('msgsupprime', function(id){
						console.log("msgsupprime");
						console.log(id);
						$('.message'+id).remove();
					});

					mirada_socket.on('chetat-live', function(etat) {
						console.log("chetat-live");
						console.log(etat);
						if (etat == 'avant') {
							mirada.display_event_before();
						} else if (etat == 'pendant') {
							mirada.display_event_live();
						} else if (etat == 'apres') {
							mirada.display_event_after();
						}
					});
			
					/*
					mirada_socket.on('etat-live', function(etat) {
						console.log("etat-live");
						console.log(etat);

						var status;

						if (($('#zone_live').html()).indexOf("avant") != -1) {
							status = 'avant';
						} else if ($('#zone_live').html().indexOf("iframe") != -1) {
							status = 'pendant';
						} else if($('#zone_live').html().indexOf("apres") !=- 1) {
							status = 'apres';
						}

						var idvisiteur = $('#zone_live').attr('id').substring(9);
						mirada_socket.emit('etat_live_visiteur', {
							id : idvisiteur,
							etat : status
						});
					});
					*/
				},
				connect: function() {
					console.log("connect");
					mirada_socket.emit('connecte', {
						insnom : mirada_nom,
						insprenom : mirada_prenom,
						insmail	 : mirada_fullname,
						userid : mirada_user_id
					});
					mirada_socket.emit('loglive');
					mirada_socket.emit('logmod');
					mirada_socket.emit('logmsg');
				},
				send_message: function() {
					
					$('#mirada_message').val(function(i, v){
						return v.replace(/\n/g,"<br>");
					});

					if ($('#mirada_message').val().length < 10) {
						alert("Posez votre question avec au moins 10 caracteres ! ");
					} else {					

						// data = $('#mirada_chat').serialize();
						data = {
							message: $('#mirada_message').val(),
							dest: $('#mirada_dest_msg').val(),
							mod: $('#man_auto').val()
						};

						console.log('send_message', data);

						mirada_socket.emit('newmsg', data);

						$('#mirada_message').val('');
						$('#mirada_message').focus();

					}
				},
				display_message_sent(message) {
					$('mirada_messages').prepend('<div class="sep"></div>');

					if (message.presenter == 0) {
						var html = '<li id="message' + message.id + '">' +
							'<div class="message-data">' +
							'<span class="message-data-name"> ' + message.user.insprenom + ' ' + message.user.insnom + '</span>' +
							'<span class="message-data-time"> ' + @if (App\Repositories\Languages::getCurrent() =='fr') message.time @else message.time @endif + '</span>' +
							'</div>' +
							'<div class="message my-message">' +
							message.message +
							'</div>' +
							'</li>';
					} else {
						var html = '<li id="message' + message.id + '">' +
							'<div class="message-data">' +
							'<span class="message-data-name"> ' + message.mail + '</span>' +
							'<span class="message-data-time"> ' + @if (App\Repositories\Languages::getCurrent() =='fr') message.time @else message.time @endif + '</span>' +
							'</div>' +
							'<div class="message my-message">' +
							message.message +
							'</div>' +
							'</li>';
					}

					$('#mirada_messages .chat-history').prepend(html);

					// $('#messages').animate({scrollTop : $('#messages').prop('scrollHeight')},-250);
				},
				display_message_received(message) {

					if (message.presenter == 0)
					{ 
						var html = '<li id="message' + message.id + '">' +
							'<div class="message-data">' +
							'<span class="message-data-name"> ' + message.user.insprenom + ' ' + message.user.insnom + '</span>' +
							'<span class="message-data-time"> ' + @if (App\Repositories\Languages::getCurrent() =='fr') message.time @else message.time @endif + '</span>' +
							'</div>' +
							'<div class="message my-message">' +
							message.message +
							'</div>' +
							'</li>';
					} else {
						var html = '<li id="message' + message.id + '">' +
							'<div class="message-data">' +
							'<span class="message-data-name"> ' + message.mail + '</span>' +
							'<span class="message-data-time"> ' + @if (App\Repositories\Languages::getCurrent() =='fr') message.time @else message.time @endif + '</span>' +
							'</div>' +
							'<div class="message my-message">' +
							message.message +
							'</div>' +
							'</li>';
					}

/*
					var html = '<div class="message'+message.id+'">\
							<span class="date">'+message.time+'</span>\
							<div class="info">\
								<p class="iden"><strong>'+message.iden+' : </strong></p>\
								<hr class="Hrinfo"/>\
								<div class="idenMessage"> '+message.message+'</div>\
							</div>\
					</div><div class="sep"></div>';
*/
					$('#mirada_messages .chat-history').prepend(html);
				},
				display_admin_message(message) {
					$('#messages'+message.dest).prepend('<div class="sep"></div>');

					if (message.iden == 'Moderateur') {
						$('#messages'+message.dest).prepend('<div class="message chatrouge"><ul id="thelist_chat">\
										<li>\
											<span class="date">'+message.time+'</span>\
											<div class="info">\
												<p class="iden"><strong>'+message.iden+' : </strong></p>\
												<hr class="Hrinfo"/>\
												<div class="idenMessage"> '+message.message+'</div>\
											</div>\
										</li>\
									</ul></div>');
					} else if(message.iden == 'SAV') {
						$('#messages'+message.dest).prepend('<div class="message chatjaune"><ul id="thelist_chat">\
							<li>\
								<span class="date">'+message.time+'</span>\
								<div class="info">\
									<p class="iden"><strong>'+message.iden+' : </strong></p>\
									<hr class="Hrinfo"/>\
									<div class="idenMessage"> '+message.message+'</div>\
								</div>\
							</li>\
						</ul></div>');
					}
				},
				display_event_before() {
					$('#mirada-before').hide().removeClass('d-none').fadeIn(5000);
					$('#mirada-live').addClass('d-none');
					$('#mirada-after').addClass('d-none');
					$('#zone_chat').hide().removeClass('d-none').fadeIn(5000);
				},
				display_event_live() {
					$('#mirada-after').addClass('d-none');
					$('#mirada-before').fadeOut(5000);
					$('#mirada-live').html('<iframe src="' + mirada_stream + '" style="border: 0;" webkitallowfullscreen allowfullscreen frameborder="no" width="720" height="405"></iframe>');
					$('#mirada-live').hide().removeClass('d-none').fadeIn(5000);
					$('#zone_chat').hide().removeClass('d-none').fadeIn(5000);

				},
				display_event_after() {
					$('#mirada-before').addClass('d-none');
					$('#mirada-live').fadeOut(5000);
					$('#zone_chat').fadeOut(5000);
					$('#mirada-after').hide().removeClass('d-none').fadeIn(5000);
				}
			}

			$(document).ready(function () {
				Countdown.init("{{ $event['begin'] }}");
				mirada.init();
			}); 

		</script>
	@endpush
	@php(define('LOAD_MIRADA', true))
@endif