public bool checkPrize(Bet toCheck, Bet drawn)
        {
            string[] numbersDrawn = drawn.Numbers.Split ('-');
            string[] starsDrawn = drawn.Stars.Split ('-');

            string[] numbersToCheck = toCheck.Numbers.Split ('-');
            string[] starsToCheck = toCheck.Stars.Split ('-');

            var commonNumbers = numbersDrawn.Intersect (numbersToCheck);
            var commonStars = starsDrawn.Intersect (starsToCheck);

            //0 estrelas minimo 2 numeros
            if (commonStars.Count() == 0)
            {
                if (commonNumbers.Count()>= 2) {
                    return true;
                } else
                {
                    return false;
                }
            }
            //1 estrela minimo 2 numeros
            if (commonStars.Count() == 1)
            {
                if (commonNumbers.Count() >= 2) {
                    return true;
                } else
                {
                    return false;
                }

            }
            //2 estrelas minimo 1 numero
            if (commonStars.Count() == 2)
            {
                if (commonNumbers.Count() >= 1) {
                    return true;
                } else
                {
                    return false;
                }

            }
            //1 numero minimo 2 estrelas
            if (commonNumbers.Count() == 1)
            {
                if (commonStars.Count() >= 2) {
                    return true;
                } else
                {
                    return false;
                }

            }

            return false;
        }
		public MyBets()
		{

			// Create the ListView.
			listView = new ListView();
			// Define template for displaying each item.
			listView.ItemTemplate = new DataTemplate(typeof(BetCell));
			//Define action for item selected
			listView.ItemSelected += (sender, e) => {
				var BetObject = (Bet)e.SelectedItem;
				var BetPage = new BetPage ();
				BetPage.BindingContext = BetObject;
				((App)App.Current).ResumeAtBetId = BetObject.ID;
				Navigation.PushAsync (BetPage);
			};

			// Accomodate iPhone status bar.
			this.Padding = new Thickness(10, Device.OnPlatform(20, 0, 0), 10, 5);
			listView.HasUnevenRows = true;

			var tbi =  new ToolbarItem("+", null, async () =>
				{
					var action =  await DisplayActionSheet ("Add Bet", "Cancel", null, "Manually", "Picture");
					switch (action){
					case "Manually":
						{
							var BetObject = new Bet();
							var BetPage = new BetPage();
							BetPage.BindingContext = BetObject;
							Navigation.PushAsync(BetPage);
							break;
						}
					case "Picture":
						{
							Navigation.PushAsync( new CameraView());
							break;
						}
					}

				}
			);
			this.ToolbarItems.Add(tbi);

			listView.IsPullToRefreshEnabled = true;

			listView.Refreshing += ListView_Refreshing;

			// Build the page.
			this.Content = new StackLayout
			{
				Children = 
				{
					listView
				}
				};
		}
		//Here you will receive all push notification messages
		//Messages arrives as a dictionary, the device type is also sent in order to check specific keys correctly depending on the platform.
		public async void OnMessage(IDictionary<string, object> Parameters, DeviceType deviceType)
		{
			Debug.WriteLine("Message Arrived");
			//UPDATE ALL BETS FROM CLOUD
			WebService conn = new WebService ();
			var dbUpdated =  await conn.GetData(App.Current.Properties["key"] as string);
			DependencyService.Get<ISQLite>().OverrideDatabase(dbUpdated[0].data);
			Debug.WriteLine("Data UPDATED");

			IEnumerable<Bet> allBets = App.Database.GetBets ().ToList ();
			List<Dictionary<string, dynamic>> rep2 = null;
	
			rep2 = await conn.GetResults (App.Current.Properties ["key"] as string);
			var msg  = JsonConvert.DeserializeObject<HelperAPI>(rep2[0]["drawn"]);

			Bet Sorteio = new Bet ();

			//Parse string[] to string
			string numbersFinal = "";
			string starsFinal = "";
			int counter = 0;
			foreach (string n in msg.numbers)
			{
				if (counter != 4) {
					numbersFinal += n + "-";
				} else {
					numbersFinal += n;
				}
			}
			counter = 0;
			foreach (string s in msg.stars)
			{
				if (counter != 1) {
					starsFinal += s + "-";
				} else {
					starsFinal += s;
				}
			}
			Sorteio.Numbers = numbersFinal;
			Sorteio.Stars = starsFinal;
			Sorteio.Date = DateTime.Now.ToString ();



			BetResult BetRes = new BetResult ();
			foreach (Bet b in allBets)
			{
				//Consoante resultado da funcao checkPrize();
				if (b.Status.Equals("WAIT"))
					{
					//Consoante resultado da funcao checkPrize();
					if (BetRes.checkPrize (b, Sorteio)) {
						b.Status = "WIN";
					} else {
						b.Status = "LOSS";
					}

					App.Database.SaveBet (b);
					}
			}


		}
        private void GetImagesFromBet(Bet betObj)
        {
            string[] numbers = betObj.Numbers.Split ('-');
            string[] stars = betObj.Stars.Split ('-');
            int counter = 0;
            int counter2 = 0;

            //Load images of the numbers in the Bet
            foreach (string num in numbers) {
                numbersList[counter].Source = ImageSource.FromFile ("n"+num+".png");
                counter++;
            }

            //Load the images of the stars in the Bet
            foreach (string star in stars) {
                starsList[counter2].Source = ImageSource.FromFile ("star"+star+".png");
                counter2++;
            }
        }
		async void Btn2_Clicked (object sender, EventArgs e)
		{	try{
				
				WebService conn = new WebService ();
				string token = Application.Current.Properties ["key"] as string;


				var response =  await conn.RequestOCR (imgByte,token);


				if(response != null)
				{
					var dataReceived = response[0];	
					if(dataReceived.result.Equals("True")){
						await DisplayAlert("Message", "Your photo has been sucessfully sent","OK");
						bool _done = false;
						List<Dictionary<string, dynamic>> rep2 = null;
						await App.Sleep(2000);
						while(!_done){
							rep2 = await conn.GetTicket(token, dataReceived.request);
							var responseResult = rep2[0]["result"];
							if(!responseResult.Equals("WAIT"))
							{
								_done = true;
							}
							else
							{
								await App.Sleep(1000);
							}
						}
						var combinationResult = rep2[0]["response"];
						var msg  = JsonConvert.DeserializeObject<HelperAPI>(rep2[0]["response"]);
						await conn.DeleteTicket(token, dataReceived.request);
						if(rep2[0]["result"])
						{
							await DisplayAlert("Message", "Ticket Received","OK");
							//Move to AddPage
							var BetObject = new Bet();
							var BetPage = new BetPage();
							BetObject.Date = DateTime.Now.ToString ();
							//Parse string[] to string
							string numbersFinal = "";
							string starsFinal = "";
							int counter = 0;
							foreach (string n in msg.numbers)
							{
								if(counter != 4){
									numbersFinal +=n+"-";
								}
								else {numbersFinal +=n;
								}
							}
							counter = 0;
							foreach (string s in msg.stars)
							{
								if(counter != 1){
									starsFinal +=s+"-";
								}else{
								starsFinal +=s;
								}
							}
							BetObject.Numbers = numbersFinal;
							BetObject.Stars = starsFinal;
							BetPage.BindingContext = BetObject;
							Navigation.PushAsync(BetPage);


						}
						else{
							await DisplayAlert("Message", "Some error ocurred" ,"OK");
						}

					}
					else{
						await DisplayAlert("Error", "Deu merda amigo","OK");
					}
				}



			}
			catch(Exception exc){
				DisplayAlert("Error",exc.Message,"OK");
			}

		}