//
 // GET: /Players/Delete/5
 /// <summary>
 /// Delete view controller method for deleting a player
 /// </summary>
 /// <param name="id">The player id</param>
 /// <returns>The Delete view</returns>
 public ActionResult Delete(int id)
 {
     Player player;
     using (var context = new FootballManagerDBContext())
     {
         player =  context.Players.Find(id);
     }
     return View(player);
 }
 //
 // GET: /Teams/Delete/5
 /// <summary>
 /// Sends to the view layer the data of the team to be deleted
 /// </summary>
 /// <param name="id">The team selected id</param>        
 /// <returns>The view</returns>
 public ActionResult Delete(int id)
 {
     Team team;
     using (var context = new FootballManagerDBContext())
     {
         team = context.Teams.Find(id);
     }
     return View(team);
 }
 //
 // GET: /Matches/Delete/5
 /// <summary>
 /// Sends the match details to the deelete view 
 /// </summary>
 /// <param name="id">The selected match id</param>        
 /// <returns>The view</returns>
 public ActionResult Delete(int id)
 {
     Match match;
     using (var context = new FootballManagerDBContext())
     {
         match = context.Matches.Find(id);
     }
     return View(match);
 }
 public ActionResult DeleteConfirmed(int id)
 {
     using (var context = new FootballManagerDBContext())
     {
         Team team = context.Teams.Find(id);
         context.Teams.Remove(team);
         context.SaveChanges();
     }
     return RedirectToAction("Index");
 }
 public ActionResult DeleteConfirmed(int id)
 {
     Player player;
     using (var context = new FootballManagerDBContext())
     {
         player = context.Players.Find(id);
         context.Players.Remove(player);
         context.SaveChanges();
     }
     return RedirectToAction("IndexList");
 }
        public ActionResult Create(Team team)
        {
            if (ModelState.IsValid)
            {
                using (var context = new FootballManagerDBContext())
                {
                    context.Teams.Add(team);
                    context.SaveChanges();
                }
                return RedirectToAction("Index");
            }

            return View(team);
        }
        public ActionResult Create(Player player, String teamList)
        {
            var teamId = Convert.ToInt32(teamList);
            if (ModelState.IsValid)
            {
                using (var context = new FootballManagerDBContext())
                {
                    Team team = context.Teams.Single(selectedTeam => selectedTeam.ID == teamId);
                    player.Team = team;
                    player.TeamId = team.ID;
                    context.Players.Add(player);
                    context.SaveChanges();
                }
                return RedirectToAction("IndexList");
            }

            return View(player);
        }
        public ActionResult Create(Match match, string teamListHome, string teamListAway)
        {
            var teamHomeId = Convert.ToInt32(teamListHome);
            var teamAwayId = Convert.ToInt32(teamListAway);
            if (ModelState.IsValid)
            {
                using (var context = new FootballManagerDBContext())
                {
                    Team teamHome = context.Teams.Single(d => d.ID == teamHomeId);
                    Team teamAway = context.Teams.Single(d => d.ID == teamAwayId);
                    match.HomeTeamId = teamHomeId;
                    match.AwayTeamId = teamAwayId;
                    match.HomeTeamName = teamHome.Name;
                    match.AwayTeamName = teamAway.Name;
                    context.Matches.Add(match);
                    context.SaveChanges();
                }

                return RedirectToAction("Index");
            }

            return View(match);
        }
 /// <summary>
 /// Generates a list for the team select filter
 /// </summary>
 private void GenerateTeamSelect()
 {
     using (var context = new FootballManagerDBContext())
     {
         var teams = context.Teams.ToList();
         var teamSelectListItems = teams.Select(team => new SelectListItem() { Text = team.Name, Value = "" + team.ID }).ToList();
         ViewBag.teamListHome = teamSelectListItems;
         ViewBag.teamListAway = teamSelectListItems;
     }
 }
        public ActionResult DeleteConfirmed(int id)
        {
            using (var context = new FootballManagerDBContext())
            {
                Match match = context.Matches.Find(id);
                context.Matches.Remove(match);
                context.SaveChanges();
            }

            return RedirectToAction("Index");
        }
        /// <summary>
        /// Generates a list for the team select filter. If the Player is about to be created, the list populates itself with no order. If 
        /// the player is about to be edited then the select has the actual team already selected
        /// </summary>
        /// <param name="idTeam">The ID of the team to be edited.</param>
        private void GenerateTeamSelect(int idTeam = 0)
        {
            using (var context = new FootballManagerDBContext())
            {
                var teams = context.Teams.ToList();
                var teamSelectListItem = new List<SelectListItem>();
                if (idTeam == 0)
                {
                    teamSelectListItem = teams.Select(team => new SelectListItem() { Text = team.Name, Value = "" + team.ID }).ToList();
                }
                else
                {
                    // If idTeam is passed as an argument it means that the user is editing a player.
                    // In that case the dropdown should select by default the player's team.

                    var teamShorterList = new List<Team>();
                    // We add the player's team firt
                    Team team = teams.Single(selectedTeam => selectedTeam.ID == idTeam);
                    teamShorterList.Add(team);

                    // Then we populate the list with the rest of the teams
                    teamShorterList.AddRange(teams.Where(teamListItem => teamListItem.ID != idTeam).ToList());

                    teamSelectListItem = teamShorterList.Select(selectedTeam => new SelectListItem() { Text = selectedTeam.Name, Value = "" + selectedTeam.ID }).ToList();
                }
                ViewBag.teamList = teamSelectListItem;
            }
        }
 /// <summary>
 /// Method in charge of disposing the db context
 /// </summary>
 /// <param name="disposing">indicates if the context should be disposed</param>
 protected override void Dispose(bool disposing)
 {
     using (var context = new FootballManagerDBContext())
     {
         context.Dispose();
     }
     base.Dispose(disposing);
 }
        /// <summary>
        /// Filters the list of players
        /// </summary>
        /// <param name="teamList">The team selected id</param>
        /// <param name="searchString">The string to use as filter</param>
        /// <returns>The view</returns>
        public ActionResult IndexList(String teamList, String searchString)
        {
            GenerateTeamSelect();

            List<Player> playersList;

            using (var context = new FootballManagerDBContext())
            {

                var players = from p in context.Players.Include(p => p.Team) select p;

                if (!String.IsNullOrEmpty(teamList))
                {
                    var teamId = Int32.Parse(teamList);
                    players = players.Where(player => player.TeamId == teamId);
                }
                if (!String.IsNullOrEmpty(searchString))
                {
                    players = players.Where(player => player.Name.Contains(searchString));
                }

                playersList = players.OrderByDescending(player => player.Rating).ToList();

                return View(playersList);
            }
        }
 //
 // GET: /Players/Edit/5
 /// <summary>
 /// Edit view controller method for editing a player
 /// </summary>
 /// <param name="id">The player id</param>
 /// <returns>The Edit view</returns>
 public ActionResult Edit(int id)
 {
     Player player;
     using (var context = new FootballManagerDBContext())
     {
         player = context.Players.Find(id);
     }
     GenerateTeamSelect(player.TeamId);
     return View(player);
 }
        //
        // GET: /Teams/Details/5
        /// <summary>
        /// Sends to the view layer the data of the team to be edited
        /// </summary>
        /// <param name="id">The team selected id</param>
        /// <returns>The view</returns>
        public ViewResult Details(int id)
        {
            Team team;
            using (var context = new FootballManagerDBContext())
            {

                team = (from t in context.Teams.Include(t => t.Players)
                       where t.ID.Equals(id)
                       select t).FirstOrDefault();
            }
            return View(team);
        }
 public ActionResult Edit(Team team)
 {
     if (ModelState.IsValid)
     {
         using (var context = new FootballManagerDBContext())
         {
             context.Entry(team).State = EntityState.Modified;
             context.SaveChanges();
         }
         return RedirectToAction("Index");
     }
     return View(team);
 }
 //
 // GET: /Teams/
 /// <summary>
 /// Sends to the view the list of teams
 /// </summary>
 /// <returns>The view</returns>
 public ViewResult Index()
 {
     using (var context = new FootballManagerDBContext())
     {
         return View(context.Teams.ToList());
     }
 }