(function($) {
	'use strict';

	var config = window.wppsStorageMonitor || {};
	var strings = config.strings || {};

	function updateWidget($widget, data) {
		$widget.find('.wpps-storage-value').text(data.total_formatted);

		$widget.find('.wpps-storage-uploads .wpps-storage-item-size').text(data.uploads_formatted);
		$widget.find('.wpps-storage-uploads .wpps-storage-progress-bar').css('width', data.uploads_percentage + '%');
		$widget.find('.wpps-storage-uploads .wpps-storage-item-percentage').text(data.uploads_percentage + '%');

		$widget.find('.wpps-storage-plugins .wpps-storage-item-size').text(data.plugins_formatted);
		$widget.find('.wpps-storage-plugins .wpps-storage-progress-bar').css('width', data.plugins_percentage + '%');
		$widget.find('.wpps-storage-plugins .wpps-storage-item-percentage').text(data.plugins_percentage + '%');

		$widget.find('.wpps-storage-themes .wpps-storage-item-size').text(data.themes_formatted);
		$widget.find('.wpps-storage-themes .wpps-storage-progress-bar').css('width', data.themes_percentage + '%');
		$widget.find('.wpps-storage-themes .wpps-storage-item-percentage').text(data.themes_percentage + '%');

		$widget.find('.wpps-storage-database .wpps-storage-item-size').text(data.database_formatted);
		$widget.find('.wpps-storage-database .wpps-storage-progress-bar').css('width', data.database_percentage + '%');
		$widget.find('.wpps-storage-database .wpps-storage-item-percentage').text(data.database_percentage + '%');

		var cacheInfo = strings.cacheInfo || 'Cached for 1 hour. Last updated:';
		var justNow = strings.justNow || 'just now';
		var ago = strings.ago || 'ago';
		$widget.find('.wpps-storage-cache-info').text(cacheInfo + ' ' + justNow + ' ' + ago);
	}

	function showFailure(message) {
		window.alert(message || strings.failed || 'Failed to refresh storage data.');
	}

	$(document).ready(function() {
		$(document).on('click', '.wpps-storage-refresh', function(e) {
			e.preventDefault();

			var $button = $(this);
			var $widget = $button.closest('.wpps-storage-monitor');
			var nonce = $button.data('nonce');

			$button.prop('disabled', true).addClass('loading');

			$.ajax({
				url: typeof ajaxurl !== 'undefined' ? ajaxurl : '/wp-admin/admin-ajax.php',
				type: 'POST',
				data: {
					action: 'wpps_refresh_storage_data',
					nonce: nonce
				},
				success: function(response) {
					if (response.success && response.data && !response.data.error) {
						if ($widget.find('.wpps-storage-error').length) {
							window.location.reload();
							return;
						}

						updateWidget($widget, response.data);
						return;
					}

					var message = (response.data && response.data.message) ? response.data.message : strings.failed;
					showFailure(message);
				},
				error: function(xhr) {
					var message = strings.error;
					if (xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
						message = xhr.responseJSON.data.message;
					}
					showFailure(message);
				},
				complete: function() {
					$button.prop('disabled', false).removeClass('loading');
				}
			});
		});
	});
})(jQuery);
