using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

/// <summary>
/// Handles the display of courses in the ListCourseScreen and CourseScreenManager scene
/// </summary>
public class CourseItem : MonoBehaviour
{
    /// <summary>
    /// Reference to the course object
    /// </summary>
    public Course course;

    /// <summary>
    /// Progress of the current user on this specific course
    /// </summary>
    public float progress;

    /// <summary>
    /// UI Reference to the image for displaying the course thumbnail
    /// </summary>
    public Image thumbnail;

    /// <summary>
    /// UI Reference to the gameobject for displaying the course title
    /// </summary>
    public TMP_Text title;

    /// <summary>
    /// UI Reference to the button so the correct callback can be trigger on click
    /// </summary>
    public Button button;

    /// <summary>
    /// Reference to the slider that displays the progress of the user
    /// </summary>
    public GameObject slider;

    /// <summary>
    /// Reference to the gameobject that holds the text 'COMPLETED'
    /// </summary>
    public GameObject completed;

    /// <summary>
    /// Start is called before the first frame update
    /// </summary>
    void Start()
    {
        // Use public function so that this component can get Instantiated
        GenerateContent();
    }

    /// <summary>
    /// (Re)generate the CourseItem object and update its appearance
    /// </summary>
    public void GenerateContent()
    {
        // Set appearance
        thumbnail.sprite = course.thumbnail;
        title.text = course.title;

        // Set progress
        progress = Mathf.Clamp01(progress);
        completed.SetActive(1.0f <= progress);
        slider.SetActive(0.0f <= progress && progress < 1.0f);
        slider.GetComponent<Slider>().value = progress;

        // Add click functionality
        button.onClick.AddListener(() => SceneManager.LoadScene("Courses/Scenes/Course_0"));
    }
}
