using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using PersonSport.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Authorization;

namespace PersonSport.Controllers {

    [Authorize]
    public class PersonSportController : Controller {
        public IActionResult Index(int? sportens_id = 1) {
            var db = new IdrottContext();

            ViewBag.AllaSporter = db.Sporter.ToList();

            ViewBag.sportens_id = sportens_id;
            ViewBag.SportNamn = db.Sporter
                    .Where(a => a.SportId == sportens_id)
                    .Select(a => a.SportNamn).Single();

            ViewBag.PersonerIValdSport = (from pers in db.Personer
                                          where pers.PersonSporter.Any(c => c.SportId == sportens_id)
                                          select pers).ToList();

            return View();
        }

        [HttpGet]
        public IActionResult Enroll() {
            List<Person> personer = new();
            List<Sport> sporter = new();

            using (var db = new IdrottContext()) {
                personer = db.Personer.ToList();
            }

            using (var db = new IdrottContext()) {
                sporter = db.Sporter.ToList();
            }

            ViewBag.personer = personer;
            ViewBag.sporter = sporter;
            return View();
        }

        [HttpPost]
        public IActionResult Enroll(IFormCollection fc) {
            try {
                int valtNamn = int.Parse(fc["Namn"]);
                int valdSport = int.Parse(fc["Sport"]);

                using (var db = new IdrottContext()) {
                    PersonSport.Models.PersonSport ps = new() { PersonId = valtNamn, SportId = valdSport };
                    db.Add(ps);
                    db.SaveChanges();
                }
                TempData["MeddelandeSuccess"] = "Medlemmen är tillagd i sporten.";
                
                return RedirectToAction(nameof(Index));
            }
            catch (Exception) {
                TempData["MeddelandeFail"] = "Kunde inte lägga till medlemmen i sporten.";
                return View("/Start/Error");
                throw;
            }
        }
        public IActionResult Deroll() {
            List<Person> personer = new();

            using (var db = new IdrottContext()) {
                personer = db.Personer.ToList();
            }

            return View(personer);
        }

        [HttpPost]
        public IActionResult DerollSport(IFormCollection fc) {
            try {
                int valdPersonId = int.Parse(fc["PersonId"]);

                using (var db = new IdrottContext()) {
                    ViewBag.SportForValdPerson = (from sport in db.Sporter
                                                  where sport.PersonSporter.Any(c => c.PersonId == valdPersonId)
                                                  select sport).ToList();
                } 
                ViewBag.PersonId = valdPersonId;
                return View();
            }
            catch (FormatException) {
                
                return RedirectToAction(nameof(Index));
                throw;
            }
        }

        [HttpPost]
        public IActionResult DerollDone(IFormCollection fc) {
            try {
                int valtPersonId = int.Parse(fc["PersonId"]);
                int valtSportId = int.Parse(fc["SportId"]);

                using (var db = new IdrottContext()) {
                    PersonSport.Models.PersonSport ps = new() { PersonId = valtPersonId, SportId = valtSportId };
                    db.Remove(ps);
                    db.SaveChanges();
                }
                TempData["MeddelandeSuccess"] = "Medlemmen är borttagen från sporten.";
                return RedirectToAction(nameof(Index));
            }
            catch (FormatException) {
                TempData["MeddelandeFail"] = "Kunde inte ta bort medlemmen från sporten";
                return RedirectToAction(nameof(Index));
            }
        }
    }
}
