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

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

        // GET: ActionsEdu
        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);
            return View(model);
        }

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

            return View(model);
        }

        // GET: ActionEduCollege/Create
        public ActionResult Create(int? actionEdu_id)
        {
            if (actionEdu_id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduCollege actionEduCollege = new ActionEduCollege();
            actionEduCollege.ActionEduId = actionEdu_id.Value;
            ActionEduFactory fact = new ActionEduFactory(db);
            actionEduCollege.ActionEdu = fact.getById(actionEdu_id);

            ActionEduCollegeViewModel model = new ActionEduCollegeViewModel(actionEduCollege, 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(ActionEduCollege actionEduCollege)
        {
            if (ModelState.IsValid)
            {
                ActionEduCollegeFactory fact = new ActionEduCollegeFactory(db);
                fact.add(ref actionEduCollege);
                return RedirectToAction("Details", "ActionsEdu", new { Id = actionEduCollege.ActionEduId });
            }

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

        // GET: ActionEduAxe/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduCollegeFactory fact = new ActionEduCollegeFactory(db);
            ActionEduCollege actionEduCollege = fact.getById(id.Value);
            if (actionEduCollege == null)
            {
                return HttpNotFound();
            }
            ActionEduCollegeViewModel model = new ActionEduCollegeViewModel(actionEduCollege, 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(ActionEduCollege actionEduCollege)
        {
            if (ModelState.IsValid)
            {
                ActionEduCollegeFactory fact = new ActionEduCollegeFactory(db);
                fact.update(ref actionEduCollege);
                return RedirectToAction("Details", new { id = actionEduCollege.Id });
            }
            ActionEduCollegeViewModel model = new ActionEduCollegeViewModel(actionEduCollege, db, ModeAcces.Modification);
            return View(model);
        }

        // GET: ActionEduAxe/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduCollegeFactory fact = new ActionEduCollegeFactory(db);
            ActionEduCollege actionEduCollege = fact.getById(id.Value);
            if (actionEduCollege == null)
            {
                return HttpNotFound();
            }
            ActionEduCollegeViewModel model = new ActionEduCollegeViewModel(actionEduCollege, db);
            return View(model);
        }

        // POST: ActionEduAxe/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ActionEduCollegeFactory fact = new ActionEduCollegeFactory(db);
            ActionEduCollege actionEduCollege = fact.getById(id);
            fact.delete(ref actionEduCollege);
            return RedirectToAction("Details", "ActionsEdu", new { id = actionEduCollege.ActionEduId });
        }

    }
}