using Microsoft.AspNetCore.Mvc;

namespace HallOfFame.Controllers
{
    public class PersonController : Controller
    {
        private readonly PersonDBContext _db;

        public PersonController(PersonDBContext db)
        {
            _db = db;
        }

        public IActionResult Index()
        {
           IEnumerable<Person> objPersonList = _db.Persons;
           return View(objPersonList);
        }

        public IActionResult ViewSkill()
        {
            IEnumerable<Skill> objPersonList = _db.Skills;
            return View(objPersonList);
        }

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

        //POST
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(Person obj)
        {
            _db.Persons.Add(obj);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public IActionResult Edit(long? id)
        {
            if (id==null || id == 0)
            {
                return NotFound();
            }
            var person = _db.Persons.Find(id);

            if (person == null)
            {
                return NotFound();
            }
            return View(person);
        }

        public IActionResult Result(long? id)
        {
            if (id == null || id == 0)
            {
                return NotFound();
            }
            var con = _db.ConPersonSkills.Where(x=>x.PersonId == id).ToList();

            if (con == null)
            {
                return NotFound();
            }
            ViewBag.data = con;
            return View(con);
        }

        //POST
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(Person obj)
        {
            _db.Persons.Update(obj);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}
