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

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

        // GET: TypeCollege
        public ActionResult Index()
        {
            TypeCollegeFactory fact = new TypeCollegeFactory(db);
            TypeCollegeIndexViewModel model = new TypeCollegeIndexViewModel(fact.getAll());
            return View(model);
        }

        // GET: TypeCollege/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
            Entity.TypeCollege typeCollege = typeCollegeFactory.getById(id.Value);
            if (typeCollege == null)
            {
                return HttpNotFound();
            }
            TypeCollegeViewModel model = new TypeCollegeViewModel(typeCollege);
            return View(model);
        }

        // GET: TypeCollege/Create
        public ActionResult Create()
        {
            Entity.TypeCollege typeCollege = new Entity.TypeCollege();
            TypeCollegeViewModel model = new TypeCollegeViewModel(typeCollege, ModeAcces.Creation);
            return View("Edit", model);
        }

        // POST: TypeCollege/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(TypeCollege typeCollege)
        {
            if (ModelState.IsValid)
            {
                TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
                typeCollegeFactory.add(ref typeCollege);
                return RedirectToAction("Index");
            }

            TypeCollegeViewModel model = new TypeCollegeViewModel(typeCollege, ModeAcces.Creation);
            return View("Edit", model);
        }

        // GET: TypeCollege/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
            Entity.TypeCollege typeCollege = typeCollegeFactory.getById(id.Value);
            if (typeCollege == null)
            {
                return HttpNotFound();
            }
            TypeCollegeViewModel model = new TypeCollegeViewModel(typeCollege, ModeAcces.Modification);
            return View(model);
        }

        // POST: TypeCollege/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(TypeCollege typeCollege)
        {
            if (ModelState.IsValid)
            {
                TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
                typeCollegeFactory.update(ref typeCollege);
                return RedirectToAction("Index");
            }
            TypeCollegeViewModel model = new TypeCollegeViewModel(typeCollege, ModeAcces.Modification);
            return View(model);
        }

        // GET: TypeCollege/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
            Entity.TypeCollege typeCollege = typeCollegeFactory.getById(id.Value);
            if (typeCollege == null)
            {
                return HttpNotFound();
            }
            DeleteViewModel model = new DeleteViewModel(typeCollege, "Type de Collège", typeCollege.Libelle);
            return View("~/Views/Shared/_AdminDeleteWarning.cshtml", model);
        }

        // POST: TypeCollege/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
            Entity.TypeCollege typeCollege = typeCollegeFactory.getById(id);
            typeCollegeFactory.delete(ref typeCollege);
            return RedirectToAction("Index");
        }

        public ActionResult Up(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
            Entity.TypeCollege typeCollege = typeCollegeFactory.getById(id.Value);
            typeCollegeFactory.Up(ref typeCollege);
            return RedirectToAction("Index");
        }

        public ActionResult Down(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TypeCollegeFactory typeCollegeFactory = new TypeCollegeFactory(db);
            Entity.TypeCollege typeCollege = typeCollegeFactory.getById(id.Value);
            typeCollegeFactory.Down(ref typeCollege);
            return RedirectToAction("Index");
        }

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