var app = {
	loading: function(is, position) {
		$('#loading').css('display', is ? 'block': 'none');
		is && position && ($('#loading').addClass(position));
	},
	info: function(message, title, position) {
		toastr.info(message, title, {positionClass: 'toast-' + (position || 'top-center'), timeOut: 1000});
	},
	error: function(message, title, position) {
		toastr.error(message, title, {positionClass: 'toast-' + (position || 'top-center')});
	},
	//============CRUD about apps===============
	editApp : function(event, appId) {
		appId = event ? $(event.target).data("id") : appId;
		if (appId) {
			$.getJSON('api/app/' + appId, function(data){
				app.showAppModal(data.content);
			});
		} else {
			// new app
			app.showAppModal({
			});
		}
	},
	showAppModal: function(content) {
		$('#appModal').showModal(function(){
			var scope = $(this);
			content.type = content.userControl ? 'control' : 'default';
			scope.renderData(content);
			scope.find('.btn-submit').click(scope.find('form'), app.saveApp);
			if(content.uid) {
				scope.find('.btn-del').click(content, app.deleteApp);
				scope.find('.btn-del').show();
			} else {
				scope.find('.btn-del').hide();
			}
			// 生成密钥对
			scope.find('#keypair .btn-create').click(function(){
				app.generateKey(function(data){
					scope.find('#keypair textarea').val(data.content);
					scope.find('#keypair .btn-download').parent().show();
				});
			});
			// 类型切换
			var toggleType = function(type) {
				if('default' == type) {
					scope.find('#appModal_type_default').show();
					scope.find('#appModal_type_control').hide();
				} else {
					scope.find('#appModal_type_default').hide();
					scope.find('#appModal_type_control').show();
				}
			};
			toggleType(content.type);
			scope.find(':radio[name=type]').change(function(){
				toggleType($(this).val());
			});
		});
	},
	saveApp : function(event) {
		var form = event.data, content = form.serializeObject();
		content.copyApps = (content.copyApps ? content.copyApps.replace(/\n+|\t+|\s+|;/g, ',') : null);
		app.loading(true, 'right-center');
		$.post('api/app', content, function(data){
			app.loading(false);
			if (data && data.errMsg) {
				app.error(data.errMsg, '保存失败', 'top-right');
			} else {
				window.location.reload();
			}
		}, 'json');
	},
	deleteApp: function(event){
		var appId = event.data.uid;
		if(appId) {
			app.loading(true);
			$.del('api/app/' + appId, function(data) {
				app.loading(false);
				if (data && data.errMsg) {
					app.error(data.errMsg, '删除失败', 'top-right');
				} else {
					window.location.reload();
				}
			});
		}
	},
	//============    utils     ===============
	generateKey: function(callback) {
		app.loading(true, 'right-center');
		$.getJSON('api/secret/generate', function(data){
			app.loading(false);
			if(data.errMsg) {
				app.error(data.errMsg, '出现异常', 'top-right');
			} else {
				callback.call(null, data);
			}
		});
	},
	//============document ready===============
	init : function() {
		$('.x-chunk-list-wrap .x-editor-trigger').click(app.editApp);
		app.loading(false);
	}
};
$(document).ready(function() {
	app.init();
});