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

namespace WebMVCDemoApp.Controllers
{
    public class ContactsController : Controller
    {
        // we need all the repos here
        // also, viewmodels!!!
        // no dropdown lists in viewbag

        private readonly IPersonRepository _personRepository;
        private readonly IContactRepository _contactRepository;
        private readonly IContactTypeRepository _contactTypeRepository;

        //ninject will initialize repos for us
        public ContactsController(IContactRepository contactRepository, IContactTypeRepository contactTypeRepository, IPersonRepository personRepository)
        {
            _contactRepository = contactRepository;
            _contactTypeRepository = contactTypeRepository;
            _personRepository = personRepository;
        }

        // GET: Contacts
        public ActionResult Index()
        {
            var contacts = _contactRepository.All.Include(c => c.ContactType).Include(c => c.Person);
            return View(contacts.ToList());
        }

        // GET: Contacts/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Contact contact = _contactRepository.GetById((int) id);
            if (contact == null)
            {
                return HttpNotFound();
            }
            return View(contact);
        }

        // GET: Contacts/Create
        public ActionResult CreateWithViewBag()
        {
            //this is bad, NO VIEWBAG PLEASE!!!
            ViewBag.ContactTypeID = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name");
            ViewBag.PersonID = new SelectList(_personRepository.All, "PersonID", "FirstLastName");
            return View();
        }

        // GET: Contacts/Create
        public ActionResult Create()
        {
            var vm = new ContactViewModel
                     {
                         ContactTypeSelectList = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name"),
                         PersonSelectList = new SelectList(_personRepository.All, "PersonID", "FirstLastName")
                     };
            return View(vm);
        }


        // POST: Contacts/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 CreateWithViewBag([Bind(Include = "ContactID,ContactTypeID,Value,PersonID")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                _contactRepository.Add(contact);
                _contactRepository.Save();
                return RedirectToAction("Index");
            }

            //this is bad, NO VIEWBAG PLEASE!!!
            ViewBag.ContactTypeID = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name", contact.ContactTypeID);
            ViewBag.PersonID = new SelectList(_personRepository.All, "PersonID", "FirstLastName", contact.PersonID);

            return View(contact);
        }

        // POST: Contacts/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(ContactViewModel vm)
        {
            if (ModelState.IsValid)
            {
                _contactRepository.Add(vm.Contact);
                _contactRepository.Save();
                return RedirectToAction("Index");
            }

            vm.ContactTypeSelectList = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name", vm.Contact.ContactTypeID);
            vm.PersonSelectList = new SelectList(_personRepository.All, "PersonID", "FirstLastName", vm.Contact.PersonID);

            return View(vm);
        }

        // GET: Contacts/Edit/5
        public ActionResult EditWithViewBag(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var contact = _contactRepository.GetById((int) id);
            if (contact == null)
            {
                return HttpNotFound();
            }

            //this is bad, NO VIEWBAG PLEASE!!!
            ViewBag.ContactTypeID = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name", contact.ContactTypeID);
            ViewBag.PersonID = new SelectList(_personRepository.All, "PersonID", "FirstLastName", contact.PersonID);

            return View(contact);
        }

        // GET: Contacts/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var contact = _contactRepository.GetById((int)id);
            if (contact == null)
            {
                return HttpNotFound();
            }

            var vm = new ContactViewModel()
                     {
                         Contact = contact,
                         ContactTypeSelectList = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name", contact.ContactTypeID),
                         PersonSelectList = new SelectList(_personRepository.All, "PersonID", "FirstLastName", contact.PersonID)
                     };
 
            return View(vm);
        }


        // POST: Contacts/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 EditWithViewBag([Bind(Include = "ContactID,ContactTypeID,Value,PersonID")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                _contactRepository.Update(contact);
                _contactRepository.Save();
                return RedirectToAction("Index");
            }

            //this is bad, NO VIEWBAG PLEASE!!!
            ViewBag.ContactTypeID = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name", contact.ContactTypeID);
            ViewBag.PersonID = new SelectList(_personRepository.All, "PersonID", "FirstLastName", contact.PersonID);

            return View(contact);
        }

        // POST: Contacts/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(ContactViewModel vm)
        {
            if (ModelState.IsValid)
            {
                _contactRepository.Update(vm.Contact);
                _contactRepository.Save();
                return RedirectToAction("Index");
            }

            vm.ContactTypeSelectList = new SelectList(_contactTypeRepository.All, "ContactTypeID", "Name", vm.Contact.ContactTypeID);
            vm.PersonSelectList = new SelectList(_personRepository.All, "PersonID", "FirstLastName", vm.Contact.PersonID);
            return View(vm);
        }


        // GET: Contacts/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            var contact = _contactRepository.GetById((int) id);
            if (contact == null)
            {
                return HttpNotFound();
            }
            return View(contact);
        }

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

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