'use strict';

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

            var URL = {
                QUERY: '/cms/api/classificationlaktasi/query',
                GET_BY_ALIAS: '/cms/api/classificationlaktasi/getbyalias'    
            };

            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;
            };
       
            return {
                query: query,
                getByAlias: getByAlias
            };
        }
    ])
    .factory('LabelService', [
        '$http',
        '$q',
        'System',
        'LabelFactory',
        function($http, $q, System, LabelFactory) {

            var URL = {
                QUERY: '/cms/api/labellaktasi/query',
                GET: '/cms/api/labellaktasi/get',
                TOP: '/cms/api/labellaktasi/top',
                REORDER: '/cms/api/labellaktasi/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 reorder = function (data) {
                var deferred = $q.defer();
                $http.post(URL.REORDER, { Labels: data }).success(function (label) {
                    deferred.resolve(label);
                });

                return deferred.promise;
            };

          

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