public void EachTestInitialize()
		{
			Player p1 = new Player() {FirstName="TestFirstName1", LastName="TestLastName1", BirthDate=new DateTime(1993,10,10), PhoneNumber="606", Email="*****@*****.**", SkillLevel=.0f, City="Testowo", TopPosition=1};
			Player p2 = new Player() { FirstName = "TestFirstName2", LastName = "TestLastName2", BirthDate = new DateTime(1991, 11, 04), PhoneNumber = "606", Email = "*****@*****.**", SkillLevel = .5f, City = "Testolandia", TopPosition = 2 };
			Account a1 = new Account()
			{
				Login = "******",
				Password = "******",
				Player = p1
			};
			Account a2 = new Account()
			{
				Login = "******",
				Password = "******",
				Player = p2
			};
			context.Accounts.Add(a1);
			context.Accounts.Add(a2);
			context.Players.Add(p1);
			context.Players.Add(p2);
			context.SaveChanges();
			id1 = context.Accounts.FirstOrDefault<Account>(a => a.Login == "test1").AccountId;
			id2 = context.Accounts.FirstOrDefault<Account>(a => a.Login == "test2").AccountId;
			context.SaveChanges();
		}
		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 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 PlayerStats(Player p)
		{
			FirstName = p.FirstName;
			LastName = p.LastName;
			Age = p.GetAge();
			Position = p.GetRank();
			TopPosition = p.TopPosition;
			won =  p.GetWonMatchesCount();
			lost = p.GetLostMatchesCount();
			Level = p.SkillLevel;
			PlayedMatches = won + lost;
			MatchesInMonth = p.GetMonthlyMatchesCount();
			
			DateTime? date = p.GetLastMatchDate();
			if (date == null)
				LastMatchDate = "(brak spotkań)";
			else
				LastMatchDate = date.ToString();
				
		}
		 public void MyTestInitialize() 
		{
			 Player p1 = new Player() { FirstName = "Test_1", LastName = "Test", BirthDate = new DateTime(1990,1,1), City = "TEST", Email = "*****@*****.**", SkillLevel = 2 };
			 context.Accounts.Add(new Account() { Login = "******", Password = "******", Player = p1 });
			 context.Players.Add(p1);
			 Player p2 = new Player() { FirstName = "Test_2", LastName = "Testowy", BirthDate = new DateTime(1991,1,1), City = "TESTION", Email = "*****@*****.**", SkillLevel = 4 };
			 context.Accounts.Add(new Account() { Login = "******", Password = "******", Player = p2 });
			 context.Players.Add(p2);
			 Player p3 = new Player() { FirstName = "Test_3", LastName = "Te?ciak", BirthDate = new DateTime(1992,2,2), City = "T", Email = "*****@*****.**", SkillLevel = 1};
			 context.Accounts.Add(new Account() { Login = "******", Password = "******", Player = p3 });
			 context.Players.Add(p3);
			 context.SaveChanges();

			 p1 = context.Players.FirstOrDefault<Player>(p => p.FirstName == "Test_1");
			 p2 = context.Players.FirstOrDefault<Player>(p => p.FirstName == "Test_2");
			 p3 = context.Players.FirstOrDefault<Player>(p => p.FirstName == "Test_3");
			 context.Duels.Add(new Duel() { Accepted = true, GuestPlayerId = p1.AccountId, HomePlayerId = p2.AccountId, Result = "3:0", Seen = true, DateOfPlay = new DateTime(2015, 1, 15, 0, 0, 0) });
			 context.Duels.Add(new Duel() { Accepted = true, GuestPlayerId= p1.AccountId, HomePlayerId = p3.AccountId, Result = "1:3", Seen = true, DateOfPlay=new DateTime(2014,12,12,0,0,0) });
			 context.SaveChanges();
		 }
		public void Test_CreateAccount()
		{
			Account acc = null;
			Player p = null;

			bool result1 = Account.CreateAccount(acc, p);
			acc = new Account { Login = "******", Password = "******"};
			p = new Player { FirstName = "AccountTest", LastName = "AccountTest",  BirthDate=new DateTime(2015,01,01), Email = "*****@*****.**", City = "AccountTest" };

			bool result2 = Account.CreateAccount(acc, p);
			bool result3 = Account.CreateAccount(acc, p);
			Assert.IsFalse(result1);
			Assert.IsTrue(result2);
			Assert.IsFalse(result3);
		}
		public bool CanPlay(Player player, DateTime date)
		{
			bool hasAnotherMatch;
			hasAnotherMatch = (from m in player.Matches
							   where m.DateOfPlay.Year == date.Year && m.DateOfPlay.Month == date.Month && m.DateOfPlay.Day == date.Day
							   select m).Any<Duel>();
			return !hasAnotherMatch;
		}
		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 Duel(Player _homePlayer, Player _guestPlayer)
		{
			HomePlayer = _homePlayer;
			GuestPlayer = _guestPlayer;
		}
		public ActionResult ProfileEdition(Player model, HttpPostedFileBase file)
		{
			string fileName, fileExt;
			int width = 140;
			int height = 140;
			if(ModelState.IsValid)
			{
				if (file != null && file.ContentLength > 0)
				{
					char DirSeparator = System.IO.Path.DirectorySeparatorChar;
					string FilesPath = HttpContext.Server.MapPath("~\\Content" + DirSeparator + "Uploads" + DirSeparator);
					
					fileName = (DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString()
						+ DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()) + "_" + (string)Session["LoggedInPlayer"];

					fileExt = Path.GetExtension(file.FileName);
					if(!fileExt.Contains("jpg") && !fileExt.Contains("png") && !fileExt.Contains("bmp"))
					{
						ViewData.Add("WrongFileExtension", (bool)true);
						return View(model);
					}
					if (!Directory.Exists(FilesPath))
					{
						Directory.CreateDirectory(FilesPath);
					}
					string path = FilesPath + DirSeparator + fileName + fileExt; 
					file.SaveAs(Path.GetFullPath(path));
					model.ImagePath = fileName + fileExt;

					//zapisz thumbnail:

					string thumbnailDirectory = String.Format(@"{0}{1}{2}", FilesPath, DirSeparator, "Thumbnails");

					if (!Directory.Exists(thumbnailDirectory))
					{
						Directory.CreateDirectory(thumbnailDirectory);
					}

					string imagePath = String.Format(@"{0}{1}{2}{3}", thumbnailDirectory, DirSeparator, fileName, fileExt);
					
					FileStream stream = new FileStream(Path.GetFullPath(imagePath), FileMode.OpenOrCreate);

					Image OrigImage = Image.FromStream(file.InputStream);
					
					Bitmap TempBitmap = new Bitmap(width,height);

					Graphics NewImage = Graphics.FromImage(TempBitmap);
					NewImage.CompositingQuality = CompositingQuality.HighQuality;
					NewImage.SmoothingMode = SmoothingMode.HighQuality;
					NewImage.InterpolationMode = InterpolationMode.HighQualityBicubic;

					Rectangle imageRectangle = new Rectangle(0, 0, width, height);
					NewImage.DrawImage(OrigImage, imageRectangle);

					TempBitmap.Save(stream, OrigImage.RawFormat);

					NewImage.Dispose();
					TempBitmap.Dispose();
					OrigImage.Dispose();
					stream.Close();
					stream.Dispose();
				}
				model.AccountId = (int)Session["LoggedInPlayerId"];
				model.UpdatePlayer();
			}
			return View(model);
		}
		public PlayerDuels(Player player, Duel d)
		{
			Opponent = d.HomePlayerId == player.AccountId ? d.GuestPlayer : d.HomePlayer;
			Score = d.Result;
			Date = d.DateOfPlay;
		}