using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using easyar;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using XRTool.Util;
using static easyar.ImageTargetController;

public class DemoManager : MonoBehaviour
{
    public TextMeshProUGUI timetext;
    public bool isinit;
    public bool isDianbiao;

    public GameObject AISelect;
   
    private void OnEnable()
    {
        AISelect.SetActive(true);
        isDianbiao = false;
      

    }

    public void jixushibie()
    {

        StartCoroutine(DownloadImage("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/MREdustry_100/DemoShiBieTu.jpg"));
        ChangeCameraSaoMiao.Instance.showDemoSaoMiao();
        imageTarget.SetActive(false);
        shibieGo.SetActive(false);
        TimerMgr.Instance.CreateTimer(() => {
            imageTarget.SetActive(true);
        }, 10f);
    }

    public void tuichushibie()
    {

        WindowsManager.Instance.show(WindowConfig.windowType.ProjectMain);
        WindowsManager.Instance.show(WindowConfig.windowType.Top, false);
    }

    public GameObject shibieGo;
    private void Update()
    {
        if(im&& isDianbiao)
        {

            if (imageTarget.activeSelf)
            {
                if (im)
                {
                    Destroy(im);
                    im = null;
                    shibieGo.SetActive(true);
                    timetext.text = DateTime.Now.ToString("yyyy 年 M 月 d 日 HH:mm");
                    ChangeCameraSaoMiao.Instance.close();
                }
            }
        }
    }

    // 协程方法,用于异步下载图片
    IEnumerator DownloadImage(string url)
    {
        // 使用UnityWebRequestTexture来获取图片纹理
        UnityWebRequest m_webrequest = UnityWebRequest.Get(url);
        yield return m_webrequest.SendWebRequest();

        // 检查下载是否成功
        if (m_webrequest.result != UnityWebRequest.Result.Success)
        {
            // 打印错误信息
            Debug.LogError("Failed to download image");
        }
        else
        {
            string giftopngpath = Application.persistentDataPath + "/DownLoadXR/DemoShiBieTu.png";
            // 从下载处理器获取纹理
            Texture2D tex = new Texture2D(1, 1);
            tex.LoadImage( m_webrequest.downloadHandler.data);
            tex.Apply();
            byte[] gifbyte = tex.EncodeToPNG();
            //文件流信息  
            //StreamWriter sw;  
            Stream sw;
            FileInfo file = new FileInfo(giftopngpath);
            try
            {

                if (!Directory.Exists(Application.persistentDataPath + "/DownLoadXR"))
                {
                    Directory.CreateDirectory(Application.persistentDataPath + "/DownLoadXR");
                }
                if (!file.Exists)
                {
                    sw = file.Create();
                    //如果此文件存在则打开  
                    //sw = file .Append();  
                    //如果此文件不存在则创建  

                    //以行的形式写入信息  
                    //sw.WriteLine(info);  
                    sw.Write(gifbyte, 0, gifbyte.Length);
                    sw.Close();
                    sw.Dispose();
                }
            }
            catch
            {
            }

            yield return new WaitForSeconds(1f) ;
            ImageFileSourceData data = new ImageFileSourceData();
            data.Name = "DemoShiBieTu";
            data.Path = giftopngpath;
            data.PathType = PathType.Absolute;
            data.Scale = 0.1f;
             im = imageTarget.AddComponent<ImageTargetController>();
            im.ImageFileSource = data;
            ARSaoTuManager.Instance.imageOnFind.LoadTarget(im);
            ARSaoTuManager.Instance.cameraDevice.Open();

        }
    }
    ImageTargetController im;
    public GameObject imageTarget;
    private void OnDisable()
    {

        if (im)
        {
            Destroy(im);
            im = null;
        }
        shibieGo.SetActive(false);
    }


    public void SeleckDianBiao()
    {
        isDianbiao = true;
        AISelect.SetActive(false);

        if (ChangeCameraSaoMiao.Instance)
        {
            StartCoroutine(DownloadImage("https://wx-model-1317557471.cos.ap-shanghai.myqcloud.com/MREdustry_100/DemoShiBieTu.jpg"));
            ChangeCameraSaoMiao.Instance.showDemoSaoMiao();
            imageTarget.SetActive(false);

            TimerMgr.Instance.CreateTimer(() => {
                imageTarget.SetActive(true);
            }, 10f);

        }
    }

    public void SeleckDevice()
    {
        ShowInfoTipManager.Instance.showTip("暂未开放,敬请期待");
        TimerMgr.Instance.CreateTimer(() => { ShowInfoTipManager.Instance.closeTip(); }, 2f);
    }

    public void GoHome()
    {

        WindowsManager.Instance.show(WindowConfig.windowType.ProjectMain);
    }

    public void GoDemo()
    {

      //  WindowsManager.Instance.show(WindowConfig.windowType.Demo);

    }

    public void GoRTC()
    {
        WindowsManager.Instance.show(WindowConfig.windowType.RTC);
    }
    public void GoDaoHang()
    {
        WindowsManager.Instance.show(WindowConfig.windowType.DaoHang);
    }
    public void GoXunJian()
    {
        WindowsManager.Instance.show(WindowConfig.windowType.XunJianLB);
    }
}