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

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

        // GET: Comments/CreateCommentPartial
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public ActionResult CreateCommentPartial(int? postId)
        {
            if (postId != null)
            {
                ViewBag.PostId = postId;
            }
            return View();
        }

        // POST: Comments/CreateCommentPartial
        // 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 CreateCommentPartial([Bind(Include = "Id,Text,UserId,PostId,DateTime")] Comment comment, HttpPostedFileBase file, string video)
        {
            if (ModelState.IsValid)
            {
                var c = db.Comments.Add(comment);
                db.SaveChanges();
                if (file != null)
                {
                    Media media = new Media();
                    media.Type = GlobalConstants.TypePhoto;
                    media.Url = Upload.UploadImage(file, GlobalConstants.RouteCommentImages, this.Server);
                    media.CommentId = c.Id;
                    db.Medias.Add(media);
                    db.SaveChanges();
                }
                if (!String.IsNullOrEmpty(video))
                {
                    Media media = new Media();
                    media.Type = GlobalConstants.TypeVideo;
                    media.Url = video;
                    media.CommentId = c.Id;
                    db.Medias.Add(media);
                    db.SaveChanges();
                }
            }
            ViewBag.PostId = comment.PostId;
            Response.Redirect(Request.UrlReferrer.ToString());
        }
       
        // POST: Comments/Delete/5
        [HttpPost, ActionName("DeleteCommentPartial")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = GlobalConstants.RoleUser)]
        public void DeleteCommentPartial(int id)
        {
            Comment comment = db.Comments.Find(id);
            db.Comments.Remove(comment);
            db.SaveChanges();
            Response.Redirect(Request.UrlReferrer.ToString());
        }

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

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

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

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

        // POST: Comments/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,Text,UserId,PostId,DateTime")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Comments.Add(comment);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

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

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

        // POST: Comments/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,Text,UserId,PostId,DateTime")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserId = new SelectList(db.AspNetUsers, "Id", "Email", comment.UserId);
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text", comment.PostId);
            return View(comment);
        }

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

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

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

    }
}
