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

        //
        // GET: /Video/

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

        //
        // GET: /Video/Page

        public ActionResult Page()
        {
            return View(db.Videos.Where(v => v.disabled == false).OrderByDescending(v => v.created).ToList());
        }

        //
        // GET: /Video/Approve
        [Authorize(Roles = "admin, editor")]
        public ActionResult Approve()
        {
            return View(db.Videos.Where(v => v.disabled == true).ToList());
        }

        //
        // GET: /Video/ApproveSet/5
        [Authorize(Roles = "admin, editor")]
        public ActionResult ApproveSet(int id = 0)
        {
            Video video = db.Videos.Find(id);
            if (video == null)
            {
                return HttpNotFound();
            }
            return View(video);
        }

        //
        // POST: /Video/ApproveSet/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ApproveSet(Video video)
        {
            if (ModelState.IsValid)
            {
                db.Entry(video).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Approve");
            }
            return View(video);
        }

        //
        // GET: /Video/Details/5

        public ActionResult Details(int id = 0)
        {
            Video video = db.Videos.Find(id);
            if (video == null)
            {
                return HttpNotFound();
            }
            return View(video);
        }

        //
        // GET: /Video/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Video/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Video video)
        {
            if (ModelState.IsValid)
            {
                video.author = (int)WebMatrix.WebData.WebSecurity.CurrentUserId;
                video.created = DateTime.Now;
                video.disabled = true;
                db.Videos.Add(video);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(video);
        }

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

        //
        // POST: /Video/Edit/5

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

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

        //
        // POST: /Video/Delete/5

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

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