//
        // GET: /Player/
        public ActionResult Index()
        {
            try
            {
                if (Session["UserAccountID"] == null)
                    return RedirectToAction("Validate", "Login");
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                    ViewData["txtIsAdmin"] = "true";
                else
                    ViewData["txtIsAdmin"] = "false";

                // Initialize or get the page state using session
                PlayerPageState pagestate = GetPageState();

                // Get the account id
                int accountid = 0;
                if (Session["UserAccountID"] != null)
                    accountid = Convert.ToInt32(Session["UserAccountID"]);

                // Set and save the page state to the submitted form values if any values are passed
                if (Request.Form["lstAscDesc"] != null)
                {
                    pagestate.AccountID = accountid;
                    pagestate.PlayerGroupID = Convert.ToInt32(Request.Form["lstPlayerGroup"].ToString().Trim());
                    pagestate.PlayerName = Request.Form["txtPlayerName"].ToString().Trim();
                    if (Request.Form["chkIncludeInactive"].ToLower().StartsWith("true"))
                        pagestate.IncludeInactive = true;
                    else
                        pagestate.IncludeInactive = false;
                    pagestate.SortBy = Request.Form["lstSortBy"].ToString().Trim();
                    pagestate.AscDesc = Request.Form["lstAscDesc"].ToString().Trim();
                    pagestate.PageNumber = Convert.ToInt32(Request.Form["txtPageNumber"].ToString().Trim());
                    SavePageState(pagestate);
                }

                // Add the session values to the view data so they can be populated in the form
                ViewData["AccountID"] = pagestate.AccountID;
                ViewData["PlayerGroupID"] = pagestate.PlayerGroupID;
                ViewData["PlayerName"] = pagestate.PlayerName;
                ViewData["IncludeInactive"] = pagestate.IncludeInactive;
                ViewData["SortBy"] = pagestate.SortBy;
                ViewData["PlayerGroupList"] = new SelectList(BuildPlayerGroupList(true), "Value", "Text", pagestate.PlayerGroupID);
                ViewData["SortByList"] = new SelectList(BuildSortByList(), "Value", "Text", pagestate.SortBy);
                ViewData["AscDescList"] = new SelectList(BuildAscDescList(), "Value", "Text", pagestate.AscDesc);

                // Determine asc/desc
                bool isdescending = false;
                if (pagestate.AscDesc.ToLower().StartsWith("d"))
                    isdescending = true;

                // Get a Count of all filtered records
                int recordcount = repository.GetPlayerRecordCount(pagestate.AccountID, pagestate.PlayerGroupID, pagestate.PlayerName, pagestate.IncludeInactive);

                // Determine the page count
                int pagecount = 1;
                if (recordcount > 0)
                {
                    pagecount = recordcount / Constants.PageSize;
                    if (recordcount % Constants.PageSize != 0) // Add a page if there are more records
                    {
                        pagecount = pagecount + 1;
                    }
                }

                // Make sure the current page is not greater than the page count
                if (pagestate.PageNumber > pagecount)
                {
                    pagestate.PageNumber = pagecount;
                    SavePageState(pagestate);
                }

                // Set the page number and account in viewdata
                ViewData["PageNumber"] = Convert.ToString(pagestate.PageNumber);
                ViewData["PageCount"] = Convert.ToString(pagecount);
                ViewData["RecordCount"] = Convert.ToString(recordcount);

                // We need to add the player group name
                IEnumerable<Player> players = repository.GetPlayerPage(pagestate.AccountID, pagestate.PlayerGroupID, pagestate.PlayerName, pagestate.IncludeInactive, pagestate.SortBy, isdescending, pagestate.PageNumber, pagecount);
                List<PlayerView> playerviews = new List<PlayerView>();
                IPlayerGroupRepository pgrep = new EntityPlayerGroupRepository();
                foreach (Player player in players)
                {
                    PlayerView playerview = new PlayerView();
                    playerview.PlayerID = player.PlayerID;
                    playerview.AccountID = player.AccountID;
                    playerview.PlayerGroupID = player.PlayerGroupID;
                    playerview.PlayerName = player.PlayerName;
                    playerview.PlayerLocation = player.PlayerLocation;
                    playerview.PlayerDescription = player.PlayerDescription;
                    playerview.IsActive = player.IsActive;
                    PlayerGroup pg = pgrep.GetPlayerGroup(player.PlayerGroupID);
                    playerview.PlayerGroupName = pg.PlayerGroupName;

                    playerviews.Add(playerview);
                }

                ViewResult result = View(playerviews);
                result.ViewName = "Index";
                return result;
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("Player", "Index", ex.Message);
                return RedirectToAction("Index", "ApplicationError");
            }
        }
        public ActionResult Edit(PlayerGroupSchedule playergroupschedule)
        {
            try
            {
                if (Session["UserAccountID"] == null)
                    return RedirectToAction("Validate", "Login");
                User user = (User)Session["User"];
                ViewData["LoginInfo"] = Utility.BuildUserAccountString(user.Username, Convert.ToString(Session["UserAccountName"]));
                if (user.IsAdmin)
                    ViewData["txtIsAdmin"] = "true";
                else
                    ViewData["txtIsAdmin"] = "false";

                int playergroupid = Convert.ToInt32(Request.Form["txtPlayerGroupID"]);
                int screenid = Convert.ToInt32(Request.Form["lstScreen"]);
                int hour = Convert.ToInt32(Request.Form["lstHour"]);
                int minute = Convert.ToInt32(Request.Form["lstMinute"]);

                if (!String.IsNullOrEmpty(Request.Form["chkSunday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 0, hour, minute);
                if (!String.IsNullOrEmpty(Request.Form["chkMonday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 1, hour, minute);
                if (!String.IsNullOrEmpty(Request.Form["chkTuesday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 2, hour, minute);
                if (!String.IsNullOrEmpty(Request.Form["chkWednesday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 3, hour, minute);
                if (!String.IsNullOrEmpty(Request.Form["chkThursday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 4, hour, minute);
                if (!String.IsNullOrEmpty(Request.Form["chkFriday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 5, hour, minute);
                if (!String.IsNullOrEmpty(Request.Form["chkSaturday"]))
                    CreatePlayerGroupSchedule(playergroupid, screenid, 6, hour, minute);

                IPlayerGroupRepository pgrep = new EntityPlayerGroupRepository();
                PlayerGroup playergroup = pgrep.GetPlayerGroup(playergroupid);
                ViewData["PlayerGroupID"] = playergroupid;
                ViewData["PlayerGroupName"] = playergroup.PlayerGroupName;

                ViewData["ScreenList"] = new SelectList(BuildScreenList(), "Value", "Text", "");
                ViewData["HourList"] = new SelectList(BuildHourList(), "Value", "Text", "");
                ViewData["MinuteList"] = new SelectList(BuildMinuteList(), "Value", "Text", "");

                ViewData["ScheduleHTML"] = BuildScheduleTable(playergroupid);

                return View(playergroupschedule);
            }
            catch (Exception ex)
            {
                Helpers.SetupApplicationError("PlayerGroupSchedule", "Edit", ex.Message);
                return RedirectToAction("Index", "ApplicationError");
            }
        }