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 ConversationsController : Controller
    {
        private Entities db = new Entities();

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

        // GET: Conversations/CreateConversationPartial
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public ActionResult CreateConversationPartial(string userIdTo)
        {
            if (userIdTo != null)
            {
                ViewBag.UserIdTo = userIdTo;
            }
            return View();
        }

        // POST: Conversations/CreateConversationPartial
        // 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 CreateConversationPartial([Bind(Include = "Id,UserIdFrom,UserIdTo")] Conversation conversation)
        {
            if (ModelState.IsValid)
            {
                var exist_conversation = (from c in db.Conversations
                                          where c.UserIdTo == conversation.UserIdTo && c.UserIdFrom == conversation.UserIdFrom ||
                                          c.UserIdTo == conversation.UserIdFrom && c.UserIdFrom == conversation.UserIdTo
                                          select c).ToList();
                if (exist_conversation.Count == 0)
                {
                    db.Conversations.Add(conversation);
                    db.SaveChanges();
                }

            }

            ViewBag.UserIdTo = conversation.UserIdTo;
            Response.Redirect(Request.UrlReferrer.ToString());
        }

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

        // GET: Conversations
        //public ActionResult Index()
        //{
        //    var conversations = db.Conversations.Include(c => c.AspNetUserFrom).Include(c => c.AspNetUserTo);
        //    return View(conversations.ToList());
        //}

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

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

        // POST: Conversations/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,UserIdFrom,UserIdTo")] Conversation conversation)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        db.Entry(conversation).State = EntityState.Modified;
        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.UserIdTo = new SelectList(db.AspNetUsers, "Id", "Email", conversation.UserIdTo);
        //    ViewBag.UserIdFrom = new SelectList(db.AspNetUsers, "Id", "Email", conversation.UserIdFrom);
        //    return View(conversation);
        //}

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

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

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