using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FotbalPortal.Models;
using FotbalPortal.DAL;

namespace FotbalPortal.Controllers
{
    public class CommentController : Controller
    {
        private PortalContext db = new PortalContext();

        //
        // GET: /Comment/
        [Authorize(Roles = "admin")]
        public ActionResult Index()
        {
            var comments = db.Comments.Include(c => c.article).Include(c => c.reactOn);
            return View(comments.ToList());
        }

        //
        // GET: /Comment/Details/5

        public ActionResult Details(int id = 0)
        {
            Comment comment = db.Comments.Find(id);
            if (comment == null)
            {
                return HttpNotFound();
            }
            return View(comment);
        }

        //
        // GET: /Comment/Create/1
        //[Authorize(Roles = "admin, authorized, editor, redaktor, sefredaktor")]
        public ActionResult Create(int articleID)
        {
            ViewBag.articleID = articleID;
            //ViewBag.reactOnID = new SelectList(db.Comments, "id", "text");
            return View();
        }

        //
        // POST: /Comment/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.author = (int)WebMatrix.WebData.WebSecurity.CurrentUserId;
                comment.disabled = false;
                comment.created = DateTime.Now;
                db.Comments.Add(comment);
                db.SaveChanges();
            }
            ViewBag.articleID = comment.articleID;
            return View(comment);
        }

        //
        // GET: /Comment/Edit/5
        [Authorize(Roles = "admin")]
        public ActionResult Edit(int id = 0)
        {
            Comment comment = db.Comments.Find(id);
            if (comment == null)
            {
                return HttpNotFound();
            }
            ViewBag.articleID = new SelectList(db.Articles, "id", "title", comment.articleID);
            ViewBag.reactOnID = new SelectList(db.Comments, "id", "text", comment.reactOnID);
            return View(comment);
        }

        //
        // POST: /Comment/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.articleID = new SelectList(db.Articles, "id", "title", comment.articleID);
            ViewBag.reactOnID = new SelectList(db.Comments, "id", "text", comment.reactOnID);
            return View(comment);
        }

        //
        // GET: /Comment/Delete/5
        [Authorize(Roles = "admin")]
        public ActionResult Delete(int id = 0)
        {
            Comment comment = db.Comments.Find(id);
            if (comment == null)
            {
                return HttpNotFound();
            }
            return View(comment);
        }

        //
        // POST: /Comment/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Comment comment = db.Comments.Find(id);
            db.Comments.Remove(comment);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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