using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
///
/// UserProgressScreen scene manager
///
public class UserProgressScreen : MonoBehaviour
{
///
/// Reference to the current user
///
private User user;
///
/// UI reference to the username
///
public TMP_Text username;
///
/// UI reference to the user's avatar
///
public Image avatar;
///
/// UI reference to the user total playtime
///
public TMP_Text playtime;
///
/// Prefab of the highscore marker to display on the graph
///
public GameObject highscoreMarker;
///
/// Prefab of a course card
///
public GameObject courseCardPrefab;
///
/// UI reference to the container holding all course cards
///
public GameObject coursesContainer;
///
/// UI reference to the message that displays when no course progress is present
///
public GameObject emptyCourses;
///
/// Prefab of a minigame card
///
public GameObject minigameCardPrefab;
///
/// UI reference to the container holding all the minigame cards
///
public GameObject minigamesContainer;
///
/// UI reference to the message that displays when no minigame progress is present
///
public GameObject emptyMinigames;
///
/// UI reference to the plot
///
public RawImage progressGraph;
///
/// Current selected activity draw to the graph
///
private int selectedActivity = -1;
///
/// List of activity backgrounds and indices
///
private List> activities = new List>();
///
/// Start is called before the first frame update
///
void Start()
{
// Assign the current user
PersistentDataController.GetInstance().Load();
user = UserList.GetCurrentUser();
// Set correct displayed items
username.text = user.GetUsername();
avatar.sprite = user.GetAvatar();
playtime.text = $"Totale speeltijd: {user.GetPlaytime().ToString("0.00")}";
// Set graph inactive
progressGraph.gameObject.SetActive(false);
int i = 0;
// Display courses
var courses = user.GetCourses();
coursesContainer.SetActive(courses.Count > 0);
emptyCourses.SetActive(courses.Count <= 0);
foreach (var courseProgress in courses)
{
// Create instance of prefab
GameObject instance = GameObject.Instantiate(courseCardPrefab, coursesContainer.transform.Find("Viewport").Find("Content").transform);
int j = i++;
// Initialize card
CourseProgressCard cpc = instance.GetComponent();
cpc.courseProgress = courseProgress;
cpc.selectActivity = () => UpdateSelection(j);
// Store reference to background so we can apply fancy coloring
Image background = instance.GetComponent();
background.color = Color.gray;
activities.Add(Tuple.Create(background, (int)courseProgress.courseIndex));
}
// Display minigames
var minigames = user.GetMinigames();
minigamesContainer.SetActive(minigames.Count > 0);
emptyMinigames.SetActive(minigames.Count <= 0);
foreach (var minigameProgress in minigames)
{
// Create instance of prefab
GameObject instance = GameObject.Instantiate(minigameCardPrefab, minigamesContainer.transform.Find("Viewport").Find("Content").transform);
int j = i++;
// Initialize card
MinigameProgressCard mpc = instance.GetComponent();
mpc.minigameProgress = minigameProgress;
mpc.selectActivity = () => UpdateSelection(j);
// Store reference to background so we can apply fancy coloring
Image background = instance.GetComponent();
background.color = Color.gray;
activities.Add(Tuple.Create(background, (int)minigameProgress.minigameIndex));
}
}
///
/// Update the current selected activity
///
/// Index to the new activity
private void UpdateSelection(int newActivity)
{
if (selectedActivity < 0)
{
progressGraph.gameObject.SetActive(true);
}
else
{
activities[selectedActivity].Item1.color = Color.gray;
}
selectedActivity = newActivity;
activities[selectedActivity].Item1.color = Color.blue;
if (selectedActivity < user.GetCourses().Count)
{
// TODO: create a better graph
//DisplayCourseGraph((CourseIndex)activities[selectedActivity].Item2);
// For now: just deactivate graph rendering
progressGraph.gameObject.SetActive(false);
}
else
{
DisplayMinigameGraph((MinigameIndex)activities[selectedActivity].Item2);
// TODO: remove line, this is only because courses deactivates the graph
progressGraph.gameObject.SetActive(true);
}
}
///
/// Plot the graph of a course
///
/// Index of the course
/// TODO: create a better plot
private void DisplayCourseGraph(CourseIndex index) { }
///
/// Plot the graph of a minigame
///
/// Index of the minigame
/// TODO: reworking
private void DisplayMinigameGraph(MinigameIndex minigameIndex) { }
}