using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
///
/// Class to handle course list progress display
///
public class PanelCourseProgress : MonoBehaviour
{
///
/// Reference to the current user
///
private User user;
///
/// Reference to the courses list
///
public CourseList courseList;
///
/// Prefab of a course card
///
public GameObject courseCardPrefab;
///
/// UI reference to the container holding all course cards
///
public Transform coursesContainer;
///
/// UI reference to the course info panel
///
public GameObject courseInfo;
///
/// UI reference to the message that displays when no course progress is present
///
public GameObject emptyCourses;
///
/// Reference to the title on the info panel
///
public TMP_Text courseTitle;
///
/// Reference to the learnable card prefab
///
public GameObject learnableCardPrefab;
///
/// Reference to the container for holding the learnable cards
///
public Transform learnablesContainer;
///
/// Reference to the course progress bar on the info panel
///
public SlicedSlider progressBar;
///
/// Current selected course
///
private int selectedCourse = 0;
///
/// List of course backgrounds and indices
///
private List> courseCards = new List>();
///
/// Start is called before the first frame update
///
void Start()
{
PersistentDataController.GetInstance().Load();
user = UserList.GetCurrentUser();
var courses = user.GetCourses();
courseInfo.SetActive(0 < courses.Count);
emptyCourses.SetActive(courses.Count <= 0);
int i = 0;
foreach (var courseProgress in courses)
{
// Create instance of prefab
GameObject instance = GameObject.Instantiate(courseCardPrefab, coursesContainer);
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;
this.courseCards.Add(Tuple.Create(background, courseProgress.courseIndex));
}
if (0 < courses.Count)
UpdateSelection(0);
}
///
/// Update the current selected course
///
/// Index to the new course
private void UpdateSelection(int newCourse)
{
courseCards[selectedCourse].Item1.color = Color.gray;
selectedCourse = newCourse;
courseCards[selectedCourse].Item1.color = Color.blue;
var progress = user.GetCourseProgress(courseCards[selectedCourse].Item2);
var course = courseList.GetCourseByIndex(progress.courseIndex);
courseTitle.text = course.title;
progressBar.fillAmount = progress.progress;
foreach (Transform child in learnablesContainer)
Destroy(child.gameObject);
for (int i = 0; i < course.theme.learnables.Count; i++)
{
GameObject instance = GameObject.Instantiate(learnableCardPrefab, learnablesContainer);
var script = instance.GetComponent();
script.learnable = course.theme.learnables[i];
script.progress = progress.learnables[i];
}
}
}