using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;

//这个节点必须在现实的下边
public class checkRootFirstRun : MonoBehaviour
{
    // Start is called before the first frame update
    public int r1 = 60;
    public int r2 = 20;
    public int width = 500;
    public int height = 500;
    public Color color = Color.white;
    private Color backColor = new Color(1.0f,1.0f,0.23f,1);
    private int oldR1 = 0;
    private int oldR2 = 0;
    private Image image = null;
    void Start()
    {
        onApply();
    }
    private void onUserYanJiaoJuXing()
    {
        Image image = transform.Find("Image").GetComponent<Image>();
        Texture2D texture = image.sprite.texture;
        Color[] colors = texture.GetPixels(0, 0, texture.width, texture.height);
        int cout = utilsTools.checkResetYuanJiao(colors, texture.width, texture.height, 100);
        if (cout > 0)
        {
            texture.SetPixels(0, 0, texture.width, texture.height, colors);
            texture.Apply();
        }
    }
    private void loopCheck(Transform trans)
    {

    }
    public void onApply()
    {
        bool removeOld = true;
        Texture2D texture2D = null;
        if(image == null)
        {
            image = transform.Find("Image").GetComponent<Image>();
            removeOld = false;
            texture2D = new Texture2D(width, height,TextureFormat.RGBA32,false);
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
            image.sprite = sprite;
        }
        else
        {
            texture2D = image.sprite.texture;
            if (texture2D.width !=  width || texture2D.height != height)
            {
                texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
                Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));
                Destroy(image.sprite);
                image.sprite = sprite;
                removeOld = false;
            }
        }
        if(oldR1 == 0)
        {
            removeOld = false;
        }
        Color[] colors = texture2D.GetPixels(0,0,texture2D.width,texture2D.height);
        
        if(removeOld)
        {
            utilsTools.Ring(width/2 -100, height/2+100, oldR1, oldR2, width, height, (int x, int y, int idx) =>
            {
                colors[idx] = backColor;
                return true;
            });
        }
        else
        {
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    int idx = y * width + x;
                    colors[idx] = backColor;
                }
            }
        }
        
        int t1 = utilsTools.getTotalMillisecond();
        int cout1 = 0;
        utilsTools.Ring( width / 2 - 100, height / 2 + 100, r1, r2, width,height,(int x,int y,int idx) => {
            colors[idx] = color;
            cout1++;
            return true;
        });
        int t2 = utilsTools.getTotalMillisecond();
        texture2D.SetPixels(colors);
        texture2D.Apply();
        oldR1 = r1;
        oldR2 = r2;
        string msg = string.Format("time:{0} width = {1} height = {2} r1 = {3} r2 = {4} cout = {5}", (t2 - t1), width, height, r1, r2,cout1);
        Debug.Log(msg);
    }
    private void Update()
    {
        if( utilsTools.IsPhone ==false )
        {
            return;
        }
    }
    
}
