'use strict';

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

            var URL = {
                QUERY: '/qusion/api/classification/query',
                GET_BY_ALIAS: '/qusion/api/classification/getbyalias',
                DELETE: '/qusion/api/classification/delete',
                CREATE: '/qusion/api/classification/create',
                UPDATE: '/qusion/api/classification/Edit',
                GET_EDIT: '/qusion/api/classification/GetEdit'
            };

            var query = function(params) {
                var deferred = $q.defer();
                $http.get(URL.QUERY, {
                    params: params
                })
                    .success(function(data) {
                        var classifications = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            classifications.push(ClassificationFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ classifications: classifications, total: data.Count });
                    });

                return deferred.promise;
            };

            var getByAlias = function(alias) {
                var deferred = $q.defer();
                $http.get(URL.GET_BY_ALIAS, {
                    params: { alias: alias }
                })
                    .success(function(data) {

                        deferred.resolve(ClassificationFactory.create(data));
                    });

                return deferred.promise;
            };

            var getEdit = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET_EDIT, {
                    params: { id: id }
                })
                    .success(function (data) {

                        deferred.resolve(ClassificationFactory.edit(data));
                    });

                return deferred.promise;
            };

            var create = function(classification) {
                var deferred = $q.defer();
                $http.post(URL.CREATE, classification)
                    .success(function(data) {
                        deferred.resolve(ClassificationFactory.create(data));
                    });

                return deferred.promise;
            };

            var update = function (classification) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE, classification
                ).success(function (data) {
                    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;
            };

            return {
                query: query,
                getByAlias: getByAlias,
                remove: remove,
                create: create,
                update: update,
                getEdit: getEdit
            };
        }
    ])
    .factory('LabelService', [
        '$http',
        '$q',
        'System',
        'LabelFactory',
        function($http, $q, System, LabelFactory) {

            var URL = {
                QUERY: '/qusion/api/label/query',
                GET: '/qusion/api/label/get',
                DELETE: '/qusion/api/label/delete',
                CREATE: '/qusion/api/label/Create',
                EDIT: '/qusion/api/label/edit',
                GETEDIT: '/qusion/api/label/getedit',
                TOP: '/qusion/api/label/top',
                REORDER: '/qusion/api/label/reorder'
            };

            var query = function(params) {
                var deferred = $q.defer();
                $http.get(URL.QUERY, {
                    params: params
                })
                    .success(function(data) {
                        var labels = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            labels.push(LabelFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ labels: labels, total: data.Count });
                    });

                return deferred.promise;
            };
            
            var top = function (params) {
                var deferred = $q.defer();
                $http.get(URL.TOP, {
                    params: params
                })
                    .success(function (data) {
                        var labels = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            labels.push(LabelFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ labels: labels, total: data.Count });
                    });

                return deferred.promise;
            };

            var get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                })
                    .success(function (data) {

                        LabelFactory.create(data);

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            var create = function(label) {
                var deferred = $q.defer();
                $http.post(URL.CREATE, label)
                    .success(function(data) {
                        deferred.resolve(LabelFactory.create(data));
                    });

                return deferred.promise;
            };

            var update = function(label) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, label)
                    .success(function (data) {
                        deferred.resolve(LabelFactory.create(data));
                    });

                return deferred.promise;
            };
            
            var reorder = function (data) {
                var deferred = $q.defer();
                $http.post(URL.REORDER, { Labels: data }).success(function (label) {
                    deferred.resolve(label);
                });

                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;
            };

            return {
                query: query,
                remove: remove,
                create: create,
                update: update,
                get: get,
                top: top,
                reorder: reorder
            };
        }
    ]);