using CD67.FicheCollege.Entity;
using CD67.FicheCollege.Factory;
using CD67.FicheCollege.MVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;

namespace CD67.FicheCollege.MVC.Controllers
{
    public class ActionsEduController : Controller
    {
        private Entities db = new Entities();

        // GET: Actions
        public ActionResult Index(int? annee_id)
        {
            if (annee_id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            AnneeFactory fact = new AnneeFactory(db);
            Annee annee = fact.getById(annee_id);
            AnneeViewModel model = new AnneeViewModel(annee, db, ModeAcces.Lecture);
            return View(model);
        }

        // GET: ActionsEdu/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduFactory fact = new ActionEduFactory(db);
            Entity.ActionEdu actionEdu = fact.getById(id);
            if (actionEdu == null)
            {
                return HttpNotFound();
            }
            ActionEduViewModel model = new ActionEduViewModel(actionEdu, db);

            return View(model);
        }


        // GET: ActionEduAxe/Create
        public ActionResult Create(int? annee_id)
        {
            if (annee_id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEdu actionEdu = new ActionEdu();
            actionEdu.AnneeId = annee_id.Value;
            AnneeFactory fact = new AnneeFactory(db);
            actionEdu.Annee = fact.getById(annee_id);

            actionEdu.TokenId = Guid.NewGuid();

            ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Creation);
            return View("Edit", model);
        }

        // POST: ActionEduAxe/Create
        // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
        // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ActionEdu actionEdu)
        {
            if (ModelState.IsValid)
            {
                ActionEduFactory fact = new ActionEduFactory(db);
                fact.add(ref actionEdu);
                return RedirectToAction("Details", new { id = actionEdu.Id });
            }
            ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Creation);
            return View("Edit", model);
        }

        // GET: ActionEduAxe/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduFactory fact = new ActionEduFactory(db);
            ActionEdu action = fact.getById(id.Value);
            if (action == null)
            {
                return HttpNotFound();
            }
            ActionEduViewModel model = new ActionEduViewModel(action, db, ModeAcces.Modification);
            return View(model);
        }

        // POST: ActionEduAxe/Edit/5
        // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
        // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ActionEdu actionEdu)
        {
            if (ModelState.IsValid)
            {
                ActionEduFactory fact = new ActionEduFactory(db);
                fact.update(ref actionEdu);
                return RedirectToAction("Details", new { id = actionEdu.Id });
            }
            if (actionEdu.Annee == null)
            {
                db.ActionsEdu.Attach(actionEdu);
                db.Entry(actionEdu).Reference(i => i.Annee).Load();
            }
            ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Modification);
            return View(model);
        }

        // GET: ActionEduAxe/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduFactory fact = new ActionEduFactory(db);
            ActionEdu actionEdu = fact.getById(id.Value);
            if (actionEdu == null)
            {
                return HttpNotFound();
            }
            ActionEduViewModel model = new ActionEduViewModel(actionEdu, db, ModeAcces.Suppression);
            return View("Details", model);
        }

        // POST: ActionEduAxe/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ActionEduCollegeFactory aec_fact = new ActionEduCollegeFactory(db);
            aec_fact.deleteMany(a => a.ActionEduId == id);

            ActionEduFactory fact = new ActionEduFactory(db);
            ActionEdu actionEdu = fact.getById(id);
            int annee_id = actionEdu.AnneeId;
            fact.delete(ref actionEdu);
            return RedirectToAction("Index", new { annee_id = annee_id });
        }

        public List<ActionEdu> actions_non_importees(int annee_id)
        {
            List<ActionEdu> non_importees = new List<ActionEdu>();
            AnneeFactory fact = new AnneeFactory(db);
            Annee annee_prec = fact.getById(annee_id - 1);
            foreach (ActionEdu actionEdu in annee_prec.ActionsEdu)
            {
                // Pour chaque college de l'année précédente, on vérifie s'il existe dans l'année en cours en se basant sur le TokenId
                if (!db.ActionsEdu.Any(a => a.TokenId == actionEdu.TokenId && a.AnneeId == annee_id))
                {
                    non_importees.Add(actionEdu);
                }
            }
            return non_importees;
        }

        // GET: Colleges
        public ActionResult Import(int? annee_id)
        {
            if (annee_id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            ImportActionEduViewModel model = new ImportActionEduViewModel();
            model.Annee_Id = annee_id.Value;
            model.Annee_Lib = db.Annees.Where(a => a.Id == annee_id.Value).First().Libelle;
            List<ActionEdu> non_importes = actions_non_importees(annee_id.Value);

            model.set_liste(non_importes);

            for (int i = 0; i < non_importes.Count; i++)
            {
                ActionEdu actionEdu = non_importes[i];
                foreach (ActionEduCollege aec in actionEdu.ActionsEduColleges)
                {
                    if (!db.Colleges.Where(c=>c.TokenId==aec.College.TokenId && c.Annee_Id == annee_id.Value).Any())
                    {
                        model.actionsEdu_averts[i].Add(aec.College.Libelle);
                    }
                }
            }

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Import(int Annee_Id, List<int> actionsEdu_ids, List<bool> selection)
        {
            ActionEduFactory fact = new ActionEduFactory(db);
            for (int i = 0; i < selection.Count; i++)
            {
                if (selection[i])
                {
                    int actionEdu_id = actionsEdu_ids[i];
                    ActionEdu actionEdu = fact.getById(actionEdu_id);
                    fact.clone_to_year(actionEdu, Annee_Id);
                }
            }
            return RedirectToAction("Index", new { annee_id = Annee_Id });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}