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

        //
        // GET: /Category/
        [Authorize(Roles = "admin")]
        public ActionResult Index()
        {
            var categories = db.Categories.Include(c => c.parent);
            return View(categories.ToList());
        }

        //
        // GET: /Category/Menu/

        public ActionResult Menu()
        {
            var categories = db.Categories.Include(c => c.parent);
            return PartialView(categories.ToList());
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(int id = 0)
        {
            Category category = db.Categories.Find(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

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

        //
        // POST: /Category/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

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

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

        //
        // POST: /Category/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Entry(category).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.parentID = new SelectList(db.Categories, "id", "name", category.parentID);
            return View(category);
        }

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

        //
        // POST: /Category/Delete/5

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

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