window.bilbord = (function($) {
	'use strict';

	var app = {};

	app.settings = {
		'maxTabs'		: 2,
		'baseUrl'		: $('body').data('base-url'),
		'maxStatuses'	: 3
	};

	app.theme = {
		'status'	: function (status, col) {
			col = col == undefined ? 'col-sm-12' : col;
			var newElm = '<input name="id" type="hidden" value="'+status.id+'">\
				<div class="statusFiles clearfix">\
					<img src="'+ (status.files != undefined ? status.files[1] : '') +'">\
				</div>\
				<div class="statusContent clearfix">\
					<div class="avatar col-sm-2">\
						<img src="'+status.profile_img+'">\
					</div>\
					<div class="username col-sm-10">&#64;'+status.username+'</div>' +
				(status.media != undefined ? '<img class="media" src="'+status.media+'">' : '') +
					'<div class="text">'+status.text+'</div>\
				</div>\
				<div class="actions">\
					<span class="cloneStatus fa fa-clone" aria-hidden="true"></span>\
					<span class="approveStatus glyphicon glyphicon-ok" aria-hidden="true"></span>\
					<span class="unapproveStatus glyphicon glyphicon-remove" aria-hidden="true"></span>\
				</div>\
				<div class="sortActions">\
					<span class="moveUpStatus glyphicon glyphicon-chevron-up" aria-hidden="true"></span>\
					<span class="moveDownStatus glyphicon glyphicon-chevron-down" aria-hidden="true"></span>\
				</div>';

			if (col) {
				newElm = '<div class="status grid-item '+col+'">'+newElm+'</div>';
			}

			return newElm;
		},

		'statusContainer'	: function (id, active, settings){
			if (!settings.hasOwnProperty('pinned')) {
				settings.pinned = false;
			}

			return '<div id="tabContent'+id+'" class="tabContentWrapp clearfix'+(active==true ? ' active' : '')+' nano">'+
			'<div class="tabSettings glyphicon glyphicon-cog" aria-hidden="true">'+
				'<form class="tabSettingsForm clearfix ">'+
					'<input name="columnId" type="hidden" value="'+id+'">'+
					'<label class="alignRight">'+
						'<input class="pinTabInp" type="checkbox" value="1" ' + (settings.pinned ? 'checked="checked"' : '') + '>'+
						'<span class="glyphicon glyphicon-pushpin" aria-hidden="true"></span>'+
					'</label>'+
					'<div class="alignRight">'+
						'<div class="removeTab glyphicon glyphicon-remove" aria-hidden="true"></div>'+
					'</div>'+
				'</form>'+
			'</div>'+
			'<div class="tabContent grid nano-content">'+
				'<div class="grid-sizer col-sm-4 col-lg-3"></div>'+
			'</div></div>';
		},

		'statusTab'	: function  ( tab, active ) {
			return '<li role="presentation"' + (active == true ? ' class="active"' : '') +'>\
				<a id="tab'+tab.id+'" class="fa fa-'+tab.social_network+'" href="#tabContent'+tab.id+'" role="tab" aria-controls="tabContent'+tab.id+'" aria-expanded="true"\
					data-toggle="tab" \
					data-network="'+tab.social_network+'"\
					data-hasNew="0">'+tab.hashtag+'</a>\
			</li>';
		}
	},

	app.formaters = {
		'statuses' : {
			'files'	: function(data) {
				if (data == undefined) {
					return false;
				}
				for ( var i=0; i<data.length; i++ ) {
					if( data[i].files != null || data[i].files != '' ) {
						data[i].files = JSON.parse(data[i].files);
					}
				}
				return data;
			}
		}
	},

	app.connection = {
		'get'	: {
			'statuses'	: {
				'all'		: function () {
					return $.ajax({
						method		: 'GET',
						url			: app.settings.baseUrl+'/admin/getStatuses',
						contentType	: 'application/json',
					}).pipe(function (response) {
						for (var i=0; i<response.length; i++) {
							response[i].tab.settings = JSON.parse(response[i].tab.settings);
						};
						return response;
					});
				},

				'approved'	: function () {
					return $.ajax({
						'method'		: 'GET',
						'url'			: app.settings.baseUrl+'/admin/getApprovedStatuses/'+app.settings.maxStatuses,
						'contentType'	: 'application/json'
					})
					.pipe(function(response) {
						// console.log(response)
						if (response.error) {
							alert(response.error);
							return false;
						}
						return app.formaters.statuses.files(response.statuses);
					},
					function () {
						alert ('Something went wrong!!!');
						return false;
					});
				},

				'archived'		: function () {
					return $.ajax({
						'method'	: 'GET',
						url			: app.settings.baseUrl+'/admin/getArchived',
						contentType	: 'application/json',
					}).pipe(function(response){
						return app.formaters.statuses.files(response.data);
						/*for( var i=0; i<response.data.length; i++) {
							response.data[i].files = JSON.parse(response.data[i].files);
						}
						return response;*/
					});
				},

				'byColumns'		: function () {
					return $.ajax({
						method		: 'GET',
						url			: app.settings.baseUrl+'/admin/collectStatusesByColumns',
						contentType	: 'application/json',
					});
				},

				'crone'		: function () {
					return $.ajax({
						method		: 'GET',
						url			: app.settings.baseUrl+'/api/crone/1',
						contentType	: 'application/json',
					});
				}
			},

			'tabs'	: {
				'all'		: function () {
					return $.ajax({
						'method'		: 'GET',
						'url'			: app.settings.baseUrl+'/admin/getColumns',
						'contentType'	: 'application/json'
					})
						.pipe(function(response){
							for (var t=0; t<response.tabs.length;t++) {
								response.tabs[t].settings = JSON.parse(response.tabs[t].settings);
							}
							return response.tabs;
						});
				},
			},

			'imageBase64'	: function (url) {
				url = encodeURIComponent(url);
				return $.ajax({
					'method'		: 'GET',
					'url'			: app.settings.baseUrl+'/admin/imageToBase64?url='+url,
					'contentType'	: 'application/json'
				});
			},
		},
		'post'	: {
			'statuses' : {
				'unapprove' : function (statusId, newState) {
					return $.ajax({
						'url'		: bilbord.settings.baseUrl+'/admin/setState/'+statusId,
						'type'		: 'POST',
						'dataType'	:'json',
						'data'		: {
							'state'	: newState,
							'remove': true
						}
					});
				},
				'approved'	: function (data) {
					return $.ajax({
						'method'		: "POST",
						'cache'			: false,
						'url'			: app.settings.baseUrl+'/admin/approveStatus',
						'contentType'	: "json",
						'async'			: false,
						'headers'		: {
							'Content-Type' : 'application/json'
						},
						'data'			: JSON.stringify(data),
						'dataType'		: 'json'
					});
				},
				'clone'			: function (id) {
					return $.ajax({
						'method'		: "POST",
						'cache'			: false,
						'url'			: app.settings.baseUrl+'/admin/cloneStatus/'+id,
						'contentType'	: "json",
					}).pipe(function (response) {
						return response.data;
					});
				},
				'changeState'	: function (statusId, newState) {
					return $.ajax({
						'url'		: bilbord.settings.baseUrl+'/admin/setState/'+statusId,
						'type'		: 'POST',
						'dataType'	:'json',
						'data'		: {
							'state'	: newState
						}
					});
				},
				'resort'		: function (data) {
					return $.ajax({
						'url'		: app.settings.baseUrl+'/admin/sortStatuses',
						'type'		: 'POST',
						'data'		: data,
						'dataType'	:'json'
					}).pipe(function(response){
						return app.formaters.statuses.files(response.statuses);
					});
				},
			},
			'tab'	: {
				'remove'			: function (id, data) {
					return $.ajax({
						'method'		: "POST",
						'cache'			: false,
						'url'			: app.settings.baseUrl+'/admin/tab/'+id+'/state',
						'contentType'	: "json",
						'async'			: false,
						'headers'		: {
							'Content-Type' : 'application/json'
						},
						'data'			: JSON.stringify(data),
						'dataType'		: 'json'
					});
				},
				'create'			: function (data) {
					return $.ajax({
						'url'		: bilbord.settings.baseUrl + "/admin/tab/create",
						'type'		: 'POST',
						'data'		: data,
						'dataType'	:'json'
					});
				},
				'settings'			: function (id, data) {
					return $.ajax({
						'url'		: bilbord.settings.baseUrl + '/admin/tab/'+id+'/settings',
						'method'		: "POST",
						'cache'			: false,
						'async'			: false,
						'headers'		: {
							'Content-Type' : 'application/json'
						},
						'contentType'	: "json",
						'data'			: JSON.stringify(data),
						'dataType'		: 'json'
					});
				}
			}
		}
	};

	$(document).ready(function(){
		app.settings.baseUrl = $('body').attr('data-base-url');
	})

	return app;
}(jQuery));