(function () {
    'use strict';

    angular
        .module('app')
        .factory('VeService', VeService);

    VeService.$inject = ['$http'];
    function VeService($http) {
        var service = {};

        service.GetAllVideos = GetAllVideos;
        service.GetAllDataDownload = GetAllDataDownload;
        service.GetAllCamVideos = GetAllCamVideos;
        service.GetAllCategories = GetAllCategories;
        service.GetAllDomains = GetAllDomains;
        service.GetAllCountries = GetAllCountries;
        service.GetAllTags = GetAllTags;
        service.GetAllStates = GetAllStates;
        service.GetAllStats = GetAllStats;
        
        service.Create = Create;
        service.ReportCreate = ReportCreate;
        service.Rate = Rate;
                        

        return service;

        function GetAllVideos(project_name,limit,offset) {
            
            var guest = window.location.hash == '#!/ct' ? 1 : 0;
            
            return $http.post('api/videos.php', {cat_name:project_name,type:'videos',guest:guest,limit:limit,offset:offset}).then(handleSuccess, handleError('Error fetchng All videos'));
        }
		
		
		function GetAllDataDownload(project_name,limit,offset) {
		    
		    
           // var guest = window.location.hash == '#!/ct' ? 1 : 0;
		   var guest = 0;
            return $http.post('api/datadownload.php', {cat_name:project_name,type:'videos',limit:limit,offset:offset,guest:guest}).then(handleSuccess, handleError('Error fetchng All videos'));
        }
		
		
		function GetAllCamVideos(project_name,limit,offset) {
            return $http.post('api/cam_videos.php', {cat_name:project_name,type:'videos',limit:limit,offset:offset}).then(handleSuccess, handleError('Error fetchng All videos'));
        }

        function GetAllCategories(project_name) {
            return $http.post('api/videos.php', {cat_name:project_name,type:'categories'}).then(handleSuccess, handleError('Error fetchng All categories'));
        }        
        
        function GetAllDomains(project_name) {
            return $http.post('api/videos.php', {cat_name:project_name,type:'domains'}).then(handleSuccess, handleError('Error fetchng All domains'));
        } 
        function GetAllCountries() {
            return $http.post('api/country.php', {}).then(handleSuccess, handleError('Error fetchng All domains'));
        }
        function GetAllStates(id) {
            return $http.post('api/state.php', {cid:id}).then(handleSuccess, handleError('Error fetchng All domains'));
        }  
		function GetAllStats(catid) {
            return $http.post('api/stats.php', {catid:catid}).then(handleSuccess, handleError('Error fetchng All domains'));
        } 
        function GetAllTags(project_name) {
            return $http.post('api/videos.php', {cat_name:project_name,type:'tags'}).then(handleSuccess, handleError('Error fetchng All tags'));
        }        
        
        function Create(data) {
            return $http.put('api/videos.php', data).then(handleSuccess, handleError('Error adding video'));
        } 

        /* New2 */ 
        function ReportCreate(client_name, domain_name, country, rating, cat_name, tags, creation_date) {
            return $http.put('api/downloadvideos.php', {clientName:client_name,domainName:domain_name,country:country,rating:rating,catName:cat_name,tags:tags,creationDate:creation_date}).then(handleSuccess, handleError('Error Exporting File'));			
        }  
        /* New2 */
            
        function Rate(data) {
            return $http.put('api/videos.php', data).then(handleSuccess, handleError('Error adding video'));
        }             
        
        
          
        
        // private functions
        function handleSuccess(res) {
            return res.data;
        }

        function handleError(error) {
            return function () {
                return { success: false, message: error };
            };
        }
    }

})();
