using CD67.FicheCollege.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using System.Linq.Expressions;

namespace CD67.FicheCollege.Factory
{
    public partial class AnneeFactory : Internal.BaseFactory<Entity.Annee>
    {
        //public AnneeFactory(Entities dbContext) : base(dbContext) { }

        public Annee getByLibelle(params object[] keyValues)
        {
            string libelle = keyValues[0].ToString();
            
            Annee annee = base.getAll().Where(a=>a.Libelle == libelle).First();
            //si l'élément n'existe pas, on le créé
            if (annee == null)
            {
                annee = new Annee()
                {
                    Libelle = libelle
                };
                this.add(ref annee);
                dbContext.Entry(annee).Reference(i => i.Colleges).Load();
            }

            return annee;
        }

        // Retourne l'id de l'année courante
        public int get_current_year_id()
        {
            string annee_lib;
            if (DateTime.Now.Month <= 7)
            {
                annee_lib = String.Format("{0}-{1}", DateTime.Now.Year - 1, DateTime.Now.Year);
            }
            else
            {
                annee_lib = String.Format("{0}-{1}", DateTime.Now.Year, DateTime.Now.Year + 1);
            }
            return dbContext.Annees.Where(a => a.Libelle == annee_lib).First().Id;
        }


    }
}