public IHttpActionResult Create(NotificationDataModel model)
        {
            var notification = new Notification
            {
                Message = model.Message,
                NotificationState = model.NotificationState,
                NotificationType = model.NotificationType,
                DateCreated = model.DateCreated
            };

            //this.data.Notification.Add(notification);
            //this.data.SaveChanges();

            //model.ID = notification.ID;
            //model.DateCreated = notification.DateCreated;

            return null;
        }
        public HttpResponseMessage JoinGame(int id, JoinGameNumberModel numberModel)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "The game must have a number.");
            }

            string number = numberModel.Number;
            if (IfNumberHasRepeatingDigits(number))
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "The number must have unique digits.");
            }

            var game = this.data.Games.Find(id);
            if (game == null)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("Game with id = {0} does not exist.", id));
            }
            if (game.GameState != GameState.WaitingForOpponent)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("Game with id = {0} has already started.", id));
            }

            var currentUserID = this.User.Identity.GetUserId();
            if (game.RedPlayerId == currentUserID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("You have already joined game with id = {0}.", id));
            }

            game.BluePlayerNumber = number;
            game.BluePlayerId = currentUserID;
            game.BluePlayer = this.data.Users.Find(currentUserID);
            //this.data.SaveChanges();

            var newOpponentNotification = new Notification()
            {
                Message = string.Format("{0} joined your game.", this.User.Identity.Name),
                DateCreated = DateTime.Now,
                Type = NotificationType.GameJoined,
                State = NotificationState.Unread,
                GameId = game.Id,
                Game = game,
                PlayerId = game.RedPlayerId,
                Player = game.RedPlayer
            };
            this.data.Notifications.Add(newOpponentNotification);
            this.data.SaveChanges();

            var playerTurnNotification = new Notification()
            {
                Message = string.Format("It is your turn in game {0}", game.Name ),
                DateCreated = DateTime.Now,
                Type = NotificationType.YourTurn,
                State = NotificationState.Unread,
                GameId = game.Id,
                Game = game,
            };

            var randomNumber = this.random.Next(1, 11);
            if (randomNumber <= 5)
            {
                game.GameState = GameState.BlueInTurn;
                playerTurnNotification.PlayerId = game.BluePlayerId;
                playerTurnNotification.Player = game.BluePlayer;
            }
            else
            {
                game.GameState = GameState.RedInTurn;
                playerTurnNotification.PlayerId = game.RedPlayerId;
                playerTurnNotification.Player = game.RedPlayer;
            }
            this.data.Notifications.Add(playerTurnNotification);

            this.data.SaveChanges();

            return Request.CreateResponse(HttpStatusCode.OK, new GameCreatedResponseModel() { Result = string.Format("You joined game \"{0}\"", game.Name)});
        }
        private void SendWinnerNotification(ApplicationUser user, Game game)
        {
            var otherUser = game.Red == user ? game.Blue : game.Red;

            var newnotification = new Notification
            {
                User = otherUser,
                Message = string.Format(YouLostTo, user.UserName, game.Name),
                State = NotificationState.Unread,
                DateCreated = DateTime.Now,
                Game = game,
                GameId = game.Id,
                Type = NotificationType.GameLost,
            };


            user.Notifications.Add(new Notification
                                       {
                                           User = user,
                                           Message =
                                               string.Format(YouHaveWon, otherUser.UserName, game.Name),
                                           State = NotificationState.Unread,
                                           DateCreated = DateTime.Now,
                                           Game = game,
                                           GameId = game.Id,
                                           Type = NotificationType.GameWon,
                                       });

            otherUser.Notifications.Add(newnotification);
            this.BullsAndCowsData.SaveChanges();
        }
        private void SendNotification(ApplicationUser user, Game game)
        {
            var otherUser = game.Red == user ? game.Blue : game.Red;

            var newnotification = new Notification
            {
                User = otherUser,
                Message =
                    string.Format(ItsYourTurn, game.Name),
                State = NotificationState.Unread,
                DateCreated = DateTime.Now,
                Game = game,
                GameId = game.Id,
                Type = NotificationType.YourTurn,
            };

            otherUser.Notifications.Add(newnotification);
            this.BullsAndCowsData.SaveChanges();
        }
        public HttpResponseMessage MakeGuess(int id, JoinGameNumberModel numberModel)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid input data");
            }
            if (IfNumberHasRepeatingDigits(numberModel.Number))
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "The guess number must have unique digits.");
            }
            if (numberModel.Number.Length != 4)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "The guess number must have 4 digits.");
            }

            var game = this.data.Games.Find(id);
            if (game == null)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("Game with id = {0} does not exist.", id));
            }
            if (game.GameState == GameState.GameFinished || game.GameState == GameState.WaitingForOpponent)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("Game with id = {0} is not being played at the moment.", id));
            }

            var currentUserID = this.User.Identity.GetUserId();
            if (game.BluePlayerId != currentUserID && game.RedPlayerId != currentUserID)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, string.Format("You are not a player in game with id = {0}.", id));
            }
            if (game.BluePlayerId == currentUserID && game.GameState != GameState.BlueInTurn
                || game.RedPlayerId == currentUserID && game.GameState != GameState.RedInTurn)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, "It is not your turn");
            }
            int bulls = 0;
            int cows = 0;
            if (game.BluePlayerId == currentUserID)
            {
                GetBullsAndCows(game.RedPlayerNumber, numberModel.Number, out bulls, out cows);
            }
            else
            {
                GetBullsAndCows(game.BluePlayerNumber, numberModel.Number, out bulls, out cows);
            }

            var winingNotification = new Notification()
            {
                Message = "You won the game!",
                DateCreated = DateTime.Now,
                Type = NotificationType.GameWon,
                State = NotificationState.Unread,
                GameId = game.Id,
                Game = game,
            };

            var losingNotification = new Notification()
            {
                Message = "You lost the game",
                DateCreated = DateTime.Now,
                Type = NotificationType.GameLost,
                State = NotificationState.Unread,
                GameId = game.Id,
                Game = game,
            };

            var playerTurnNotification = new Notification()
            {
                Message = string.Format("It is your turn in game {0}", game.Name),
                DateCreated = DateTime.Now,
                Type = NotificationType.YourTurn,
                State = NotificationState.Unread,
                GameId = game.Id,
                Game = game,
            };

            if (bulls == 4 && game.RedPlayerId == currentUserID)
            {
                game.GameState = GameState.GameFinished;
                game.RedPlayer.Wins++;
                winingNotification.PlayerId = game.RedPlayerId;
                winingNotification.Player = game.RedPlayer;
                game.BluePlayer.Losses++;
                losingNotification.PlayerId = game.BluePlayerId;
                losingNotification.Player = game.BluePlayer;

                this.data.Notifications.Add(winingNotification);
                this.data.Notifications.Add(losingNotification);

                this.data.SaveChanges();
            }
            else if (bulls == 4 && game.BluePlayerId == currentUserID)
            {
                game.GameState = GameState.GameFinished;
                game.BluePlayer.Wins++;
                winingNotification.PlayerId = game.BluePlayerId;
                winingNotification.Player = game.BluePlayer;
                game.RedPlayer.Losses++;
                losingNotification.PlayerId = game.RedPlayerId;
                losingNotification.Player = game.RedPlayer;

                this.data.Notifications.Add(winingNotification);
                this.data.Notifications.Add(losingNotification);

                this.data.SaveChanges();
            }
            else
            {
                if (game.GameState == GameState.BlueInTurn)
                {
                    game.GameState = GameState.RedInTurn;
                    playerTurnNotification.PlayerId = game.RedPlayerId;
                    playerTurnNotification.Player = game.RedPlayer;
                }
                else if (game.GameState == GameState.RedInTurn)
                {
                    game.GameState = GameState.BlueInTurn;
                    playerTurnNotification.PlayerId = game.BluePlayerId;
                    playerTurnNotification.Player = game.BluePlayer;
                }

                this.data.Notifications.Add(playerTurnNotification);
                this.data.SaveChanges();
            }

            var guess = new Guess()
            {
                PlayerId = currentUserID,
                Player = this.data.Users.Find(currentUserID),
                GameId = game.Id,
                Game = game,
                Number = numberModel.Number,
                DateMade = DateTime.Now,
                CowsCount = cows,
                BullsCount = bulls
            };
            this.data.Guesses.Add(guess);
            this.data.SaveChanges();

            var response = new GuessDetails()
            {
                Id = guess.Id,
                UserId = guess.PlayerId,
                Username = guess.Player.UserName,
                GameId = guess.GameId,
                Number = guess.Number,
                DateMade = guess.DateMade,
                CowsCount = guess.CowsCount,
                BullsCount = guess.BullsCount
            };

            return Request.CreateResponse(HttpStatusCode.OK, response);
        }
        private void CreateNotificationForTurn(Game game)
        {

            ApplicationUser user;

            if (game.GameState == GameState.BlueInTurn)
            {
                user = game.Blue;
            }
            else
            {
                user = game.Red;
            }

            var newnotification = new Notification
            {
                User = user,
                Message = string.Format(ItsYourTurn, game.Name),
                State = NotificationState.Unread,
                DateCreated = DateTime.Now,
                Game = game,
                GameId = game.Id,
                Type = NotificationType.YourTurn,
            };

            user.Notifications.Add(newnotification);
            this.BullsAndCowsData.SaveChanges();
        }