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

        // GET: RestaurationTypes
        public ActionResult Index()
        {
            RestaurationTypeFactory fact = new RestaurationTypeFactory(db);
            RestaurationTypeIndexViewModel model = new RestaurationTypeIndexViewModel(fact.getAll());
            return View(model);
        }

        // GET: RestaurationTypes/Create
        public ActionResult Create()
        {
            RestaurationType type = new RestaurationType();

            RestaurationTypeViewModel model = new RestaurationTypeViewModel(type, ModeAcces.Creation);
            return View("Edit", model);
        }

        // POST: RestaurationTypes/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(RestaurationType type)
        {
            if (ModelState.IsValid)
            {
                RestaurationTypeFactory fact = new RestaurationTypeFactory(db);
                fact.add(ref type);
                return RedirectToAction("Index");
            }

            RestaurationTypeViewModel model = new RestaurationTypeViewModel(type, ModeAcces.Creation);
            return View("Edit", model);
        }

        // GET: RestaurationTypes/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurationTypeFactory fact = new RestaurationTypeFactory(db);
            RestaurationType type = fact.getById(id.Value);
            if (type == null)
            {
                return HttpNotFound();
            }
            RestaurationTypeViewModel model = new RestaurationTypeViewModel(type, ModeAcces.Modification);
            return View(model);
        }

        // POST: RestaurationTypes/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(RestaurationType type)
        {
            if (ModelState.IsValid)
            {
                RestaurationTypeFactory fact = new RestaurationTypeFactory(db);
                fact.update(ref type);
                return RedirectToAction("Index");
            }
            RestaurationTypeViewModel model = new RestaurationTypeViewModel(type, ModeAcces.Modification);
            return View(model);
        }

        // GET: RestaurationTypes/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            RestaurationTypeFactory fact = new RestaurationTypeFactory(db);
            RestaurationType type = fact.getById(id.Value);
            if (type == null)
            {
                return HttpNotFound();
            }
            RestaurationTypeViewModel model = new RestaurationTypeViewModel(type);
            return View(model);
        }

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

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

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

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