﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Rantory.Models;
using PagedList;

namespace Rantory.Controllers
{
    public class PositionsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        // GET: Positions
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> Index(string SortOrder, string Name, string NameShow, int? Page)
        {
            var positions = db.Positions
                .Where(p => true);

            ViewBag.NameFilter = Name;
            ViewBag.NameShowFilter = NameShow;

            ViewBag.NameSort = SortOrder == "Name" ? "NameDesc" : "Name";
            ViewBag.NameShowSort = SortOrder == "NameShow" ? "NameShowDesc" : "NameShow";

            if (!String.IsNullOrEmpty(Name))
            {
                positions = positions.Where(p => p.Name.Contains(Name));
            }
            if (!String.IsNullOrEmpty(NameShow))
            {
                positions = positions
                    .ToList()
                    .Where(p => p.NameShow.Contains(NameShow))
                    .AsQueryable();
            }

            switch (SortOrder)
            {
                case "Name":
                    positions = positions.OrderBy(p => p.Name);
                    break;
                case "NameDesc":
                    positions = positions.OrderByDescending(p => p.Name);
                    break;
                case "NameShow":
                    positions = positions
                        .ToList()
                        .OrderBy(p => p.NameShow)
                        .AsQueryable();
                    break;
                case "NameShowDesc":
                    positions = positions
                        .ToList()
                        .OrderByDescending(p => p.NameShow)
                        .AsQueryable();
                    break;
                default:
                    positions = positions.OrderBy(p => p.Id);
                    break;
            }

            int PageSize = 20;
            int PageNumber = (Page ?? 1);

            return View(positions.ToPagedList(PageNumber, PageSize));
        }

        // GET: Positions/Details/5
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Position position = await db.Positions.FindAsync(id);
            if (position == null)
            {
                return HttpNotFound();
            }
            return View(position);
        }

        // GET: Positions/Create
        [Authorize(Roles = "Admin")]
        public ActionResult Create()
        {
            return View();
        }

        // POST: Positions/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> Create([Bind(Include = "Id,Name")] Position position)
        {
            if (ModelState.IsValid)
            {
                db.Positions.Add(position);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            return View(position);
        }

        // GET: Positions/Edit/5
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Position position = await db.Positions.FindAsync(id);
            if (position == null)
            {
                return HttpNotFound();
            }
            return View(position);
        }

        // POST: Positions/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> Edit([Bind(Include = "Id,Name")] Position position)
        {
            if (ModelState.IsValid)
            {
                db.Entry(position).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            return View(position);
        }

        // GET: Positions/Delete/5
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Position position = await db.Positions.FindAsync(id);
            if (position == null)
            {
                return HttpNotFound();
            }
            return View(position);
        }

        // POST: Positions/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        [Authorize(Roles = "Admin")]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            Position position = await db.Positions.FindAsync(id);
            db.Positions.Remove(position);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

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