using System.Net;
using System.Web.Mvc;
using CD67.ModeleMVC.Entity;
using CD67.ModeleMVC.Factory;


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

        // GET: VIKINGS
        public ActionResult Index()
        {
            VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
            return View(vikingsFactory.getAll());
        }

        public ActionResult Test()
        {
            return View();
        }

        // GET: VIKINGS/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
            EXEMPLE_VIKINGS viking = vikingsFactory.getById(id.Value);
            if (viking == null)
            {
                return HttpNotFound();
            }
            ViewBag.id = id;
            return View(viking);
        }

        // GET: VIKINGS/Create
        public ActionResult Create()
        {
            EXEMPLE_VIKINGS viking = new EXEMPLE_VIKINGS();
            FillSelect(viking);
            return View(viking);
        }

        // POST: VIKINGS/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([Bind(Include = "ID,NOM,ID_TYPE,DESCRIPTION,DATE_INUTILE")] EXEMPLE_VIKINGS viking)
        {
            if (ModelState.IsValid)
            {
                VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
                vikingsFactory.add(ref viking);
                return RedirectToAction("Index");
            }
            FillSelect(viking);
            return View(viking);
        }

        // GET: VIKINGS/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
            EXEMPLE_VIKINGS viking = vikingsFactory.getById(id.Value);
            if (viking == null)
            {
                return HttpNotFound();
            }
            FillSelect(viking);
            return View(viking);
        }

        // POST: VIKINGS/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([Bind(Include = "ID,NOM,ID_TYPE,DESCRIPTION,DATE_INUTILE")] EXEMPLE_VIKINGS viking)
        {
            if (ModelState.IsValid)
            {
                VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
                vikingsFactory.update(ref viking);
                return RedirectToAction("Index");
            }
            FillSelect(viking);
            return View(viking);
        }

        // GET: VIKINGS/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
            EXEMPLE_VIKINGS viking = vikingsFactory.getById(id.Value);
            if (viking == null)
            {
                return HttpNotFound();
            }
            return View(viking);
        }

        // POST: VIKINGS/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            VIKINGSFactory vikingsFactory = new VIKINGSFactory(db);
            EXEMPLE_VIKINGS viking = vikingsFactory.getById(id);
            vikingsFactory.delete(ref viking);
            return RedirectToAction("Index");
        }

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

        private void FillSelect(EXEMPLE_VIKINGS viking)
        {
            TYPE_VIKINGFactory typeVikingFactory = new TYPE_VIKINGFactory(db);
            ViewBag.ID_TYPE = new SelectList(typeVikingFactory.getAll(), "ID", "TYPE", viking.ID_TYPE);
        }
    }
}
