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 RedSocial.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;

namespace RedSocial.Controllers
{
    public class MessagesController : Controller
    {
        private Entities db = new Entities();

        /******************** USER ********************/

        // GET: Messages/CreateMessagePartial
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public ActionResult CreateMessagePartial(int? conversationId)
        {
            if (conversationId != null)
            {
                ViewBag.ConversationId = conversationId;
            }
            return View();
        }

        // POST: Messages/Create
        // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
        // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public void CreateMessagePartial([Bind(Include = "Id,Text,DateTime,ConversationId,UserId")] Message message)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(message);
                db.SaveChanges();
            }

            ViewBag.ConversationId = message.ConversationId;
            Response.Redirect(Request.UrlReferrer.ToString());
        }

        // GET: Messages/MyMessages
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public ActionResult MyMessages()
        {
            var id = User.Identity.GetUserId();
            AspNetUser user = db.AspNetUsers.Find(id);
            if (user == null)
            {
                return HttpNotFound();
            }
            ViewBag.conversationsTo = user.ConversationsTo;
            ViewBag.conversationsFrom = user.ConversationsFrom;
            ViewBag.messages = new List<Message>();
            ViewBag.friends = user.AspNetUsers;
            return View(ViewBag);
        }

        // GET: Messages/FromConversation/id
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public ActionResult MessagesFromConversation(int? id)
        {
            var conversation = db.Conversations.Find(id);
            if (conversation == null)
            {
                return HttpNotFound();
            }
            var userId = User.Identity.GetUserId();
            AspNetUser user = db.AspNetUsers.Find(userId);
            if (user == null)
            {
                return HttpNotFound();
            }
            ViewBag.conversationsFrom = user.ConversationsFrom;
            ViewBag.conversationsTo = user.ConversationsTo;
            ViewBag.messages = conversation.Messages.OrderByDescending(x => x.DateTime);
            ViewBag.friends = user.AspNetUsers;
            ViewBag.ConversationIdSelected = id;
            return View("MyMessages", ViewBag);
        }

        /******************** ADMIN ********************/

        // GET: Messages
        //public ActionResult Index()
        //{
        //    var messages = db.Messages.Include(m => m.AspNetUser).Include(m => m.Conversation);
        //    return View(messages.ToList());
        //}

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

        // GET: Messages/Edit/5
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Message message = db.Messages.Find(id);
        //    if (message == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", message.UserId);
        //    ViewBag.ConversationId = new SelectList(db.Conversations, "Id", "UserIdFrom", message.ConversationId);
        //    return View(message);
        //}

        // POST: Messages/Edit/5
        // Para protegerse de ataques de publicación excesiva, habilite las propiedades específicas a las que desea enlazarse. Para obtener 
        // más información vea http://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "Id,Text,DateTime,ConversationId,UserId")] Message message)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(message).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", message.UserId);
        //    ViewBag.ConversationId = new SelectList(db.Conversations, "Id", "UserIdFrom", message.ConversationId);
        //    return View(message);
        //}

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

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

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

    }
}
