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

public class CourseScreenManager : MonoBehaviour
{
    [Header("Course Screen Components")]
    // Reference to text that displays when there are no recent courses
    public GameObject noRecentCourses;
    // Reference to recent-courses-list holder object
    public Transform recentCoursesContainer;
    // Reference to recommended-courses-list holder object
    public Transform recommendedCoursesContainer;

    [Header("Prefabs")]
    // CourseItem prefab
    public GameObject courseItem;

    [Header("User")]
    // Reference to the users so we can get the current user;
    public UserList userList;
    // Reference to the courses
    public CourseList courseList;

    void Start()
    {
        User user = userList.users[userList.currentUserIndex];

        // 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;
        }
    }

    // Method used as callback for on click events
    public void LoadScene(string sceneName)
    {
        SceneManager.LoadScene(sceneName);
    }
}
