angular.module('app').factory('vendorService', ['$http', 'MARLINAPI_CONFIG', 'userService',
    function($http, MARLINAPI_CONFIG, User) {

        var url = MARLINAPI_CONFIG.base_url;


        // create and expose service methods
        var exports = {};

        // get all items
        exports.getAll = function() {
            return $http.get(url + 'vendors').then(function(response) {
                return response.data;
            });
        };

        // get one item by id
        exports.getById = function(id) {
            return $http.get(url + 'vendors/' + id).then(function(response) {
                return response.data;
            });
        };

        // update one item by item 
        // @note we figure out id from item
        exports.update = function(newItem) {
            var id = newItem._id;
            newItem = _.omit(newItem, '_id');
            return $http.put(url + 'vendors/' + id, newItem).then(function(response) {
                return response.data;
            });
        };

        // add a new item
        exports.add = function(item) {
            return $http.post(url + 'vendors', item).then(function(response) {
                return response.data;
            });
        };

        // remove item by item
        exports.remove = function(id) {
            return $http({
                method: 'DELETE',
                url: url + 'vendors/' + id
            }).then(function(response) {
                return response.data;
            });
        };

        // get one item by id
        exports.getSalesRep = function(vendorId) {
            return $http.get(url + 'vendors/' + vendorId + '/salesRep').then(function(response) {
                return response.data;
            });
        };

        /**
         * Add a vendor to a program using the id of each
         * @param int vendorId ID of the vendor
         * @param int programId ID of the program
         * @return array Updated array of programIds associated with vendor
         */
        exports.addProgramToVendor = function(programId, vendorId) {

            // gets array key for this vendor
            var theId = _.findIndex(itemList, function(item) {
                return item._id == vendorId;
            });

            //console.log(itemList[theId].programIds);

            // just in case this vendor has no ids array yet! 
            if (!itemList[theId].programIds) itemList[theId].programIds = [];

            itemList[theId].programIds.push(programId);

            return itemList[theId].programIds;
        };


        /**
         * Removes vendor from program the id of each
         *
         * @param int vendorId ID of the vendor
         * @param int programId ID of the program
         * @return array Updated array of programs for the vendor
         */
        exports.removeProgramFromVendor = function(programId, vendorId) {
            // gets array key for this vendor
            var theId = _.findIndex(itemList, function(item) {
                return item._id == vendorId;
            });

            itemList[theId].programIds = _.reject(itemList[theId].programIds, function(item) {
                return programId == item;
            });

            //console.log(itemList[theId].programIds);
            //console.log(itemList[theId]);

            return itemList[theId].programIds;
        };


        /**
         * Reduces the itemList to those where ID is in values array
         *
         * If ID exists multiple times, will only return item one time
         *
         */
        exports.getManyWhereIn = function(values) {
            var str = {};
            str._id = {
                "$in": values
            };

            var params = {
                query: JSON.stringify(str)
            };

            //console.log(params);

            return $http.get(url + 'vendors', {
                params: params
            }).then(function(response) {
                return response.data;
            });
        };

        /**
         * Reduces the itemList to those where ID is in values array
         *
         * If ID exists multiple times, will only return item one time
         *
         */
        exports.getManyWhereNotIn = function(values) {
            var str = {};
            str._id = {
                "$nin": values
            };

            var params = {
                query: JSON.stringify(str)
            };

            //console.log(params);

            return $http.get(url + 'vendors', {
                params: params
            }).then(function(response) {
                return response.data;
            });
        };


        exports.getAllWithoutSalesReps = function() {

            return $http.get(url + 'vendors').then(function(response) {

                var vendorIds = [];

                // get all the vendor ids
                _.each(response.data, function(item) {
                    if (item.vendorIds) vendorIds = vendorIds.concat(item.vendorIds);
                });

                // ensure they are unique
                vendorIds = _.uniq(vendorIds);

                //console.log(vendorIds);

                //console.log('There are ' + vendorIds.length + ' without sales reps');

                return exports.getManyWhereNotIn(vendorIds);

            });

        };
        
        exports.getAllVendorTags = function(tagType) {
        
            var type = tagType || 'tags';
        
            return $http.get(url + 'vendors/tags/' + type).then(function(response) {
                return _.sortBy(response.data);
            });
        };
        
        return exports;

    }
]);

@@ -84,16 +84,29 @@
 ervice',
+ '$location',
 %0A    fun
@@ -140,16 +140,27 @@
 IG, User
+, $location
 ) %7B%0A%0A   
@@ -248,16 +248,16 @@
 methods%0A
-
         
@@ -273,16 +273,299 @@
 ts = %7B%7D;
+%0A        %0A        // Generic find functionality%0A        exports.find = function(query) %7B%0A%0A            query = JSON.stringify(query);%0A%0A            return $http.post(url + 'vendors/find', query).then(function(response) %7B%0A                return response.data;%0A            %7D);%0A        %7D;
 %0A%0A      
@@ -2059,32 +2059,940 @@
   %7D);%0A        %7D;
+%0A        %0A        exports.lookup = function(callback) %7B%0A          %0A            var vendorSlug = $location.host().split('.')%5B0%5D;%0A            console.log(%22vendorSlug is: %22, vendorSlug);  %0A            %0A            // Private function that will execute callback if its valid%0A            function doCallback(result) %7B%0A                result = result %7C%7C null;%0A                if(callback && typeof callback === 'function') %7B%0A                    callback(result);%0A                %7D%0A            %7D%0A            %0A            // attempt to find vendor by slug%0A            exports.find(%7B 'slug' : vendorSlug %7D).then(function(response) %7B%0A                %0A                if(response && response.length === 1) %7B%0A                    callback(response%5B0%5D);%0A                %7D else %7B%0A                    callback();%0A                %7D           %0A                %0A            %7D);%0A            %0A            %0A            %0A        %7D;
 %0A%0A        /**%0A  

Add resolve lookup method to vendor service.