/**************************************************
* Funkcje związane z geolokalizacją GPS
**************************************************/
WMB.Comment = {
	/**
	* Funkcja dodająca nowy komentarz do zgłoszenia
	*
	* @method onAddSubmit
	*/
	onAddSubmit: function(marker_id) {
		if (WMB.User.isLoggedIn()) {
			if ($('#add-comment-form')[0].checkValidity()) {
				var post_data = new FormData();
				post_data.append('user_id', parseInt(getCookie('user_id')));
				post_data.append('marker_id', parseInt(marker_id));
				post_data.append('comment', $('#comment').val());
				$.ajax({
					url: REST_API_URL + 'comment',
					beforeSend: function(request) {
						request.setRequestHeader('Authorization', 'Token ' + getCookie('token'));
					},
					type: 'POST',
					cache: false,
					processData: false,
					contentType: false,
					data: post_data,
					success: function(s) {
						$('#add-comment-modal').modal('hide');
						bootbox.alert('Pomyślnie dodaną komentarz.', function() {
							location.reload();
						});
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) {
						$('#add-comment-modal').modal('hide');
						var response = JSON.parse(XMLHttpRequest.responseText);
						bootbox.alert(response.message);
					}
				});
			}
		} else {
			bootbox.alert('Aby móc dodawać komentarze musisz być zalogowany.');
		}
	},
	onEditClick: function(id) {
		// Pobranie danych o komentarzu
		$.ajax({
			url: REST_API_URL + 'comment/id/' + id,
			type: 'GET',
			success: function(data) {
				// Wstawienie danych do formularza
				$('#comment_id').val(id);
				$('#comment').val(data.comment);
				$('#edit-comment-modal').modal('show');
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				var response = JSON.parse(XMLHttpRequest.responseText);
				bootbox.alert(response.message, function() {
					window.location.href = WEBSITE_URL;
				});
			}
		});
	},
	onEditSubmit: function() {
		if ($('#edit-comment-form')[0].checkValidity()) {
			var comment_data = new FormData(),
				comment_id = parseInt($('#comment_id').val()),
				comment = $('#comment').val();
			comment_data.append('comment', comment);
			$.ajax({
				url: REST_API_URL + 'comment/id/' + comment_id + '/edit',
				beforeSend: function (request) {
					request.setRequestHeader('Authorization', 'Token ' + getCookie('token'));
				},
				type: 'POST',
				cache: false,
				processData: false,
				contentType: false,
				data: comment_data,
				success: function(s) {
					$('#edit-comment-modal').modal('hide');
					bootbox.alert('Pomyślnie zedytowano komentarz.', function() {
						window.location.href = WEBSITE_URL + 'comments';
					});
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					var response = JSON.parse(XMLHttpRequest.responseText);
					bootbox.alert(response.message);
				}
			});
		}
	},
	onDeleteClick: function(id) {
		$.ajax({
			url: REST_API_URL + 'comment/id/' + id,
			beforeSend: function (request) {
				request.setRequestHeader('Authorization', 'Token ' + getCookie('token'));
			},
			type: 'DELETE',
			cache: false,
			processData: false,
			contentType: false,
			success: function(data) {
				$('#edit-comment-modal').modal('hide');
				bootbox.alert('Pomyślnie usunięto komentarz.', function() {
					location.reload();
				});
			},
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				var response = JSON.parse(XMLHttpRequest.responseText);
				bootbox.alert(response.message, function() {
					window.location.href = WEBSITE_URL;
				});
			}
		});
	},
	/**
	* Funkcja służąca do zmiany statusu komentarza
	*
	* @method changeStatus
	* @param {Integer} id Identyfikator komentarza
	* @param {Integer} status_id Identyfikator statusu
	*/
	changeStatus: function(id, status_id) {
		if (WMB.User.isLoggedIn()) {
			$.ajax({
				url: REST_API_URL + 'comment/id/' + id + '/status/' + status_id,
				beforeSend: function (request) {
					request.setRequestHeader('Authorization', 'Token ' + getCookie('token'));
				},
				type: 'PUT',
				cache: false,
				processData: false,
				contentType: false,
				success: function(data) {
					if (status_id == 0) {
						bootbox.alert('Pomyślnie zgłoszono nadużycie w komentarzu.', function() {
							location.reload();
						});
					} else {
						bootbox.alert('Pomyślnie zmieniono status komentarza na ' + comment_statuses[status_id] + '.', function() {
							location.reload();
						});
					}
				},
				error: function(XMLHttpRequest, textStatus, errorThrown) {
					var response = JSON.parse(XMLHttpRequest.responseText);
					bootbox.alert(response.message, function() {
						window.location.href = WEBSITE_URL;
					});
				}
			});
		} else {
			bootbox.alert('Aby zgłosić nadużycie musisz być zalogowany/a.');
		}
	},
	/**
	* Funkcja pobierająca wszystkie komentarze dla danego zgłoszenia
	*
	* @method getAll
	* @param {Integer} marker_id Identyfikator zgłoszenia
	*/
	getAll: function(marker_id) {
		$.ajax({
			url: REST_API_URL + 'comment/marker/' + marker_id,
			type: 'GET',
			cache: false,
			dataType: 'json',
			success: function(data) {
				$.each(data.comments, function(i) {
					if (data.comments[i].status_id != 0 && data.comments[i].user_id == parseInt(getCookie('user_id'))) {
						$('#marker-comment-list').append(
						'<tr>' +
							'<td>' + data.comments[i].login + '</td>' +
							'<td>' + data.comments[i].date + '</td>' +
							'<td>' + data.comments[i].comment + '</td>' +
							'<td>brak</td>' +
						'</tr>');
					} else if (data.comments[i].status_id != 0) {
						$('#marker-comment-list').append(
						'<tr>' +
							'<td>' + data.comments[i].login + '</td>' +
							'<td>' + data.comments[i].date + '</td>' +
							'<td>' + data.comments[i].comment + '</td>' +
							'<td><a href="#" onclick="WMB.Comment.changeStatus(' + data.comments[i].comment_id + ', 0)">Zgłoś nadużycie</a></td>' +
						'</tr>');
						
					}
				});
			}
		});
	}
}