$(function(){

	var autopilot = 0;

	$('#myModal').on('hidden.bs.modal', function (e) {
		$('.modal-error').hide();
		$('.modal-error label').html('');
		$('#input-hashtag').val('');
	})


	var base_url = $(".base_url").val();


	/* Filter statuses */
	$(document).on('click', '.filter-options', function() {
		var action   = $(this).attr('data-action');
		var columnId = $(this).attr('data-id');

		if(action == '2') {
			$('#column_'+columnId+' .blocked-status').hide();
			$('#column_'+columnId+' .active-status').fadeIn();
			$('#column_'+columnId+' .icon-menu').removeClass('panel-dropdown-red-icon');
		} else if(action == '4') {
			$('#column_'+columnId+' .active-status').hide();
			$('#column_'+columnId+' .blocked-status').fadeIn();
			$('#column_'+columnId+' .icon-menu').addClass('panel-dropdown-red-icon');
		}
	});


	/* Approve all statuses from column */
	$(document).on('click', '.approve-all', function(){
		var columnId = $(this).attr('data-id');

		if($('#column_'+columnId+' .column-status').length > 0) {

			$('#column_'+columnId+' .column-status').each(function(){
				$(this).prependTo('.playlist .panel-body');
				$(this).slideDown('fast');

				var plCounter = parseInt($('.playlist .badge').html());
				$('.playlist .badge').html(plCounter+1);
			});

			$('#column_'+columnId+' .badge').html(0);
			$('#column_'+columnId+' .gathering-statuses').fadeIn();

			$.ajax({
				url  : base_url+"/admin/approveStatusesByColumn",
				type : 'post',
				dataType:'json',
				data : {
					columnId : columnId
				},
				success : function(ret) {
					if(ret.error == 0) {

					} else {
						alert('error');
					}
				}
			});
		}
	});

	/* Remove column */
	$(document).on('click', '.remove-column', function() {
		var columnId = $(this).attr('data-id');

			$.ajax({
				url  : base_url+"/admin/removeColumn",
				type : 'post',
				dataType:'json',
				data : {
					columnId : columnId
				},
				success : function(ret) {
					if(ret.error == 0) {
						$('#column_'+columnId).remove();
					} else {
						alert('error');
					}
				}
			});
	});

	/*Populate playlist*/

	getStatusesForPlaylist();
	function getStatusesForPlaylist() {
		$.ajax({
			url  : base_url+"/admin/getStatusesForPlaylist",
			type : 'get',
			dataType:'json',
			success : function(ret) {
				if(ret.error == 0) {
					$('.playlist .column-stauses-holder').html(ret.data.statuses);
					$('.playlist .badge').html(ret.data.statusCount);
				} else {
					//alert(ret.msg);
				}
			}
		});
	}

	/* Sorting */
	$('.playlist .column-stauses-holder').sortable({
		start: function( event, ui ){ui.item.addClass('sorting-status')},
		stop : function( event, ui ){
			ui.item.removeClass('sorting-status');
			var data = $(this).sortable('serialize');

			$.ajax({
				url  : base_url+"/admin/sortStatuses",
				type : 'post',
				dataType:'json',
				data : data,
				success : function(ret) {
					if(ret.error == 0) {

					} else {
						alert(ret.msg);
					}
				}
			});

		},
		axis: 'y'
	});

	/* Add column */
	$(document).on('click', '.add-column', function(){
		$.ajax({
			url  : base_url+"/admin/addColumn",
			type : 'post',
			dataType:'json',
			data : {
				social_network : $('#select-social-network').val(),
				hashtag		: $('#input-hashtag').val()
			},
			success : function(ret) {
				if(ret.error == 0) {
					$('.modal-error').hide();
					$('#myModal').modal('hide');

					getColumns(1,null,function(){
						$.ajax({
							url  : base_url+"/admin/collectStatusesByColumns",
							type : 'get',
							dataType:'json',
							data : {
							  columnId : ret.id
							},
							success : function() {
								getStatuses(ret.id,null);//---------------------------------------------------------------
							},
							error: function() {
								alert("Error: collectAndGetStatuses");
							}
						});
					});

				} else {
					$('.modal-error').fadeIn('fast');
					$('.modal-error label').html(ret.msg);
				}
			}
		});
	});

	/*Autopilot*/
	$(document).on('click', '.autopilot', function(){
		var btn = $('.autopilot');
		if(autopilot == 0) {
			btn.removeClass('btn-default');
			btn.addClass('btn-success');
			autopilot = 1;
		} else {
			btn.removeClass('btn-success');
			btn.addClass('btn-default');
			autopilot = 0;
		}
	});

	/* Set state for statuses*/
	$(document).on('click', '.state_btn', function(){
		var statusElem = $(this).parent().parent();
		var state	  = $(this).attr('data-state');
		var statusId   = $(this).attr('data-status-id');
		var columnId   = $(this).attr('data-column');
		var curState   = statusElem.attr('data-cur-state');

		//$(this).parent().parent().remove();

		if(state == 2) {

			statusElem.hide();
			statusElem.prependTo('.playlist .panel-body');
			statusElem.slideDown('fast');
			statusElem.addClass('approved-status');

			var plCounter = parseInt($('.playlist .badge').html());
			$('.playlist .badge').html(plCounter+1);

			if(curState != 4) {
				var thisColumnCounter = parseInt($('#column_'+columnId+' .badge').html());
				$('#column_'+columnId+' .badge').html(thisColumnCounter-1);
			}

		} else if(state == 4) {

			var thisColumnCounter = parseInt($('#column_'+columnId+' .badge').html());
			$('#column_'+columnId+' .badge').html(thisColumnCounter-1);

			statusElem.removeClass('active-status');
			statusElem.addClass('blocked-status');
			statusElem.hide();

		} else {

			if($(this).parent().parent().parent().parent().attr('id') == 'column_playlist') {
				var plCounter = parseInt($('.playlist .badge').html());
				$('.playlist .badge').html(plCounter-1);
			} else {
				var thisColumnCounter = parseInt($('#column_'+columnId+' .badge').html());
				$('#column_'+columnId+' .badge').html(thisColumnCounter-1);
			}

			statusElem.fadeOut(500);
			statusElem.remove();
		}

		setState(statusId, state);

	});

	function alwaysTrue(){return true}

	getColumns(null,null,alwaysTrue);

	function getColumns(lastCreated, id,callback) {
		$.ajax({
			url  : base_url+"/admin/getColumns",
			type : 'get',
			dataType:'json',
			data : {
				id : id,
				lastCreated : lastCreated
			},
			success : function(ret) {
				if(ret.error == 0) {
					$('.scrolls').append(ret.data);
					/*$('.column-stauses-holder').mCustomScrollbar('destroy').mCustomScrollbar({
						autoHideScrollbar	 : false,
						theme				 : 'dark-thick',
						updateOnContentResize : true,
						axis				  : "x",
						mouseWheel			: false
					});*/
					callback();
				} else {
					alert(ret.msg);
				}
			}
		});
	}


	//getStatuses(0);
	init();
	function getStatuses(idColumn,onlyNew) {
		$.ajax({
			url  : base_url+"/admin/getStatuses",
			type : 'get',
			dataType:'json',
			data : {
				idColumn   : idColumn,
				onlyNew	: onlyNew
			},
			success : function(ret) {
				if(ret.error == 0) {

					if(autopilot == 0) {

						for(i=0; ret.data.length > i; i++) {

							// Update counter
							var counter	= parseInt(ret.data[i].statusCount);
							var oldCounter = parseInt($('#column_'+ret.data[i].column_id+' .badge').html());

							if(counter > 0) {
								var finalCount = oldCounter+counter;
								$('#column_'+ret.data[i].column_id+' .badge').html(finalCount);
							}

							if(ret.data[i].statuses !=0) {
								var column_status_holder = $('#column_'+ret.data[i].column_id+' .column-stauses-holder');

							   // column_status_holder.mCustomScrollbar('destroy');

								$('#column_'+ret.data[i].column_id+' .column-stauses-holder .column-status').removeClass('new-status');
								$('#column_'+ret.data[i].column_id+' .gathering-statuses').hide();

								$('<div class="status-group">'+ret.data[i].statuses+'</div>').prependTo(column_status_holder);
								$('#column_'+ret.data[i].column_id+' .column-stauses-holder .status-group').slideDown();


	/*							column_status_holder.mCustomScrollbar({
									autoHideScrollbar	 : true,
									theme				 : 'minimal-dark',
									updateOnContentResize : true
								});*/
							}
						}
					} else {
						getStatusesForPlaylist();
					}

					if(timeout) {
						window.learTimeout(timeout);
					}
					var timeout = window.setTimeout(update, 110000);

				} else {
					alert(ret.msg);
				}
			}
		});
	}


	function update() {
		$.ajax({
			url  : base_url+"/admin/collectStatusesByColumns",
			type : 'get',
			dataType:'json',
			data : {
				autopilot : autopilot
			},
			success : function() {
				getStatuses(null,1);
			},
			error: function() {
				alert("Error: collectAndGetStatuses");
			}
		});
	}

	function init(){
		$.ajax({
			url  : base_url+"/admin/collectStatusesByColumns",
			type : 'get',
			dataType:'json',
			success : function() {
				getStatuses();
			},
			error: function() {
				alert("Error: collectAndGetStatuses");
			}
		});
	}


	function activeColumns() {
		var activeColumns = [];
		$('.column').each(function() {
			activeColumns.push($(this).attr('data-id'));
			console.log($(this).attr('data-id'));
		});
		return activeColumns;
	}


	function setState(statusId, state) {
		$.ajax({
			url  : base_url+"/admin/setState",
			type : 'post',
			dataType:'json',
			data : {
				state	: state,
				statusId : statusId
			},
			success : function() {
				//getStatuses(0,1);
			},
			error: function() {
				alert("Error: setState");
			}
		});
	}
});
