using Android.App;
using Android.Content;
using Android.Gms.Tasks;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Firebase.Firestore;
namespace RunAndEarn
{
[Activity(Label = "LeaderboardActivity")]
public class LeaderboardActivity : Activity, Android.Views.View.IOnClickListener, IOnCompleteListener
{
private Button btnBack;
private ListView lvLeaderboard;
private FbData fbData;
private ScoreAdapter scoreAdapter;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
SetContentView(Resource.Layout.activity_leaderboard);
InitObjects();
Initviews();
}
/// <summary>
/// הפעולה בונה את כל האובייקטים שבמשחק
/// </summary>
private void InitObjects()
{
fbData = new FbData();
scoreAdapter = new ScoreAdapter(this);
}
/// <summary>
/// הפעולה מוסיפה את כל הוויוז למסך
/// </summary>
private void Initviews()
{
btnBack = FindViewById<Button>(Resource.Id.btnBack);
lvLeaderboard = FindViewById<ListView>(Resource.Id.lvLeaderboard);
btnBack.SetOnClickListener(this);
lvLeaderboard.Adapter = scoreAdapter;
}
/// <summary>
/// הפעולה עוצרת את המשחק שנלחץ הכפתור של העצירה
/// </summary>
public void OnClick(View v)
{
if(v== btnBack)
{
Intent intent = new Intent(this, typeof(MainActivity));
StartActivity(intent);
Finish();
}
}
/// <summary>
/// הפעולה מעדכנת את טבלת השיאים לפי רשימת השיאים
/// </summary>
public void OnComplete(Task task)
{
if (task.IsSuccessful)
{
int scoreVal;
string username;
DocumentSnapshot ds = (DocumentSnapshot)task.Result;
JavaList scores = (JavaList)ds.Get(General.FB_DOCUMENT_NAME);
scoreAdapter.ClearList();
for (int i = 0; i <scores.Count; i++)
{
JavaDictionary score = (JavaDictionary)scores[i];
scoreVal = int.Parse(score["score"].ToString());
username = (score["username"].ToString());
scoreAdapter.Add(new Score(scoreVal, username));
}
scoreAdapter.SetLeaderboard();
}
}
}
}