(function () {
    'use strict';

    angular
        .module('app')
        .controller('veController', veController)
        .filter('startFrom', function() { 
			return function(input, start) {
				start = +start; 
				return input.slice(start);
			}
		}); 

    veController.$inject = ['UserService', 'VeService', '$rootScope', 'FlashService', '$route'];
    
    function veController(UserService, VeService, $rootScope, FlashService, $route) { 
        var vm = this;
		vm.db = ($route.current.db ? $route.current.db : 've');
        vm.user = null;
        vm.add_video = add_video;
        vm.add_cat = add_cat;
        vm.getCatName = getCatName;
        vm.numberOfPages = numberOfPages;
		vm.tree = {name: "Projects", nodes: [], categories: [], domains: []};
        vm.currentPage = 0;
        vm.pageSize = '10';
        vm.screen = '';
        vm.video = {};    
        vm.selectVideoIndex = null;    
        vm.category = {};        
        vm.domain = {};        
        vm.orderByField = 'client_name';
		vm.reverseSort = '0';
        
        vm.del = del;
        vm.resetValues = resetValues;
        
		function resetValues(){
		
			vm.screen = 'listing';
			if(vm.db == 've'){
			vm.video = {id:null,title:'',youtube_video_id:'',cat_id:'',quote:'',client_name:'',company_name:'',designation:'',city:'',state:'',country:'',domain:'',ratings:null,domain_name:'',status:'',website:'',creation_date:'', remarks:''};		
			} else {
			vm.video = {id:null,title:'',cat_id:'',editing_status:'',particular:'',keyword:'',youtube_status:'',youtube_link:'',fb_status:'',fb_link:'',linkedIn_status:'',linkedin_link:'',destination:'',remarks:''};	
			}
			vm.selectVideoIndex = null;
			vm.category = {id:null,name:'',description:''};		
			vm.domain = {id:null,name:'',description:''};		
			
		}

        initController();

        function initController() { 
            resetValues();
            loadCurrentUser();
        }
        
        function add_video(type) {
			
					
            vm.dataLoading = true;
            var data = vm.video;
            data.type = type;
            data.item = 'video';
            data.uploaded_by = vm.user.id;
            VeService.Create(data)
                .then(function (response) {  
                    if (response.success) { 
						
						response.data.website_status = response.data.website == 1 ? 'Yes' : 'No';
						response.data.cat_name = vm.getCatName(response.data.cat_id);
						
						if(vm.selectVideoIndex != null){
							
							//vm.tree.nodes[vm.selectVideoIndex] = response.data;
							
						} else { 
							vm.tree.nodes.push(response.data);
						}
                        
                        resetValues();
                        FlashService.Success(response.message);
                        vm.dataLoading = false;
                    } else {
                        FlashService.Error(response.message);
                        vm.dataLoading = false;
                    }
                });
        }    
        
        function getCatName(cat_id){
			
			for (var i=0; i < vm.tree.categories.length; i++) { 
				if (vm.tree.categories[i].id == cat_id) { 
					return vm.tree.categories[i].name;
				}
			}			
						
		}   

        function add_cat(type) {
			
					
            vm.dataLoading = true;
            var data = vm.category;
            data.type = type;
            data.item = 'category';
            data.uploaded_by = vm.user.id;
            VeService.Create(data)
                .then(function (response) {  
                    if (response.success) { 
                        vm.tree.categories.push(response.data);
                        resetValues();
                        FlashService.Success(response.message);
                        vm.dataLoading = false;
                    } else {
                        FlashService.Error(response.message);
                        vm.dataLoading = false;
                    }
                });
        }           
        
        function loadCurrentUser() { 
            UserService.GetByEmail($rootScope.globals.currentUser.email)
                .then(function (response) { 
					
					if(response.success){
						vm.user = response.data;
						loadAllVideos(vm.db);
						loadAllCategories(vm.db);
						loadAllDomains(vm.db);
					}
                    
                    
                    
                });
        }
        
        
        function loadAllVideos(db) { 
            VeService.GetAllVideos(db)
                .then(function (response) { 
					
					if(response.success){
						angular.forEach(response.data, function(value, key) {
						  //value.time_created = parseInt(value.time_created);	
						  value.website_status = value.website == 1 ? 'Yes' : 'No';
						  this.push(value);
						}, vm.tree.nodes);
						 
					}
                    
                    
                });
        }
        
        function loadAllCategories(db) { 
            VeService.GetAllCategories(db)
                .then(function (response) {  
					
					if(response.success){ console.log(response.data);
						angular.forEach(response.data, function(value, key) {
						  //value.time_created = parseInt(value.time_created);	
						  this.push(value);
						}, vm.tree.categories);
						 
					}
                    
                    
                });
        }      
        
        function loadAllDomains(db) { 
            VeService.GetAllDomains(db)
                .then(function (response) {  
					
					if(response.success){ console.log(response.data);
						angular.forEach(response.data, function(value, key) {
						  //value.time_created = parseInt(value.time_created);	
						  this.push(value);
						}, vm.tree.domains);
						 
					}
                    
                    
                });
        }    
        
		function numberOfPages(length){ 
			console.log(length);
			return Math.ceil(length/vm.pageSize);                
		}            
        
        
        
        
        
        
        
        
        
        

		function del(data) {
			
			vm.user.p_id = data.id;
			vm.user.del = 'project';
			
					
            vm.dataLoading = true;
            
            DesignService.Deleteproject(vm.user)
                .then(function (response) {  
                    if (response.success) {
                        FlashService.Success(response.message, true);
						var index = vm.tree.nodes.indexOf(data);
						vm.tree.nodes.splice(index, 1);  
                        vm.dataLoading = false;
                    } else {
                        FlashService.Error(response.message);
                        vm.dataLoading = false;
                    }
                });
		};
		
		

		
        
		
		vm.hasNodes = function(data) {
			return (data.nodes.length > 0)
		};
    


    

        
        
        
        
    }
       
    

})();


