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 DAL;
using Domain;
using WebApp.ViewModels;

namespace WebApp.Controllers
{
    public class FooController : Controller
    {
        private DataBaseContext db = new DataBaseContext();

        // GET: Foo
        public ActionResult Index()
        {
            var foos = db.Foos.Include(f => f.FooItem);
            return View(foos.ToList());
        }

        // GET: Foo/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Foo foo = db.Foos.Find(id);
            if (foo == null)
            {
                return HttpNotFound();
            }
            return View(foo);
        }

        // GET: Foo/Create
        public ActionResult Create()
        {
            var vm = new FooCreateViewModel
                     {
                         FooItemSelectList = new SelectList(db.FooItems, "FooItemId", "ItemValue")
                     };
            return View(vm);
        }

        // POST: Foo/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(FooCreateViewModel vm)
        {
            if (ModelState.IsValid)
            {
                db.Foos.Add(vm.Foo);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            vm.FooItemSelectList = new SelectList(db.FooItems, "FooItemId", "ItemValue", vm.Foo.FooItemId);
            return View(vm);
        }

        // GET: Foo/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Foo foo = db.Foos.Find(id);
            if (foo == null)
            {
                return HttpNotFound();
            }
            ViewBag.FooItemId = new SelectList(db.FooItems, "FooItemId", "ItemValue", foo.FooItemId);
            return View(foo);
        }

        // POST: Foo/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "FooId,SingleLineText,MultilineLineText,Number,CheckBox,FooItemId")] Foo foo)
        {
            if (ModelState.IsValid)
            {
                db.Entry(foo).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.FooItemId = new SelectList(db.FooItems, "FooItemId", "ItemValue", foo.FooItemId);
            return View(foo);
        }

        // GET: Foo/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Foo foo = db.Foos.Find(id);
            if (foo == null)
            {
                return HttpNotFound();
            }
            return View(foo);
        }

        // POST: Foo/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Foo foo = db.Foos.Find(id);
            db.Foos.Remove(foo);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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