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;
using FotbalPortal.Filters;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web.Helpers;

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

        //
        // GET: /Article/

        public ActionResult Index()
        {
            var articles = db.Articles.Include(a => a.category).OrderByDescending(a => a.created);
            return View(articles.ToList());
        }

        //
        // GET: /Article/Home

        public ActionResult Home()
        {
            var articles = db.Articles.Include(a => a.category)
                    .Where(a => a.status == 1)
                    .Where(a => a.published < DateTime.Now)
                    .OrderByDescending(a => a.published);
            return View(articles.ToList());
        }

        //
        // GET: /Article/Approve
        [Authorize(Roles = "admin, sefredaktor")]
        public ActionResult Approve()
        {
            var articles = db.Articles.Include(a => a.category)
                    .Where(a => a.status == 0);
            return View(articles.ToList());
        }

        //
        // GET: /Article/ApproveSet/5
        [Authorize(Roles = "admin, sefredaktor")]
        public ActionResult ApproveSet(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // POST: /Article/ApproveSet/5

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

        //
        // GET: /Article/Category/5
        public ActionResult Category(int id = 0)
        {

            ViewBag.categoryID = id;

            var articles = db.Articles.Include(a => a.category)
                   .Where(a => a.categoryID == id)
                   .Where(a => a.status == 1)
                   .Where(a => a.published < DateTime.Now)
                   .OrderByDescending(a => a.published);
            return View(articles.ToList());
        }

        //
        // GET: /Article/Details/5

        public ActionResult Details(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // GET: /Article/Read/5

        public ActionResult Read(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }

            var comments = db.Comments.Where(a => a.articleID == id).Where(a => a.disabled  == false);
            ViewData["comments"] = comments.ToList();

            return View(article);
        }

        //
        // GET: /Article/Create
        [Authorize(Roles = "admin, redaktor, sefredaktor")]
        public ActionResult Create()
        {
            ViewBag.categoryID = new SelectList(db.Categories, "id", "name");
            return View();
        }

        //
        // POST: /Article/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Article article, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Images/")
                                                          + file.FileName);
                    article.image = file.FileName;
                }
                article.author = (int)WebMatrix.WebData.WebSecurity.CurrentUserId;
                article.created = DateTime.Now;
                article.edited = DateTime.Now;
                article.status = 0;
                db.Articles.Add(article);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.categoryID = new SelectList(db.Categories, "id", "name", article.categoryID);
            return View(article);
        }

        //
        // GET: /Article/Edit/5
        [Authorize(Roles = "admin, redaktor, sefredaktor")]
        public ActionResult Edit(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            ViewBag.categoryID = new SelectList(db.Categories, "id", "name", article.categoryID);
            return View(article);
        }

        //
        // POST: /Article/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Article article, HttpPostedFileBase file)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    file.SaveAs(HttpContext.Server.MapPath("~/Images/")
                                                          + file.FileName);
                    article.image = file.FileName;
                }
                article.edited = DateTime.Now;
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.categoryID = new SelectList(db.Categories, "id", "name", article.categoryID);
            return View(article);
        }

        //
        // GET: /Article/Delete/5
        [Authorize(Roles = "admin, redaktor, sefredaktor")]
        public ActionResult Delete(int id = 0)
        {
            Article article = db.Articles.Find(id);
            if (article == null)
            {
                return HttpNotFound();
            }
            return View(article);
        }

        //
        // POST: /Article/Delete/5

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

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

        public void ImageResize(int width, int height, string filename)
        {
            var imageFile = Path.Combine(Server.MapPath("~/Images"), filename);
            WebImage image = new WebImage(imageFile).Resize(width, height).Write();
            

            
        }
    }
}