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 DALWebApi.Interfaces;
using Domain.Contacts;
using Microsoft.AspNet.Identity;

namespace WebAppNoEF.Controllers
{
    [Authorize]
    public class ContactController : Controller
    {
        //private WebAppEFContext db = new WebAppEFContext();
        private readonly IWebApiUOW _uow;

        public ContactController(IWebApiUOW uow)
        {
            _uow = uow;
        }


        // GET: Contact
        public ActionResult Index()
        {
            //var contacts = db.Contacts.Include(c => c.ContactType).Include(c => c.Person);
            var contacts = _uow.Contacts.AllForUser(User.Identity.GetUserId());
            return View(contacts);
        }

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

        // GET: Contact/Create
        public ActionResult Create()
        {
            ViewBag.ContactTypeId = new SelectList(_uow.ContactTypes.All, "ContactTypeId", "ContactTypeValue");
            ViewBag.PersonId = new SelectList(_uow.Persons.AllForUser(User.Identity.GetUserId()), "PersonId", "FirstLastName");
            return View();
        }

        // POST: Contact/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 = "ContactId,ContactValue,PersonId,ContactTypeId")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                _uow.Contacts.Add(contact);
                _uow.Commit();
                return RedirectToAction("Index");
            }

            ViewBag.ContactTypeId = new SelectList(_uow.ContactTypes.All, "ContactTypeId", "ContactTypeValue", contact.ContactTypeId);
            ViewBag.PersonId = new SelectList(_uow.Persons.AllForUser(User.Identity.GetUserId()), "PersonId", "FirstLastName", contact.PersonId);
            return View(contact);
        }

        // GET: Contact/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Contact contact = _uow.Contacts.GetById(id);
            if (contact == null)
            {
                return HttpNotFound();
            }
            ViewBag.ContactTypeId = new SelectList(_uow.ContactTypes.All, "ContactTypeId", "ContactTypeValue", contact.ContactTypeId);
            ViewBag.PersonId = new SelectList(_uow.Persons.AllForUser(User.Identity.GetUserId()), "PersonId", "FirstLastName", contact.PersonId);
            return View(contact);
        }

        // POST: Contact/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 = "ContactId,ContactValue,PersonId,ContactTypeId")] Contact contact)
        {
            if (ModelState.IsValid)
            {
                _uow.Contacts.Update(contact.ContactId,contact);
                _uow.Commit();
                return RedirectToAction("Index");
            }
            ViewBag.ContactTypeId = new SelectList(_uow.ContactTypes.All, "ContactTypeId", "ContactTypeValue", contact.ContactTypeId);
            ViewBag.PersonId = new SelectList(_uow.Persons.AllForUser(User.Identity.GetUserId()), "PersonId", "FirstLastName", contact.PersonId);
            return View(contact);
        }

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

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

    }
}
