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 RestaurationTypesRepasController : Controller
    {
        private Entities db = new Entities();

        // GET: RestaurationTypesRepas
        public ActionResult Index()
        {
            RestaurationTypesRepaFactory fact = new RestaurationTypesRepaFactory(db);
            RestaurationTypesRepaIndexViewModel model = new RestaurationTypesRepaIndexViewModel(fact.getAll());
            return View(model);
        }

        // GET: RestaurationTypesRepas/Create
        public ActionResult Create()
        {
            RestaurationTypesRepa repas = new RestaurationTypesRepa();

            RestaurationTypesRepaViewModel model = new RestaurationTypesRepaViewModel(repas, ModeAcces.Creation);
            return View("Edit", model);
        }

        // POST: RestaurationTypesRepas/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(RestaurationTypesRepa repas)
        {
            if (ModelState.IsValid)
            {
                RestaurationTypesRepaFactory fact = new RestaurationTypesRepaFactory(db);
                fact.add(ref repas);
                return RedirectToAction("Index");
            }

            RestaurationTypesRepaViewModel model = new RestaurationTypesRepaViewModel(repas, ModeAcces.Creation);
            return View("Edit", model);
        }

        // GET: RestaurationTypesRepas/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurationTypesRepaFactory fact = new RestaurationTypesRepaFactory(db);
            RestaurationTypesRepa repas = fact.getById(id.Value);
            if (repas == null)
            {
                return HttpNotFound();
            }
            RestaurationTypesRepaViewModel model = new RestaurationTypesRepaViewModel(repas, ModeAcces.Modification);
            return View(model);
        }

        // POST: RestaurationTypesRepas/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(RestaurationTypesRepa repas)
        {
            if (ModelState.IsValid)
            {
                RestaurationTypesRepaFactory fact = new RestaurationTypesRepaFactory(db);
                fact.update(ref repas);
                return RedirectToAction("Index");
            }
            RestaurationTypesRepaViewModel model = new RestaurationTypesRepaViewModel(repas, ModeAcces.Modification);
            return View(model);
        }

        // GET: RestaurationTypesRepas/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurationTypesRepaFactory fact = new RestaurationTypesRepaFactory(db);
            RestaurationTypesRepa repas = fact.getById(id.Value);
            if (repas == null)
            {
                return HttpNotFound();
            }
            DeleteViewModel model = new DeleteViewModel(repas, "Type de Repas", repas.Libelle);
            return View("~/Views/Shared/_AdminDeleteWarning.cshtml", model);
        }

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

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

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

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