using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static UnityEngine.Rendering.DebugUI;

public class rebackListItem : MonoBehaviour
{
    //public Texture2D StyleImage;
    public GameObject value;
    private static Color[] allColors = null;
    private static int width = 0;
    private static int height = 0;
    bool isInit = false;
    private bool valueIsFirst = true;
    public Color showColorValue = Color.white;
    public string imageid = "";
    public int listId = -1;
    // Start is called before the first frame update
    void Start()
    {
        initMember();
        
    }
    void initMember()
    {
        if(isInit == false)
        {
            isInit = true;
            if(allColors == null)
            {
                Texture2D texture2D = GetComponent<Image>().sprite.texture;
                allColors = texture2D.GetPixels();
                width = texture2D.width;
                height = texture2D.height;
            }
            string path = utilsTools.getWritableByPath("gameData/cc_4mMEugNomv_ytuQ_pictureColorThn4.jpg");
            utilsTools.loadLocalImage(this, path, (string id, string url, bool isSuccess, System.Object data) =>
            {
                if (isSuccess)
                {
                    Texture2D texture2D = data as Texture2D;
                    setData(texture2D);
                }
                return 0;
            });
        }
    }
    public void setData(Texture2D texture2d)
    {
        Image image = value.GetComponent<Image>();
        if(image != null)
        {
            if(valueIsFirst == false)
            {

                Destroy(image.sprite);
            }
            valueIsFirst = false;
            int width1 = texture2d.width;
            int height1 = texture2d.height;
            if(width1 < width)
            {
                float bet = (width / (float)width1);
                var temp = utilsTools.ScaleTexture(texture2d, width, (int)(height1 * bet));
                Destroy(texture2d);
                setData(temp);
                return;
            }
            int startY = height1 / 2 - height / 2;
            if(width1.Equals(height1) == false)
            {
                startY = height1 - height - height1 / 10;
            }
            Color[] nowTextureColors = texture2d.GetPixels(width1 / 2 - width / 2, startY, width, height);
            Texture2D texture2d1 = new Texture2D(width, height,TextureFormat.RGBA32,false);
            Color[] nowText001 = texture2d1.GetPixels();
            Color delColor = new Color(0, 0, 0, 0);
            //´æÔÚÃ«±ß?
            for (int x1 = 0; x1 < width; x1++)
            {
                for (int y1 = 0; y1 < height; y1++)
                {
                    int idx = y1 * width + x1;
                    Color color = allColors[idx];
                    bool isDel = false;
                    if(color.Equals(showColorValue) == false || color.a.Equals(0))
                    {
                        isDel = true;
                    }
                    else if(color.a > 0.2f)
                    {
                        if(x1 > 0 && x1 < (width - 1))
                        {
                            if(y1 > 0 && y1 < (height - 1))
                            {
                                Color color1 = allColors[y1 * width + x1 + 1];
                                if (color1.Equals(showColorValue) == false || color1.a.Equals(0)) isDel = true;
                                color1 = allColors[y1 * width + x1 - 1];
                                if (color1.Equals(showColorValue) == false || color1.a.Equals(0)) isDel = true;
                                color1 = allColors[y1 * width + width + x1 ];
                                if (color1.Equals(showColorValue) == false || color1.a.Equals(0)) isDel = true;
                                color1 = allColors[y1 * width - width + x1];
                                if (color1.Equals(showColorValue) == false || color1.a.Equals(0)) isDel = true;
                            }
                        }
                    }
                    if(isDel == true)
                    {
                        nowText001[idx] = delColor;
                    }
                    else
                    {
                        Color color1 = nowTextureColors[idx];
                        color1.a = 1f;
                        nowText001[idx] = color1;
                    }
                }
            }
            Destroy(texture2d);
            texture2d1.SetPixels(nowText001);
            image.sprite = Sprite.Create(texture2d1, new Rect(0, 0, texture2d1.width, texture2d1.height), new Vector2(0.5f, 0.5f));
            RectTransform rectTransform = GetComponent<RectTransform>();
            Vector2 bet1 = new Vector2(rectTransform.sizeDelta.x / 164, rectTransform.sizeDelta.y / 164);
            RectTransform rectTran1 = value.GetComponent<RectTransform>();
            rectTran1.sizeDelta = new Vector2(rectTran1.rect.width * bet1.x,rectTran1.rect.height * bet1.y);
            texture2d1.Apply();
        }
    }
    public void onClickHandler()
    {
        //string path = utilsTools.getWritableByPath("gameData/cc_4mMEugNomv_ytuQ_pictureColorThn4.jpg");
        //utilsTools.loadLocalImage(this, path, (string id, string url, bool isSuccess, System.Object data) =>
        //{
        //    if (isSuccess)
        //    {
        //        Texture2D texture2D = data as Texture2D;
        //        setData(texture2D);
        //    }
        //    return 0;
        //});
        utilsTools.sendEventObjectParams("rebackListItemClick", imageid, listId);
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
