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

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

        // GET: ActionAxes
        public ActionResult Index()
        {
            ActionEduAxeFactory fact = new ActionEduAxeFactory(db);
            ActionEduAxeIndexViewModel model = new ActionEduAxeIndexViewModel(fact.getAll());
            return View(model);
        }

        // GET: ActionEduAxe/Create
        public ActionResult Create()
        {
            ActionEduAxe axe = new ActionEduAxe();

            ActionEduAxeViewModel model = new ActionEduAxeViewModel(axe, 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(ActionEduAxe axe)
        {
            if (ModelState.IsValid)
            {
                ActionEduAxeFactory fact = new ActionEduAxeFactory(db);
                fact.add(ref axe);
                return RedirectToAction("Index");
            }

            ActionEduAxeViewModel model = new ActionEduAxeViewModel(axe, ModeAcces.Creation);
            return View("Edit", model);
        }

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

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

        // POST: ActionEduAxe/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ActionEduAxeFactory fact = new ActionEduAxeFactory(db);
            ActionEduAxe axe = fact.getById(id);
            fact.delete(ref axe);
            return RedirectToAction("Index");
        }

        public ActionResult Up(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduAxeFactory fact = new ActionEduAxeFactory(db);
            ActionEduAxe axe = fact.getById(id.Value);
            fact.Up(ref axe);
            return RedirectToAction("Index");
        }

        public ActionResult Down(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ActionEduAxeFactory fact = new ActionEduAxeFactory(db);
            ActionEduAxe axe = fact.getById(id.Value);
            fact.Down(ref axe);
            return RedirectToAction("Index");
        }

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