using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using Agora.Rtc.LitJson;
using UnityEngine;
using UnityEngine.UI;

public class UpLoadmageItem : MonoBehaviour
{
    public string path;
    string uuid ;
    public  JsonData fildData ;
    public void init(Texture2D texture2D,string str)
    {
        this.transform.GetComponent<RawImage>().texture = texture2D;


        string filePath = str;
        string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
         uuid = fileNameWithoutExtension+"_"+GetFormattedDateTime();// Guid.NewGuid().ToString();
        string fileName = uuid+".png";
        string objecName = "workorder/"+int.Parse(ChuLiItemManager.ChooseData["id"].ToString())+"/";
        // 文件预上传
        JsonData jData = new JsonData();
        byte[] data = texture2D.EncodeToPNG();
        jData["size"] = data.Length;
        jData["ext"] = "jpg";
        jData["name"] = fileName;

        JsonData fileData = new JsonData();
        fileData["filePath"] = filePath;
        fileData["fileSize"] = data.Length;
        fileData["fileName"] = fileName;
        // project /{ projectId}/ inspection /{ projectInspectionId}/{ taskId}
        fileData["objectName"] = objecName;

        fildData = fileData;
        this.path = objecName+fileName;
    }
    string GetFormattedDateTime()
    {
        DateTime now = DateTime.Now;
        return now.ToString("yyyyMMddHHmm");
        // 或者更明确的写法:
        // return $"{now.Year}{now.Month:D2}{now.Day:D2}{now.Hour:D2}{now.Minute:D2}";
    }
    public void startUpLoad(Action<bool> uploadcallback)
    {
        // 文件上传
        DownLoadXRManager.PutFile(fildData["filePath"].ToString(), fildData["fileName"].ToString(), fildData["objectName"].ToString() + fildData["fileName"].ToString(),uuid, int.Parse(fildData["fileSize"].ToString()), msg =>
        {
            uploadcallback.Invoke(true);
            Debug.Log(" 文件上传成功 " + msg);
        });
           
    }

    public void delete()
    {
        BaoGaoUpLoadManager.Instance.itemlist.Remove(this);
        Destroy(this.transform.GetComponent<RawImage>().texture);
        Destroy(this.gameObject);
    }
}