
/**
 * Module dependencies.
 */

var mongoose = require('mongoose');
var Imager = rekuire('imager');
var config = rekuire('config');
var imagerConfig = rekuire(config.root + '/config/imager.js');
var utils = require('../../lib/utils')

var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;

/**
 * Getters
 */

var getTags = function (tags) {
  return tags.join(',');  
};

/**
 * Setters
 */

var setTags = function (tags) {
  return tags.split(',');
};

/**
 * Article Schema
 */



var SectionSchema = new Schema({
  sectionType: { type: String, default: '', trim : true  },
  sectionGrup: { type: String, default: '', trim : true },
  sectionIndex: {type: Number, trim : true},
  tag: {type: String, default: '', trim : true},
  files: [{ type: String }],
  items: [{
    title: { type : String, default: ' ', trim : true  },
    subtitle: { type : String, default: '', trim : true },
    code: { type : String, default: '', trim : true },
    imageUrl: { type: String }
  }]

});

/**
 * Validations
 */

SectionSchema.path('sectionType').required(true, 'Section tipi boş olamaz');
SectionSchema.path('sectionGrup').required(true, 'Section grubu boş olamaz');
SectionSchema.path('sectionIndex').required(true, 'Section index boş olamaz');
SectionSchema.path('tag').required(true, 'Tag boş olamaz');


/**
 * Pre-remove hook
 */

SectionSchema.pre('remove', function (next) {
  //var imager = new Imager(imagerConfig, 'S3');
  //var files = this.image.files;

  // if there are files associated with the item, remove from the cloud too
  //imager.remove(files, function (err) {
    //if (err) return next(err);
  //}, 'section');

  next();
});

/**
 * Methods
 */

SectionSchema.pre('save', function(next) {
  next();
});


SectionSchema.methods = {

  /**
   * Save article and upload image
   *
   * @param {Object} images
   * @param {Function} cb
   * @api private
   */

  canSeeByUser: function(user){
    return true;
  },
  canEditByUser: function(user) {
    
      return true;
  },

  
};

/**
 * Statics
 */
SectionSchema.statics = {

  /**
   * Find article by id
   *
   * @param {ObjectId} id
   * @param {Function} cb
   * @api private
   */
load: function (id, cb) {
    this.findOne({_id: id})
      .select('sectionType sectionGrup sectionIndex items _id')
      .exec(cb);
  },
  load2: function (options, cb) {
    options.select = options.select || 'sectionType sectionGrup sectionIndex items _id';
    this.find(options.criteria)
      .select(options.select)
      .sort({ 'sectionIndex' : 1})
      .exec(cb);
  },


  /**
   * List articles
   *
   * @param {Object} options
   * @param {Function} cb
   * @api private
   */

  list: function (options, cb) {
    var criteria = options.criteria || {}

    this.find(criteria)
      .populate('user', 'name username')
      .sort({ 'sectionIndex' : 1}) // sort by date
      .limit(options.perPage)
      .skip(options.perPage * options.page)
      .exec(cb);
  },
  
  
}

var Section = mongoose.model('Section', SectionSchema);
