'use strict';
'use strict';

/**
 */

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

            var URL = {
                GET: '/qusion/api/video/Get',
                ALL: '/qusion/api/video/Query',
                DELETE: '/qusion/api/video/remove',
                ORGANIZE: '/qusion/api/video/organize',
                LIBRARIES: '/qusion/api/library/GetByType',
                DELETE_LIB: '/qusion/api/video/removelibrary',
                SETTINGS: '/qusion/api/video/settings',
                SEARCH_YOUTUBE: '/qusion/api/Video/SearchYoutTube',
                SAVEYTVIDEOS: '/qusion/api/Video/SaveYouTubeVideo',
                EDITYT: '/qusion/api/video/editYouTube',
                EDIT: '/qusion/video/edit',
                DELETE_SETTINGS: '/qusion/api/video/removesettings'
            };
            
            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 remove = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE, {
                    params: params
                })
                    .success(function (data) {

                        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 saveYtVideos = function(data) {
                var deferred = $q.defer();
                $http.post(URL.SAVEYTVIDEOS, data).success(function (response) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };
            
            var editYtVideo = function (data) {
                var deferred = $q.defer();
                $http.post(URL.EDITYT, data).success(function (response) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };
            
            var editVideo = function (video) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, video)
                    .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 removeLibrary = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE_LIB, {
                    params: params
                })
                    .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,
                saveYtVideos: saveYtVideos,
                editYtVideo: editYtVideo,
                organize: organize,
                remove: remove,
                get: get,
                editVideo: editVideo,
                removeLibrary: removeLibrary,
                removeSettings: removeSettings
            };
        }
    ]);
