using System.Collections;
using System.Collections.Generic;
using System.IO;
using RenderHeads.Media.AVProVideo;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class InfoWindow : MonoBehaviour
{
    public Text titlestr;
    public Text infoStr;
    public Text infoStr2;
    public RawImage img;
    public RawImage img2;
    public GameObject mp4;

    public MediaPlayer mp;

    public void init (string info,string url,string info2,int index,string title,string application)
    {
        StopAllCoroutines();
        idx=0;
        mp.Stop();
        titlestr.text = TabListManager.Instance.GetTextByKey(title);
        infoStr.text = TabListManager.Instance.GetTextByKey(application);

        infoStr2.text = TabListManager.Instance.GetTextByKey(info)+"\n\n\n"+TabListManager.Instance.GetTextByKey(info2);
        if(url.Contains("mp4"))
        {
            mp4.SetActive(true);
            img2.gameObject.SetActive(false);
            mp.OpenMedia(MediaPathType.RelativeToStreamingAssetsFolder,url,true);
        }
        else
        {
            mp4.SetActive(false);
            img2.gameObject.SetActive(true);
        }

            // 构建文件路径
        filePath = Application.streamingAssetsPath+"/DeMa/png/"+index;

        img.gameObject.SetActive(true);
        Debug.Log("文件路径==》"+filePath);
        #if UNITY_EDITOR
            string fileListPath = Path.Combine(Application.streamingAssetsPath, "DeMa/png/"+index);
            // 获取 StreamingAssets 文件夹中的所有文件
            files= Directory.GetFiles(fileListPath, "*.png"); // 假设图片是 PNG 格式
            if(files.Length<=0)
            {
                img.gameObject.SetActive(false);
            }
            else
            {
            StartCoroutine(LoadImages());

            }
        Debug.Log("文件路径llllll==》"+files.Length);
        #else
            StartCoroutine(loadImg());
        #endif

    }  
    string filePath ;
    string[] files ;
    int idx=0;
    private void OnDisable() {
        
       // infoStr2.transform.parent.gameObject.SetActive(false);
    }
    Texture2D texture ;
    IEnumerator loadImg()
    {
        // 构建文件路径
        string f = filePath+"/"+idx+".png";
        Debug.Log(" to load image: " + f);

        // 使用 UnityWebRequest 加载图片
        using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(f))
        {
            yield return request.SendWebRequest();

            if (request.result == UnityWebRequest.Result.ConnectionError || request.result == UnityWebRequest.Result.ProtocolError)
            {
                if(idx==0)
                {
                    img.gameObject.SetActive(false);

                }
                else
                {
                    idx=0;
                    StartCoroutine(loadImg()); 

                }
                Debug.LogError("Failed to load image: " + request.error);
            }
            else
            {
                img.gameObject.SetActive(true);
                if(texture!=null)
                {
                    Destroy(texture);
                }
                // 获取 Texture2D
               texture = DownloadHandlerTexture.GetContent(request);

                // 显示图片
               img.texture = texture;

                // 等待一段时间再加载下一张图片
                yield return new WaitForSeconds(3.0f);
                idx++;
                StartCoroutine(loadImg()); 

            }
        }
    }

    IEnumerator LoadImages()
    {
        // 构建文件路径
        string filePath = files[idx];

        if(texture!=null)
        {
            Destroy(texture);
        }
        // 读取文件字节数据
        byte[] fileData = File.ReadAllBytes(filePath);

        // 创建 Texture2D 并加载图片数据
        texture = new Texture2D(2, 2);
        texture.LoadImage(fileData);


        // 显示图片
        img.texture = texture;

        // 等待一段时间再加载下一张图片
        yield return new WaitForSeconds(3.0f);
        idx++;
        if(idx>=files.Length)
        {
            idx=0;
        }
        StartCoroutine(LoadImages());
    }
}