using Blue;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using LitJson;
using System.IO;
using UnityEngine;
using System.Collections.Generic;
using System.Collections;

/// <summary>
/// 获取点云文件(json、bytes)的Url下载地址命令
/// </summary>
public class PointFileGetUrlCommand : ICommand
{
    private string methodName = "/project/pointcloud";
    private int sceneID;
    private PointFileType fileType;

    /// <summary>
    /// 获取点云文件(json、bytes)的Url下载地址命令
    /// </summary>
    /// <param name="sceneId">场景ID</param>
    /// <param name="fileType">文件类型</param>
    public PointFileGetUrlCommand(int sceneId, PointFileType fileType)
    {
        this.sceneID = sceneId;
        this.fileType = fileType;
    }
    public void OnExcute()
    {
        this.GetService<IPointService>().GetPointFileDownloadUrl(methodName, sceneID.ToString(), fileType, GetPointFileUrlSuccess);
    }

    string PointName;
    private void GetPointFileUrlSuccess(string message)
    {
        Debug.Log(message);
        if (!string.IsNullOrWhiteSpace(message))
        {
            JObject jobject = JObject.Parse(message);
            if (jobject["code"].ToString() == "200")
            {
                Debug.Log("Hjj   message====>"+ message);
                    JsonData dataJ = JsonMapper.ToObject(message);
                    string downLoadPath = "";
                    if (fileType==PointFileType.bytes)
                    {
                        downLoadPath = dataJ["data"]["bytesFile"]["path"].ToString();

                    }
                    else
                    {
                        downLoadPath = dataJ["data"]["jsonFile"]["path"].ToString();

                    }

                    PointName = dataJ["data"]["bytesFile"]["name"].ToString() + "." + dataJ["data"]["bytesFile"]["ext"].ToString();
                 
                //  Debug.Log(" SetData " + updateTime);

              //  PointFileData PointFile = JsonConvert.DeserializeObject<PointFileData>(message);

              //  this.GetService<IPointService>().DownloadPointFile(PointFile.url, fileType);
              if(!string.IsNullOrWhiteSpace(downLoadPath))
                {
                    DownLoadMaterial data = new DownLoadMaterial();
                    data.downLoadPath = downLoadPath;
                    data.localLoadPath = Application.persistentDataPath + "/Map Data/" + Path.GetFileName(downLoadPath);
                    Debug.Log("DGJ    byte   " + data.downLoadPath);
                    data.updataTime = 0;// GameManager.Instance.m_SceneValue.updateTime;
                    data.type = "-1";
                    MsgHandler.AddListener(downLoadPath, HandleMsg);

                    DownloadResManager.Instance.DownLoad(data);
                }
                else
                {
                    Debug.LogError("当前场景未上传点云文件 ");
                }

               
            }
        }
    }
    private void HandleMsg(Msg msg)
    {
        Debug.Log("DGJ  GetPointFileUrlSuccess  HandleMsg");
        if (msg.Value!=null)
        if (fileType == PointFileType.bytes)
        {

            GameManager.Instance.StartCoroutine(loadmapend((byte[])msg.Value));
        }
          //  this.SendEvent(new LoadMapFileEvent() { fileName = PointName ,bytes = (byte[])msg.Value});
    }

    public static int mapct = 1000;
    public IEnumerator loadmapend(byte[] bytes)
    {
        mapct++;
        yield return GameManager.Instance.armap.LoadMap(bytes, mapct);
        GameManager.Instance.armap.enabled = true;
        this.SendCommand(new StartImmersalLocalizerCommand());
    }
}