/**
 * Created by KLE-Talha on 23/02/16.
 */ 
var util = require('util');
var async = require('async');
var mongoose = require('mongoose')
var utils = rekuire('/lib/utils')
var extend = require('util')._extend
var SectionGrup = mongoose.model('SectionGrup');
var SectionType = mongoose.model('SectionType');
var User = mongoose.model('User');
var Section = mongoose.model('Section');



exports.model = mongoose.model('Section');
exports.title = 'İçerik';
exports.singularName = 'section';
exports.pluralName = 'sections';
var BaseController = require('elderwand').BaseController;
BaseController.inherit(exports);




exports.super.loadRelatedData = exports.loadRelatedData
 exports.loadRelatedData = function(data, req, res, callback){  
  exports.super.loadRelatedData(data, req, res, function(newData){
    async.parallel([
      function(next){
        Section.list({}, function(err, result){
          newData.sections = result;
          next()
        });
      },
      function(next){
        SectionGrup.list({}, function(err, result){
          newData.sectionGrups = result;
          next()
        });
      },
      function(next){
        SectionType.list({}, function(err, result){
          newData.sectionTypes = result;
          next()
        });
      }
    ], function(err){
      callback(newData); 
    }) 
  });
 }               
 
 exports.IndexControl = function (req, res) {
  var sectionGrup = String(req.body.sectionGrup);
  var sectionIndex = Number(req.body.sectionIndex);
  var data = {};
  async.parallel([
    function (next) {
      data.section = {};

      var options = {
        criteria: {sectionIndex: sectionIndex , sectionGrup: sectionGrup},
        select: 'sectionIndex '
      };
      Section.load2(options, function (err, section) {
        if(section.length !=0)
        {
        	data.section.status=false;
        	data.section.message='Aynı grup içinde aynı index e sahip section olamaz';
        	next();
        }
        else{
        	data.section.status=true;
        //data.section.message='ekleme başarılı';
        next();
        }	
      });
        
    }
    ], function (err) {
    	if (data.section.status) {
          exports.create(req, res);
        	return;     
      	}else{
      		req.flash('error', 'Aynı grup içinde aynı index e sahip section olamaz');
      		res.redirect("/sections/new");
        	return;
     	}    	
    });
}


exports.section = function (req, res) {
  
  var data = {};
  
  async.series([
    function (next) {
      data.project = {};
      
      var options = {
        criteria: {_id: req.params.id},
        select: 'sectionGrup'
      };
      SectionGrup.load2(options, function (err, sectionGrup) {
        data.sectionGrups = sectionGrup;
         EWLog(data);      
        next()
      });

    },
    function (next) {
      EWLog(data)
      data.section = {};
       var options2 = {
        criteria: { sectionGrup: data["sectionGrups"]["0"].sectionGrup},
        select: 'sectionType sectionIndex sectionGrup items _id'
      };
      Section.load2(options2, function (err, section) {
        data.sections = section;
        EWLog(data);
        next(); 
      });
       
    }
   
    ], function (err) {
      res.render("sections/liste", data);    
    });
}

exports.document = function (req, res) {
  res.render('documentation.html', {
    title: 'Elderwand'
  });
};


