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 System.Web.UI.WebControls;

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

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

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

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

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

        // GET: Medias/Create
        [Authorize(Roles = GlobalConstants.RoleAdmin)]
        public ActionResult Create()
        {
            ViewBag.Type = new SelectList(new List<ListItem>
        {
              new ListItem { Text = GlobalConstants.TypePhoto, Value = GlobalConstants.TypePhoto },
              new ListItem { Text = GlobalConstants.TypeVideo, Value = GlobalConstants.TypeVideo }
        }, "Value", "Text");
            ViewBag.CommentId = new SelectList(db.Comments, "Id", "Text");
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text");
            return View();
        }

        // POST: Medias/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,Url,Type,PostId,CommentId")] Media media)
        {
            if (ModelState.IsValid)
            {
                db.Medias.Add(media);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

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

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

        // POST: Medias/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,Url,Type,PostId,CommentId")] Media media)
        {
            if (ModelState.IsValid)
            {
                db.Entry(media).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CommentId = new SelectList(db.Comments, "Id", "Text", media.CommentId);
            ViewBag.PostId = new SelectList(db.Posts, "Id", "Text", media.PostId);
            return View(media);
        }

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

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

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