using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using TMPro;
using UnityEditor;

public class StartPause : MonoBehaviour
{

    [Header("UI References")]
    // Reference to instructional video player
    public VideoPlayer player;
    // Reference to pause button
    public Button button;
    // Reference to sprite for the pause button
    public Sprite pauseSprite;
    // Reference to the image for displaying the current words sprite
    public Image wordImage;
    // Reference to the text object for displaying the current word
    public TextMeshProUGUI title;

    [Header("User")]
    // Reference to user list to get current user
    public UserList userList;
    // The current user
    private User user;
    // Current user progress for this course
    private Progress progress = null;

    [Header("Course")]
    // ScriptableObject with list of all courses
    public CourseList courselist;
    // Reference to Course ScriptableObject
    private Course course;

    // Index of the current word/letter in the course.learnables list
    private int currentWordIndex = 0;
    // In my example, i have 4 videos/images
    private int maxWords = 4;
    // Number of correct words so far
    // (can be modified to a list or something like that to give better feedback)
    private int correctWords = 0;

    public void Awake()
    {
        // Setting up course
        course = courselist.courses[courselist.currentCourseIndex];

        // Create entry in current user for keeping track of progress
        user = userList.users[userList.currentUserIndex];
        progress = user.courses.Find((p) => p != null && p.Get<CourseIndex>("courseIndex") == course.index);
        if (progress == null)
        {
            progress = new Progress();
            progress.AddOrUpdate<CourseIndex>("courseIndex", CourseIndex.FINGERSPELLING);
            progress.AddOrUpdate<float>("courseProgress", -1.0f);
            user.courses.Add(progress);
        }
        EditorUtility.SetDirty(user);

        // Setup UI
        button.image.sprite = pauseSprite;
        title.text = course.name;
        NextVideo();
        NextImage();
    }

    // These two functions generate video and image from files 
    private void NextVideo()
    {
        player.clip = course.learnables[currentWordIndex].clip;
        // This loads first frame, so that it can be used as a sort-of preview for the video
        player.Play();
        player.Pause();
        // As the video will start paused -> show button
        Color col = button.image.color;
        col.a = 255;
        button.image.color = col;
    }

    // This doesn't work
    private void NextImage()
    {
        wordImage.sprite = course.learnables[currentWordIndex].image;
    }

    // Activate by pressing the center of the screen
    public void Pause()
    {
        if (!player.isPlaying)
        {
            // Play video and hide button
            player.Play();
            Color col = button.image.color;
            col.a = 0;
            button.image.color = col;
        }
        else
        {
            // Pause video and show button
            player.Pause();
            Color col = button.image.color;
            col.a = 255;
            button.image.color = col;
        }
    }

    // Press next-sign button for next word
    public void NextSign()
    {
        // Goto the next word/letter
        currentWordIndex++;

        // TODO: fix correct word count
        correctWords++;
        progress.AddOrUpdate<float>("courseProgress", (float)correctWords / (float)maxWords);
        EditorUtility.SetDirty(user);

        // Update UI if course is not finished yet
        if (currentWordIndex < maxWords)
        {
            NextVideo();
            NextImage();
        }
        // Finish course and record progress
        else
        {
            FinishCourse();
        }
    }

    public void FinishCourse()
    {
        // TODO: update progress (maybe this can also be at the `NextSign()`-method)
        progress.AddOrUpdate<float>("courseProgress", correctWords / maxWords);
    }
}
