using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

public class imageItem : MonoBehaviour
{
    netLableImageItem netdata = null;
    bool netImageIsOk = false;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void onClick()
    {
        if (netImageIsOk)
        {
            List<UnityEngine.Object> list = new List<UnityEngine.Object>();
            list.Add(transform.Find("img").GetComponent<Image>());
            utilsTools.sendEventUObjects("selectImageItem", list);
        }
        else
        {
            Debug.Log("数据还没准备号请稍后再试");
        }
    }
    private string getTextFlgData()
    {
        //if (netdata == null) return "";
        //string[] tags = netdata.tags.Split(",");
        //for (int i = 0; i < tags.Length; i++)
        //{
        //    if (tags[i].Equals("Special"))
        //    {
        //        return "Special";
        //    }
        //    else if (tags[i].Equals("Bonus"))
        //    {
        //        return "Bonus";
        //    }
        //}
        return "";
    }
    public void setData(netLableImageItem data)
    {
        netImageIsOk = false;
        netdata = data;
        var imgflg = transform.Find("imgflg").GetComponent<Image>();
        var textflg = transform.Find("extflg").GetComponent<TextMeshProUGUI>();
        //if (data.isNew)
        //{
        //    textflg.text = "";
        //    imgflg.gameObject.SetActive(true);
        //    utilsTools.loadLocalImage(this, imgflg, "Resources/19.png");
        //}
        //else
        //{
        //    imgflg.gameObject.SetActive(false);
        //    string[] tags = data.tags.Split(",");
        //    string lableText = getTextFlgData();
        //    textflg.text = lableText;
        //}
        
        string localpath = "temps/net_" + data.id.ToSafeString() + ".png";
        string url = "http://127.0.0.1:6921/tempImgs/" + data.id.ToSafeString() + ".png";
        utilsTools.httpDOWNIMG(this, url, "Resources/" + localpath, (string id, string url, bool isOk, System.Object imgdata) =>
        {
            onLoadImageHandler(url,localpath,isOk, imgdata);
            return 0;
        });
    }
    public void onLoadImageHandler(string url,string localpath, bool isOk,System.Object imgdata)
    {
        if (isOk == false)
        {
            utilsTools.httpDOWNIMG(this, url, "Resources/" + localpath, (string id, string url, bool isOk, System.Object imgdata) =>
            {
                onLoadImageHandler(url, localpath, isOk, imgdata);
                return 0;
            });
            return ;
        }
        Texture2D texture2d = imgdata as Texture2D;
        Sprite sprite = Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0.5f));
        Image image = transform.Find("img").GetComponent<Image>();
        image.sprite = sprite;
        netImageIsOk = true;
    }
}
