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

/// <summary>
/// This class is dedicated to the camera and other actions that later could be connected to it, like the feedback.
/// It is responsible for finding working cameras, displaying them and being able to toggle between them.
/// This class also holds temporary code to display feedback via a button.
/// </summary>
public class Webcam : MonoBehaviour
{
    
    /// <summary>
    /// Index to indicate which camera is being used
    /// </summary>
    int camdex = 0;

    
    /// <summary>
    /// This texture is used as an intermidiary between the camera output and the display image
    /// </summary>
    WebCamTexture tex;

    /// <summary>
    /// Reference to the RawImage that will display the video
    /// </summary>
    public RawImage display;
    
    /// <summary>
    /// Reference to the button that is currently used to test the feedback-display
    /// </summary>
    public Button feedback;

    /// <summary>
    /// This is a reference to the PANEL that holds the feedbackwindow
    /// </summary>
    public GameObject popup;
    
    /// <summary>
    /// This is a reference to the textfield that holds the part of the feedback-window that will change: bad/good/excellent
    /// </summary>
    public TextMeshProUGUI dynamic;

    /// <summary>
    /// This function is called at the start of the frame.
    /// It inactivatis the popup, finds a webcam to use and links it via the WebcamTexture to the display RawImage.
    /// </summary>
    void Awake()
    {
        popup.SetActive(false);
        if (WebCamTexture.devices.Length > 0) { 
            WebCamDevice device = WebCamTexture.devices[camdex];
            tex = new WebCamTexture(device.name);
            display.texture = tex;

            tex.Play();
        }
    }

    /// <summary>
    /// So long as there are cameras to use, you swap the camera you are using to another in the list.
    /// </summary>
    public void SwapCam()
    {
        if (WebCamTexture.devices.Length > 0)
        {
            // Stop the old camera
            // If there was no camera playing before, then you dont have to reset the texture, as it wasn't assigned in the first place.
            if (tex.isPlaying) { 
                display.texture = null;
                tex.Stop();
                tex = null;
            }
                // Find the new camera
                camdex += 1;
                camdex %= WebCamTexture.devices.Length;
            // Start the new camera
            WebCamDevice device = WebCamTexture.devices[camdex];
            tex = new WebCamTexture(device.name);
            display.texture = tex;

            tex.Play();
        }
    }

    /// <summary>
    /// The notmal sceneChanger cannot be used here since the camera also needs to be stopped.
    /// This extra functionality is implemented in this function
    /// </summary>
    /// <param name="sceneName"> The path for the scene you want to travel to, assuming root-directory is Assets</param>
    public void LoadScene(string sceneName)
    {
        display.texture = null;
        tex.Stop();
        tex = null;

        SceneManager.LoadScene(sceneName);
    }

    /// <summary>
    /// This function toggles between inactivity and activity for the popup panel.
    /// This will be changed later when the model gets integrated, probably being timed to dissapear.
    /// </summary>
    public void Show_feedback()
    {
        if (popup.activeSelf)
        {
            dynamic.text = "";
            popup.SetActive(false);
            return;
        }
        double index = UnityEngine.Random.value;
        if (index < 0.5)
        {
            dynamic.text = "Poor";
        }
        else if (index > 0.8)
        {
            dynamic.text = "Excellent";
        }
        else
        {
            dynamic.text = "Good";
        }
        popup.SetActive(true);
    }
}
