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 Infrastructure.Models;
using Infrastructure.DataAccess;

namespace TestCasePlanner.Controllers
{
    public class TestController : Controller
    {
        private AppDbContext db = new AppDbContext();

        // GET: /Test/
        public async Task<ActionResult> Index()
        {
            var testcases = db.TestCases.Include(t => t.Creator).Include(t => t.Editor).Include(t => t.Project);
            return View(await testcases.ToListAsync());
        }

        // GET: /Test/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TestCase testcase = await db.TestCases.FindAsync(id);
            if (testcase == null)
            {
                return HttpNotFound();
            }
            return View(testcase);
        }

        // GET: /Test/Create
        public ActionResult Create()
        {
            ViewBag.CreatorID = new SelectList(db.Users, "Id", "Email");
            ViewBag.EditorID = new SelectList(db.Users, "Id", "Email");
            ViewBag.ProjectID = new SelectList(db.TestProjects, "ID", "Name");
            return View();
        }

        // POST: /Test/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]
        public async Task<ActionResult> Create([Bind(Include="ID,Title,Description,Prerequsity,CreatorID,EditorID,ProjectID,DateCreated,DateUpdated")] TestCase testcase)
        {
            if (ModelState.IsValid)
            {
                db.TestCases.Add(testcase);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.CreatorID = new SelectList(db.Users, "Id", "Email", testcase.CreatorID);
            ViewBag.EditorID = new SelectList(db.Users, "Id", "Email", testcase.EditorID);
            ViewBag.ProjectID = new SelectList(db.TestProjects, "ID", "Name", testcase.ProjectID);
            return View(testcase);
        }

        // GET: /Test/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TestCase testcase = await db.TestCases.FindAsync(id);
            if (testcase == null)
            {
                return HttpNotFound();
            }
            ViewBag.CreatorID = new SelectList(db.Users, "Id", "Email", testcase.CreatorID);
            ViewBag.EditorID = new SelectList(db.Users, "Id", "Email", testcase.EditorID);
            ViewBag.ProjectID = new SelectList(db.TestProjects, "ID", "Name", testcase.ProjectID);
            return View(testcase);
        }

        // POST: /Test/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]
        public async Task<ActionResult> Edit([Bind(Include="ID,Title,Description,Prerequsity,CreatorID,EditorID,ProjectID,DateCreated,DateUpdated")] TestCase testcase)
        {
            if (ModelState.IsValid)
            {
                db.Entry(testcase).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.CreatorID = new SelectList(db.Users, "Id", "Email", testcase.CreatorID);
            ViewBag.EditorID = new SelectList(db.Users, "Id", "Email", testcase.EditorID);
            ViewBag.ProjectID = new SelectList(db.TestProjects, "ID", "Name", testcase.ProjectID);
            return View(testcase);
        }

        // GET: /Test/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            TestCase testcase = await db.TestCases.FindAsync(id);
            if (testcase == null)
            {
                return HttpNotFound();
            }
            return View(testcase);
        }

        // POST: /Test/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            TestCase testcase = await db.TestCases.FindAsync(id);
            db.TestCases.Remove(testcase);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

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