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

        //
        // GET: /Opinion/

        public ActionResult Index()
        {
            return View(db.Opinions.ToList());
        }

        //
        // GET: /Opinion/Timeline

        public ActionResult Timeline()
        {
            return View(db.Opinions.ToList());
        }

        //
        // GET: /Opinion/Details/5

        public ActionResult Details(int id = 0)
        {
            Opinion opinion = db.Opinions.Find(id);
            if (opinion == null)
            {
                return HttpNotFound();
            }
            return View(opinion);
        }

        //
        // GET: /Opinion/Create
        [Authorize(Roles = "admin, authorized, editor, redaktor, sefredaktor")]
        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Opinion/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Opinion opinion)
        {
            if (ModelState.IsValid)
            {
                opinion.author = (int)WebMatrix.WebData.WebSecurity.CurrentUserId;
                opinion.status = 1;
                opinion.created = DateTime.Now;

                db.Opinions.Add(opinion);
                db.SaveChanges();
                return RedirectToAction("Timeline");
            }

            return View(opinion);
        }

        //
        // GET: /Opinion/Edit/5
        [Authorize(Roles = "admin, editor")]
        public ActionResult Edit(int id = 0)
        {
            Opinion opinion = db.Opinions.Find(id);
            if (opinion == null)
            {
                return HttpNotFound();
            }
            return View(opinion);
        }

        //
        // POST: /Opinion/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Opinion opinion)
        {
            if (ModelState.IsValid)
            {
                db.Entry(opinion).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(opinion);
        }

        //
        // GET: /Opinion/Delete/5
        [Authorize(Roles = "admin, editor")]
        public ActionResult Delete(int id = 0)
        {
            Opinion opinion = db.Opinions.Find(id);
            if (opinion == null)
            {
                return HttpNotFound();
            }
            return View(opinion);
        }

        //
        // POST: /Opinion/Delete/5

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

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