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;

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

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

        // GET: Likes/Create
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public ActionResult CreateLikePartial(int? postId, int? commentId)
        {
            if (postId != null)
            {
                ViewBag.PostId = postId;
            }
            if (commentId != null)
            {
                ViewBag.CommentId = commentId;
            }
            return View();
        }

        // POST: Likes/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 CreateLikePartial([Bind(Include = "Id,DateTime,UserId,PostId,CommentId")] Like like)
        {
            if (ModelState.IsValid)
            {
                db.Likes.Add(like);
                db.SaveChanges();
            }

            ViewBag.CommentId = like.CommentId;
            ViewBag.PostId = like.PostId;
            Response.Redirect(Request.UrlReferrer.ToString());
        }

        // POST: Likes/Delete/5
        [HttpPost, ActionName("DeleteLikePartial")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public void DeleteLikePartial(int id)
        {
            Like like = db.Likes.Find(id);
            db.Likes.Remove(like);
            db.SaveChanges();
            Response.Redirect(Request.UrlReferrer.ToString());
        }

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

        // GET: Likes
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult Index()
        {
            var likes = db.Likes.Include(l => l.AspNetUser).Include(l => l.Comment).Include(l => l.Post);
            return View(likes.ToList());
        }

        // GET: Likes/Details/5
        //[Authorize(Roles = GlobalConstants.RoleAdmin)]
        //public ActionResult Details(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Like like = db.Likes.Find(id);
        //    if (like == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    return View(like);
        //}

        // GET: Likes/Create
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult Create()
        {
            ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email");
            ViewBag.CommentId = new SelectList(db.Comments, "Id", "Text");
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text");
            return View();
        }

        // POST: Likes/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.RoleAdmin)]
        public ActionResult Create([Bind(Include = "Id,DateTime,UserId,PostId,CommentId")] Like like)
        {
            if (ModelState.IsValid)
            {
                db.Likes.Add(like);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", like.UserId);
            ViewBag.CommentId = new SelectList(db.Comments, "Id", "Text", like.CommentId);
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text", like.PostId);
            return View(like);
        }

        // GET: Likes/Edit/5
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Like like = db.Likes.Find(id);
            if (like == null)
            {
                return HttpNotFound();
            }
            ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", like.UserId);
            ViewBag.CommentId = new SelectList(db.Comments, "Id", "Text", like.CommentId);
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text", like.PostId);
            return View(like);
        }

        // POST: Likes/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]
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult Edit([Bind(Include = "Id,DateTime,UserId,PostId,CommentId")] Like like)
        {
            if (ModelState.IsValid)
            {
                db.Entry(like).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", like.UserId);
            ViewBag.CommentId = new SelectList(db.Comments, "Id", "Text", like.CommentId);
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text", like.PostId);
            return View(like);
        }

        // GET: Likes/Delete/5
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Like like = db.Likes.Find(id);
            if (like == null)
            {
                return HttpNotFound();
            }
            return View(like);
        }

        // POST: Likes/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult DeleteConfirmed(int id)
        {
            Like like = db.Likes.Find(id);
            db.Likes.Remove(like);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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