(function($) {
	'use strict';

	bilbord.app = {
		'status'	: {
			'approve'	: function () {
				if ($('#approvedStatusesList .status').length >= bilbord.settings.maxStatuses) {
					alert('Popuniliste sva slobodna polja za odobrene statuse!')
					return false;
				}

				var status	= $(this).closest('.status'),
				id			= status.find('input[name="id"]').val();
				// console.log(status, id)

				bilbord.connection.post.statuses.changeState(id, 1)
					.done(function (response) {
						if (response.error != undefined) {
							return false;
						}
						// status.removeClass('col-sm-[*]')
						status.get(0).className = status.get(0).className.replace(/\bcol-.*?\b/g, '');
						status.addClass('col-sm-12');
						$('#approvedStatusesList .nano-content').prepend(status);
						$('#approvedStatusesForm').trigger('submit');
					});
			},

			'unapprove'	: function (e) {
				console.log('unapprove');
				var status	= $(this).closest('.status'),
				id			= status.find('input[name="id"]').val(),
				newState	= status.closest('#archivedStatusesList').length > 0 ? -1 : 3;

				bilbord.connection.post.statuses.changeState(id, newState)
					.done(function (response) {
						if (response.error != undefined) {
							return false;
						}
						if (newState == -1) {
							status.remove();
						}
						else {
							status.get(0).className = status.get(0).className.replace(/\bcol-.*?\b/g, '');
							status.addClass('col-sm-6 col-lg-4');
							$('#archivedStatusesList .nano-content').prepend(status);
							$('#approvedStatusesForm').trigger('submit');
						}
					});
			},

			'order'		: function (e) {
				var status = $(this).closest('.status');

				if( e.data.direction > 0 ) {
					$(status).next().after( status );
				} else {
					$(status).prev().before( status );
				}

				$('#approvedStatusesForm').trigger('submit');
			},

			'clone'		: function (e) {
				e.preventDefault();
				console.log('cloneStatus')

				var status	= $(this).closest('.status'),
				id			= status.find('input[name="id"]').val(),
				alfa		= $(this);
				// console.log(status, id)

				bilbord.connection.post.statuses.clone(id)
					.done(function (response) {
						console.log(response);
						/*if (response.error != undefined) {
							return false;
						}
						alfa.after(status);
						$('#approvedStatusesForm').trigger('submit');*/
					});
			},

			'init'		: function (status) {
				$(status)
					.on('click', '.cloneStatus', bilbord.app.status.clone)
					.on('click', '.approveStatus', bilbord.app.status.approve)
					.on('click', '.unapproveStatus', bilbord.app.status.unapprove)
					.on('click', '.moveUpStatus', {'direction':-1}, bilbord.app.status.order)
					.on('click', '.moveDownStatus', {'direction':1}, bilbord.app.status.order);
			}
		},

		'lists'		: {
			'initApproved'	: function (statuses) {
				var container = $('#approvedStatusesList .nano-content'),
				statusesHtml = '';
				for ( var s in statuses ) {
					statusesHtml += bilbord.theme.status(statuses[s]);//col-sm-4 col-lg-3
				};
				container
					.append(statusesHtml)
					.find('.status')
					.each(function (i, elm) {
						bilbord.app.status.init(elm);
					});
			},

			'initArchive'	: function (archive) {
				var container = $('#archivedStatuses .nano-content'),
				statusesHtml = '';
				if(archive.length < 1) {
					return false;
				}
				for (var s=0; s<archive.length; s++) {
					statusesHtml += bilbord.theme.status(archive[s], 'col-sm-6 col-lg-4');
				};

				container
					.append(statusesHtml)
					.find('.status')
					.each(function (i, elm) {
						bilbord.app.status.init(elm);
					});
			},

			'resort'	: function (e) {
				e.preventDefault();

				var form = $(this),
				formContent = form.find('.nano-content'),
				statuses = form
					.serializeArray()
					.map(function(val, index){
						if (val.hasOwnProperty('value')) {
							return val.value;
						}
					});

				bilbord.connection.post.statuses
					.resort({
						'statuses'	: statuses,
						'max'		: bilbord.settings.maxStatuses
					})
					.done(function (response) {
						formContent.find('.status').remove();
						// console.log('resort: ', response)
						for (var i=0; i<response.length; i++) {
							var elm = bilbord.theme.status(response[i], null);
							formContent.append(elm);
						}

						formContent.find('.status').each(function(i, elm) {
							bilbord.app.status.init(elm);
						});
					});

				return false;
			}
		},

		'init'		: function () {
			$('#approvedStatusesForm').on('submit', bilbord.app.lists.resort);

			bilbord.connection.get.statuses.approved()
				.done(bilbord.app.lists.initApproved);

			bilbord.connection.get.statuses.archived()
				.done(bilbord.app.lists.initArchive);
		},
	};

	$(document).ready(bilbord.app.init);
})(jQuery);