using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Web;
using System.Web.Mvc;
using WillItScale.Models;

namespace WillItScale.Controllers
{
    public class IdeaController : Controller
    {
        private WillItScaleContext db = new WillItScaleContext();

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

        public ActionResult Index()
        {
            ViewBag.Message = "Will it Scale?";
            ViewBag.Title = "Find out if your amazing idea will scale! Will it Scale?";
            return View();
        }

        [HttpPost]
        public ActionResult Index(Idea idea)
        {
            if (ModelState.IsValid)
            {
                idea.CreatedAt = DateTime.Now;
                idea.Content = idea.Content;
                db.Ideas.Add(idea);
                db.SaveChanges();
                var slug = HttpUtility.UrlEncodeUnicode(idea.Content);
                return RedirectToAction("Answer", new { q = slug });
            }

            return View(idea);
        }

        //
        // GET: /Idea/

        public ViewResult PlainList()
        {
            return View(db.Ideas.OrderByDescending(idea => idea.CreatedAt).Take(10).ToList());
        }

        public ViewResult List()
        {
            return View();
        }

        public JsonResult MegaListRows()
        {
            //paging
            string sPageSize = Request["paging[pageSize]"];
            int pageSize = -1;
            if (!string.IsNullOrEmpty(sPageSize))
            {
                pageSize = int.Parse(sPageSize);
            }
            string sPageIndex = Request["paging[pageIndex]"];
            int pageIndex = -1;
            if (!string.IsNullOrEmpty(sPageIndex))
            {
                pageIndex = int.Parse(sPageIndex);
            }

            //sorting
            var sort = "";
            var sortDir = "";
            if (Request["sorting[0][dataKey]"] != null && Request["sorting[0][dataKey]"] != "")
            {
                sort = Request["sorting[0][dataKey]"];
                if (Request["sorting[0][sortDirection]"] != null && Request["sorting[0][sortDirection]"] != "")
                {
                    sortDir = Request["sorting[0][sortDirection]"];
                    if (sort == "Idea")
                    {
                        sort = "Content";
                        if (sortDir.ToLower() == "ascending")
                        {
                            sort = "it." + sort + " asc";
                        }
                        else if (sortDir.ToLower() == "descending")
                        {
                            sort = "it." + sort + " desc";
                        }
                    }
                    else if (sort == "Scalable")
                    {
                        sort = "";
                    }
                }
            }
            IEnumerable<Idea> ideas;
            if (sort != "")
            {
                ideas = db.Ideas.OrderBy(sort).Skip(pageIndex * pageSize).Take(pageSize);
            }
            else
            {
                ideas = db.Ideas.OrderBy("it.CreatedAt desc").Skip(pageIndex * pageSize).Take(pageSize);
            }
            int totalCount = db.Ideas.Count();


            var result = new { TotalRowCount = totalCount, Items = ideas };

            return Json(result, JsonRequestBehavior.AllowGet);
        }
        //
        // GET: /Idea/Details/5

        public ViewResult Details(int id)
        {
            Idea idea = db.Ideas.Find(id);
            return View(idea);
        }

        public ViewResult Answer(string q)
        {
            var slug = HttpUtility.UrlDecode(q);
            Idea idea = db.Ideas.Where(_idea => _idea.Content == slug).First();
            return View(idea);
        }
        //
        // GET: /Idea/Create

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

        //
        // POST: /Idea/Create

        [HttpPost]
        public ActionResult Create(Idea idea)
        {
            if (ModelState.IsValid)
            {
                idea.CreatedAt = DateTime.Now;
                idea.Content = HttpUtility.HtmlEncode(idea.Content);
                db.Ideas.Add(idea);
                db.SaveChanges();
                return RedirectToAction("Answer", idea.Content);
            }

            return View(idea);
        }

        ////
        //// GET: /Idea/Edit/5

        //public ActionResult Edit(int id)
        //{
        //    Idea idea = db.Ideas.Find(id);
        //    return View(idea);
        //}

        ////
        //// POST: /Idea/Edit/5

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

        //
        // GET: /Idea/Delete/5

        public ActionResult Delete(int id)
        {
            Idea idea = db.Ideas.Find(id);
            return View(idea);
        }

        //
        // POST: /Idea/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Idea idea = db.Ideas.Find(id);
            db.Ideas.Remove(idea);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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

    }
}
