using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using MyBT;

#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(NamedStereoscopicSlideshow))]
public class NamedStereoscopicSlideshowInspector : ComponentHandlerInspector {
}
#endif

[System.Serializable]
public class NamedStereoscopicSlideshow : ComponentHandler {

    public override string TypeLabel()
    {
        return "StereoSlideshow";
    }

    public override string ContentLabel()
    {
        UpdateComponent();
        if (photoTransition == null || photoTransition.imgPathList.Count == 0)
        {
            return "ImgsNotDefined";
        }
        string[] fullPath = photoTransition.imgPathList[0].Split('/');
        string filename = fullPath[fullPath.Length - 1];
        string fileNameWithoutExt = filename.Remove(filename.Length - 5);   // remove .jpeg
        string final = fileNameWithoutExt.Length > 10 ? fileNameWithoutExt.Substring(0, 10) : fileNameWithoutExt;   // first 10 characters
        return final;
    }

    public override void UpdateComponent()
    {
        base.UpdateComponent();
        photoTransition = GetComponent<PhotoTransition>();
        if (photoTransition == null)
        {
            Debug.Log($"NamedStereoscopicSlideshow.UpdateComponent photoTransition is null");
        }
    }

    public PhotoTransition photoTransition;
    float _timeLeft;
    public float totalTime = 5.0f;

    public override void Run(NodeState nodeState)
    {
        switch (nodeState)
        {
            case NodeState.FirstRun:
                photoTransition.Reset();
                _timeLeft = totalTime;
                StartCoroutine(photoTransition.SetUp());
                break;
            case NodeState.Running:
                _timeLeft -= Time.deltaTime;
                if (_timeLeft < 0)
                {
                    StartCoroutine(photoTransition.LoadNextImage());
                    _timeLeft = totalTime;
                }

                if (photoTransition.IsSlideshowFinished())
                {
                    Task.SetSucceeded();
                }
                
                break;
            case NodeState.Aborting:
                photoTransition.Reset();
                _timeLeft = totalTime;
                break;
        }
    }
}