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 StudentDAL.Interfaces;
using StudentDAL.Repos;
using StudentModels;
using StudentDAL;

namespace StudentMVC.Controllers
{
    public class PersonController : Controller
    {
        //private StudentContext db = new StudentContext();

        //private readonly PersonRepository _personRepository = new PersonRepository(new StudentContext());

        private readonly IPersonRepository _personRepository;

        public PersonController(IPersonRepository personRepository)
        {
            _personRepository = personRepository;
        }

        // GET: /Person/
        public ActionResult Index()
        {
            return View(_personRepository.All.ToList());
        }

        // GET: /Person/Details/5
        public ActionResult Details(int id)
        {
            Person person = _personRepository.GetById(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }

        // GET: /Person/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: /Person/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([Bind(Include = "PersonID,FirstName,LastName,DOB,IsActive")] Person person)
        {
            if (ModelState.IsValid)
            {
                _personRepository.Add(person);
                _personRepository.Save();
                return RedirectToAction("Index");
            }

            return View(person);
        }

        // GET: /Person/Edit/5
        public ActionResult Edit(int id)
        {
            Person person = _personRepository.GetById(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }

        // POST: /Person/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 = "PersonID,FirstName,LastName,DOB,IsActive")] Person person)
        {
            if (ModelState.IsValid)
            {
                _personRepository.Update(person);
                _personRepository.Save();
                //db.Entry(person).State = EntityState.Modified;
                //db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(person);
        }

        // GET: /Person/Delete/5
        public ActionResult Delete(int id)
        {
            Person person = _personRepository.GetById(id);
            if (person == null)
            {
                return HttpNotFound();
            }
            return View(person);
        }

        // POST: /Person/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            _personRepository.Delete(id);
            _personRepository.Save();
            return RedirectToAction("Index");
        }

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