/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    env = process.env.NODE_ENV || 'development',
    config = require('../../config/config')[env],
    Schema = mongoose.Schema,
    _ = require('lodash');

var customNameSchema = new Schema({
    type: Schema.ObjectId,
    displayName: String
});

var toolEnabledSchema = new Schema({
    "name": String,
    "active": Boolean,
    "slug": String
});

/**
 * Vendor Schema
 * @todo refactor tools to be use slug as keys? is this possible?
 */
var VendorSchema = new Schema({
    "created": {
        type: Date,
        "default": Date.now
    },
    "name": {
        type: String,
        "default": '',
        trim: true
    },
    "contactPerson": {
        "name": {
            type: String,
            "default": '',
            trim: true
        },
        "email": {
            type: String,
            "default": '',
            trim: true
        },
        "phone": {
            type: String,
            "default": '',
            trim: true
        }
    },
    "salesRep": {
        type: Schema.ObjectId,
        ref: 'User'
    },
    "vendorRep": {
        type: Schema.ObjectId,
        ref: 'User'
    },
    "logo": {
        "original": {
            type: String,
            "default": '',
            trim: true
        }
    },
    "website": {
        type: String,
        "default": '',
        trim: true
    },
    "legalTerms": {
        type: String,
        "default": '',
        trim: true
    },
    "businessPhone": {
        type: String,
        "default": '',
        trim: true
    },
    "businessAddress": {
        "address1": {
            type: String,
            "default": '',
            trim: true
        },
        "address2": {
            type: String,
            "default": '',
            trim: true
        },
        "city": {
            type: String,
            "default": '',
            trim: true
        },
        "state": {
            type: String,
            "default": '',
            trim: true
        },
        "zip": {
            type: String,
            "default": '',
            trim: true
        }
    },
    "geo": {
        "latitude": {
            type: Number,
            "default": null
        },
        "longitude": {
            type: Number,
            "default": null
        }
    },
    "tools": [toolEnabledSchema],
    "programs": [{
        type: Schema.ObjectId,
        ref: 'Program'
    }],
    "programCustomNames": [customNameSchema],
    "customField": {
        required: {
            type: Boolean,
            "default": false
        },
        enabled: {
            type: Boolean,
            "default": false
        },
        displayName: {
            type: String,
            "default": '',
            trim: true
        }
    }
});

var troop = require('mongoose-troop');
VendorSchema.plugin(troop.merge);

var taggable = require('mongoose-taggable');
VendorSchema.plugin(taggable, {'path':'tags'});

/**
 * Statics
 */
VendorSchema.statics = {

    // this gets called when a vendorId is present in the req.params
    // its a clever way to be able to access req.vendor in the next middlewares
    //
    load: function(id, cb) {
        this.findOne({
            _id: id
        }).populate('programs salesRep vendorRep').exec(cb);
    }
};

/**
 * Runs similar to "after get" callback
 *
 */
VendorSchema.pre('init', function(next, data) {

    // iterate through each program and replace its name with custom name if set
    // @todo not the most effecient
    // attempts to just "merge" the 2 datasets failed, and replaced all the props
    // of programs with custom name
    _.each(data.programs, function(item) {
        var customName = _.where(data.programCustomNames, {
            _id: item._id
        });
        item.displayName = customName.length ? customName[0].displayName : null;
    });

    next();
});

function convertToSlug(Text) {
    return Text
        .toLowerCase()
        .replace(/ /g, '-')
        .replace(/[^\w\-]+/g, '');
}

VendorSchema.pre('save', function(next) {
    
    var vendor = this;
    
    _.each(vendor.vendorTags, function(item) {     
        vendor.addTag(item.text, function(err, addedTag) {
            //console.log(err);
            //console.log(addedTag);
        });
    });

    _.each(vendor.tools, function(item) {
        item.slug = convertToSlug(item.name);
    });

    next();
});

VendorSchema.statics = {

    getCurrentReps: function(vendorId, cb) {

        // attempt to get the sales rep, which we save with the quote
        // for easy geting from the database 
        return this.findOne({
            _id: vendorId
        }, function(err, result) {
            if (err) return cb(err);
            if (result && result._id) {
                console.log(result);
                return cb(null, result);
            } else {
                return cb(new Error(vendorId + ' is Not a valid vendor id'));
            }
        });
    },
    load: function(id, cb) {
        this.findOne({
            _id: id
        }).populate('programs salesRep vendorRep').exec(cb);
    }
};

mongoose.model('Vendor', VendorSchema);
@@ -4130,24 +4130,164 @@
  this;%0A    %0A
+    var vendorTags = %5B%5D;  // tags that are currently being passed from the vendor%0A    %0A    // create a unified array of current vendor tags%0A
     _.each(v
@@ -4320,21 +4320,224 @@
 (item) %7B
+%0A
      
+   vendorTags.push(item.text);%0A    %7D);%0A%0A    var newTags = _.difference(vendorTags, vendor.tags);%0A    var removeTags = _.difference(vendor.tags, vendorTags);%0A        %0A    _.each(newTags, function(item) %7B
 %0A       
@@ -4555,21 +4555,16 @@
 Tag(item
-.text
 , functi
@@ -4587,34 +4587,32 @@
 ) %7B%0A            
-//
 console.log(err)
@@ -4607,21 +4607,158 @@
 ole.log(
-err);
+'Added: ' + item);%0A        %7D);%0A    %7D);%0A    %0A    _.each(removeTags, function(item) %7B%0A        vendor.removeTag(item, function(err, removedTag) %7B
 %0A       
@@ -4762,18 +4762,16 @@
         
-//
 console.
@@ -4774,24 +4774,34 @@
 ole.log(
-addedTag
+'Removed: ' + item
 );%0A     

Enable user to delete tags from vendors