appice.controller('chatbotSettingController',function ($scope,appIceService) {
	
	$scope.chatbot = {};
	$scope.chatbotData;
	$scope.api_key = semusiCommon.getActiveUser.api_key;
	$scope.URL = semusiCommon.APIUrl;

	$scope.createChatBot = function(){
		var platforms = [];
		if($scope.chatbot.facebook){
			platforms.push('facebook');
		}
		if($scope.chatbot.slack){
			platforms.push('slack');
		}
		if($scope.chatbot.skype){
			platforms.push('skype');
		}

		var data = {
			name: $scope.chatbot.name,
			platforms: platforms
		};

		var promise = appIceService.createChatbot(data);
		promise.then(function(data){
			if(data){
				$scope.app_id = data._id;
				$scope.app_key = data.key;
				$scope.platforms = data.platforms;
			}
		});
	}

	angular.element(document).ready(function () {
    setTimeout(function(){
		$("#setupwizard").bwizard({validating: function (e, ui) {
	//   Validating the username to contain only alphabet, number and space
			var re = /^[0-9A-Za-z\s\-]+$/;
			if (!re.test($scope.chatbot.name)) {
				$('.chatbotnameError').css({'color':'red','display':'block'});
				$('.bwizard-buttons').find('.previous').click();
				return false;
			}
			if(($.trim($scope.chatbot.name) == '' || $scope.chatbot.name == undefined)){
				$('.chatbotnameError').css({'color':'red','display':'block'});
		        $('.bwizard-buttons').find('.previous').click();
		        return false;
			}
			
			$('.chatbotnameError').css({'color':'red','display':'none'});

			if($scope.chatbot.facebook == undefined && $scope.chatbot.skype == undefined && $scope.chatbot.slack == undefined){
				$('.platformError').css({'color':'red','display':'block'});
		        $('.bwizard-buttons').find('.previous').click();
		        return false;
			}

			$('.platformError').css({'color':'red','display':'none'});

		    if($.trim($scope.chatbot.name) != '' && ($scope.chatbot.facebook || $scope.chatbot.skype || $scope.chatbot.slack ) ){
		    	return true;	
			}
			
		}, activeIndexChanged: function (e, ui) {	
				if(ui.index == 1 && $scope.chatbot.name == undefined){
					//$scope.chatbot.appCreated = true;
					return false;
				}
				if(ui.index == 1 && $scope.chatbot.name){
					$scope.createChatBot();
				}

				if(ui.index == 2 && !$scope.chatbot.email){

					if($scope.chatbot.email){
						$.ajax({
							type:"GET",
							url:semusiCommon.API_PARTS.apps.w + '/SendDevData',
							data:{
							args:JSON.stringify({
							    app_id: localStorage.getItem("hdnApp_id"),
								app_key: localStorage.getItem("hdnApp_key"),
							    devemailid:devemailid,
							    a_appname:$('#appnameGoogle').val(),
							    i_appname:$('#appnameStore').val(),
							    sendSteps:sendSteps
						    }),    
							//api_key: "<%= member.api_key %>"
							uniqueNumber : semusiCommon.randomNumber(),
							api_key: semusiCommon.getActiveUser.api_key
							},
							success:function (data) {
								emailSent = true;
						}});

					}
				}
			} 
		});

    },10)
   });
});