'use strict';
'use strict';

var VideoService = angular.module('videos.services', [
    'system'
])
    .factory('VideoService', [
        '$http',
        '$q',
        'System',
        function ($http, $q) {

            var URL = {
                GET: '/cms/api/videolaktasi/Get',
                ALL: '/cms/api/videolaktasi/Query',
                ORGANIZE: '/cms/api/videolaktasi/organize',
                LIBRARIES: '/cms/api/videolaktasi/GetByType',
                SETTINGS: '/cms/api/videolaktasi/settings',
                SEARCH_YOUTUBE: '/cms/api/videolaktasi/SearchYoutTube',
                SAVEYTVIDEOS: '/cms/api/videolaktasi/SaveYouTubeVideo'
            };
            
            var get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                }).success(function (data) {
                    
                    deferred.resolve(data);
                });

                return deferred.promise;
            };
            
            var query = function (params) {
                var deferred = $q.defer();
                $http.get(URL.ALL, {
                    params: params
                })
                    .success(function (data) {
                        console.log(data);
                        var images = [];
                        for (var i = 0; i < data.Videos.length; i++) {
                            //images.`push(ImageFactory.create(data.Images[i]));
                        }
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
           
            
            var searchYT = function (params) {
                var deferred = $q.defer();
                $http.get(URL.SEARCH_YOUTUBE, {
                    params: params
                })
                    .success(function (data) {
                        
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
            var organize = function (members) {
                var deferred = $q.defer();
                $http.post(URL.ORGANIZE, members)
                    .success(function (data) {
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };


            var libraries = function () {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIES, {
                    params: { 'type': 'Video' }
                })
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };                      
            
            var getSettings = function () {
                var deferred = $q.defer();
                $http.get(URL.SETTINGS, {}).success(function (data) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };

            var saveSettings = function (settings) {
                var deferred = $q.defer();
                $http.post(URL.SETTINGS, settings).success(function (data) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };
            
            var removeSettings = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE_SETTINGS, {
                    params: params
                })
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            return {
                query: query,
                libraries: libraries,
                getSettings: getSettings,
                saveSettings: saveSettings,
                searchYT: searchYT,
                organize: organize,
                get: get,
                removeSettings: removeSettings
            };
        }
    ]);
