public static bool CreateAccount(Account acc, Player p)
		{
			if (acc == null || p == null) return false;
			if (CheckAvailability(acc.Login) == false) return false;
			acc.Player = p;
			acc.Password = Encrypter.GetSHA256Hash(acc.Password);
			using (var db = new TennisOrganizerContext())
			{
				db.Accounts.Add(acc);
				db.Players.Add(p);
				db.SaveChanges();

				var query = (from a in db.Accounts
							 where a.Login == acc.Login
							 select a);
				if (query.Count<Account>() != 0) return true;
				else return false;
			}
		}
		public void TestCelanup()
		{
			db = new TennisOrganizerContext();
			var query = db.Accounts.FirstOrDefault<Account>(a => a.Login == "Account_Test");
			if (query != null)
			{
				var DuelQuery = (from d in db.Duels
								 where d.HomePlayerId == query.AccountId || d.GuestPlayerId == query.AccountId
								 select d);

				foreach (var d in DuelQuery)
				{
					db.Duels.Remove(d);
				}
				db.Players.Remove(query.Player);
				db.Accounts.Remove(query);

			}
			db.SaveChanges();
		}
		public bool UpdateAccount(String oldPassword, String newPassword = "******", String newLogin = "******")
		{
			if (this == null) return false;
			using (var db = new TennisOrganizerContext())
			{
				var query = db.Accounts.FirstOrDefault<Account>(a => a.AccountId == this.AccountId);
				if (query == null) return false;
				else if (query.Password != Encrypter.GetSHA256Hash(oldPassword)) return false;
				else
				{
					if (newLogin != "default" && newLogin != null)
					{
						query.Login = newLogin;
					}
					if (newPassword != "default" && newPassword != null)
					{
						query.Password = Encrypter.GetSHA256Hash(newPassword);
					}
					db.SaveChanges();
					return true;
				}
			}
		}
		public bool UpdatePlayer()
		{
			using(var db = new TennisOrganizerContext())
			{
				var p = db.Players.Find(this.AccountId);
				if (p == null) return false;
				
				p.FirstName = this.FirstName;
				p.LastName = this.LastName;
				p.BirthDate = this.BirthDate;
				p.City = this.City;
				p.Email = this.Email;
				p.PhoneNumber = this.PhoneNumber;
				p.SkillLevel = this.SkillLevel;
				p.ImagePath = this.ImagePath;
				
				db.SaveChanges();
			}
			return true;
		}
		public bool UpdatePlayer(String Password, Player p)
		{
			if (this == null || p == null) return false;
			using (var db = new TennisOrganizerContext())
			{
				var query = db.Accounts.FirstOrDefault<Account>(a => a.AccountId == this.AccountId);
				if (query == null || query.Password != Password) return false;
				else
				{
					var updated = db.Players.FirstOrDefault<Player>(player => player.AccountId == this.AccountId);
					if (updated == null) return false;
					updated.FirstName = p.FirstName;
					updated.LastName = p.LastName;
					updated.BirthDate = p.BirthDate ;
					updated.PhoneNumber = p.PhoneNumber;
					updated.Email = p.Email;
					updated.ImagePath = p.ImagePath;
					updated.City = p.City;

					db.SaveChanges();
					return true;
				}
			}
		}
		public ActionResult Challenge(ChallengeCriteria cc)
		{
			Player player = Player.GetPlayerByLogin(User.Identity.Name);

			
			if (cc.OpponentNumber <= 0)
			{
				if (cc.AgeFrom == null)
					cc.AgeFrom = 0;
				if (cc.AgeTo == null)
					cc.AgeTo = 200;
				if (cc.LevelFrom == null)
					cc.LevelFrom = 0;
				if (cc.LevelTo == null)
					cc.LevelTo = 100;

				if (cc.City == null || cc.City == String.Empty)
					cc.SuitableOpponents = player.GetOpponentsBy(cc.Date, (int)cc.AgeFrom, (int)cc.AgeTo, (float)cc.LevelFrom, (float)cc.LevelTo);
				else
					cc.SuitableOpponents = player.GetOpponentsBy(cc.Date, (int)cc.AgeFrom, (int)cc.AgeTo, (float)cc.LevelFrom, (float)cc.LevelTo, cc.City);

				
				return View(cc);
			}

			// else
			Player opponent = new Player() {  AccountId = cc.OpponentNumber};
			String[] hour = cc.Hour.ToString().Split(':') ;
			DateTime date = (DateTime)cc.Date;
			//dateOfPlay.Hour = hour[0];
			//dateOfPlay.Minute = hour[1];
			DateTime dateOfPlay;
			try
			{
				dateOfPlay = new DateTime(date.Date.Year, date.Date.Month, date.Date.Day, int.Parse(hour[0]), int.Parse(hour[1]), 0);
			}
			catch (IndexOutOfRangeException)
			{
				return View(cc);
			}

			using (var db = new TennisOrganizerContext())
			{
				db.Duels.Add(new Duel() { Accepted = false, GuestPlayerId = opponent.AccountId, HomePlayerId = player.AccountId, Seen = false, DateOfPlay = dateOfPlay });
				db.SaveChanges();

				TempData.Add("opponentName", Player.GetPlayerById(cc.OpponentNumber).ToString());
				TempData.Add("dateOfPlay", cc.Date.ToShortDateString());
				TempData.Add("hourOfPlay", cc.Hour);

				return RedirectToAction("ChallengeSuccess", "Main");
			}
		}
		public ActionResult Training(TrainingCriteria cc)
		{
			Player player = Player.GetPlayerByLogin(User.Identity.Name);


			if (cc.OpponentNumber <= 0)
			{
				return View(cc);
			}
			Player opponent = Player.GetPlayerById(cc.OpponentNumber);
			String[] hour = cc.Hour.ToString().Split(':');
			DateTime date = (DateTime)cc.Date;
			DateTime dateOfPlay = new DateTime(date.Date.Year, date.Date.Month, date.Date.Day, int.Parse(hour[0]), int.Parse(hour[1]), 0);

			using (var db = new TennisOrganizerContext())
			{
				db.Duels.Add(new Duel() { Accepted = false, GuestPlayerId = opponent.AccountId, HomePlayerId = player.AccountId, Seen = false, DateOfPlay = dateOfPlay });
				db.SaveChanges();
				try
				{
					Mailer.NotifyAboutChallenge(player.ToString(), opponent.Email, opponent.ToString());
				}
				catch(FormatException e)
				{

				}
				TempData.Add("opponentName", Player.GetPlayerById(cc.OpponentNumber).ToString());
				TempData.Add("dateOfPlay", cc.Date.ToShortDateString());
				TempData.Add("hourOfPlay", cc.Hour);

				return RedirectToAction("TrainingSuccess", "Main");
			}
		}