using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

/// <summary>
/// CourseScreen scene manager
/// </summary>
public class CourseScreenManager : MonoBehaviour
{
    /// <summary>
    /// Reference to text that displays when there are no recent courses
    /// </summary>
    public GameObject noRecentCourses;

    /// <summary>
    /// Reference to recent-courses-list container object
    /// </summary>
    public Transform recentCoursesContainer;

    /// <summary>
    /// Reference to recommended-courses-list container object
    /// </summary>
    public Transform recommendedCoursesContainer;

    /// <summary>
    /// Prefab of the course item object
    /// </summary>
    public GameObject courseItem;

    /// <summary>
    /// Reference to the users so we can get the current user;
    /// </summary>
    public UserList userList;

    /// <summary>
    /// Reference to the courses
    /// </summary>
    public CourseList courseList;

    /// <summary>
    /// Start is called before the first frame update
    /// </summary>
    void Start()
    {
        User user = userList.GetCurrentUser();

        // Recent courses
        List<Tuple<CourseIndex, float>> recentCourses = user.GetRecentCourses();
        noRecentCourses.SetActive(recentCourses.Count <= 0);
        foreach (Tuple<CourseIndex, float> course in recentCourses)
        {
            // Create instance of prefab
            GameObject instance = GameObject.Instantiate(courseItem, recentCoursesContainer);

            // Dynamically load appearance
            CourseItem item = instance.GetComponent<CourseItem>();
            item.course = courseList.courses.Find((j) => j.index == course.Item1);
            item.progress = course.Item2;
        }

        // Recommended courses
        List<Tuple<CourseIndex, float>> recommenedCourses = user.GetRecommendedCourses();
        foreach (Tuple<CourseIndex, float> course in recommenedCourses)
        {
            // Create instance of prefab
            GameObject instance = GameObject.Instantiate(courseItem, recommendedCoursesContainer);

            // Dynamically load appearance
            CourseItem item = instance.GetComponent<CourseItem>();
            item.course = courseList.courses.Find((j) => j.index == course.Item1);
            item.progress = course.Item2;
        }
    }

    /// <summary>
    /// Method used as callback for course item onClick events
    /// </summary>
    /// <param name="sceneName">The path to the new scene (<c>path == $"Assets/{sceneName}"</c>)</param>
    public void LoadScene(string sceneName)
    {
        SceneManager.LoadScene(sceneName);
    }
}
