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

class DotInfo
{
    public float x = 0;
    public float y = 0;
    public float r;
    public float speed;//运动速度
    public float angle = 0;//运动方向
    private Vector2 normal;
    private float leijiLen = 0;
    public Color ColorOffset;
    public int id = 0;
    public void runByDt(float dt)
    {
        leijiLen = leijiLen + dt * speed;
        //normal x y == +- 1之间
        x = normal.x * leijiLen;
        y = normal.y * leijiLen;

    }
    public bool IsOutArea(Vector2 size)
    {
        if ((x + r) > size.x/2 || (x - r) < size.x / -2)
        {
            return true;
        }
        if ((y + r) > size.y/2 || (y - r) < size.y / -2)
        {
            return true;
        }
        return false;
    }
    public bool Contains(float x1,float y1)
    {
        if(x1 < x - r || y1 < y - r) return false;
        if(x1 > x + r || y1 > y + r) return false;
        return true;
    }
    public void setAngle(float runAngle)
    {
        angle = runAngle % 360f;
        normal = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle));
        leijiLen = 0;
    }
}
public class GameClickPosEffect : MonoBehaviour
{
    public float centerX = 0f;
    public float centerY = 0f;
    public float runR = 200f;
    public float tim = 0.25f;
    public float maxDotR = 45f;
    public Color baseColor = Color.white;
    bool isinit = false;
    List<DotInfo> dotInfos = new List<DotInfo>();
    void Start()
    {
        initMember();
    }
    public bool initMember()
    {
        if(isinit)
        {
            return false;
        }
        isinit = true;
        Image image = GetComponent<Image>();
        if(image == null)
        {
            image = transform.AddComponent<Image>();
        }
        resetSize(image);
        return true;
    }
    private void resetSize(Image img)
    {
        RectTransform rectTransform = GetComponent<RectTransform>();
        float width = (runR + maxDotR) * 2;
        float height = width;
        int iwidth = Convert.ToInt32(width);
        int iheight = Convert.ToInt32(height);
        rectTransform.sizeDelta = new Vector2(width,height);
        Texture2D texture2D = new Texture2D( iwidth,iheight);
        Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, iwidth,iheight), new Vector2(0.5f, 0.5f));
        var oldSprite = img.sprite;
        img.sprite = sprite;
        Color delColor = new Color(0, 0, 0, 0);
        for (int x = 0; x < iwidth; x++)
        {
            int x1 = Convert.ToInt32(x - (runR + maxDotR));
            for (int y = 0; y < iheight; y++)
            {
                texture2D.SetPixel(x1, Convert.ToInt32(y - (maxDotR + runR)), delColor);
            }
        }
        texture2D.Apply();
        Destroy(oldSprite);
    }
    public void player()
    {
        player1(6, tim);
    }
    public void player1(int cout,float maxTime)//最大运动长度
    {
        gameObject.SetActive(true);
        if(maxTime < 0.25f)
        {
            maxTime = 0.25f;
        }
        dotInfos.Clear();
        System.Random r = new System.Random();
        for (int i = 0; i < cout; i++)
        {
            DotInfo dotInfo = new DotInfo();
            dotInfo.x = 0f;
            dotInfo.y = 0f;
            dotInfo.r = r.Next(8,Convert.ToInt32(maxDotR));
            Color color1 = new Color(baseColor.r, baseColor.g, baseColor.b);
            float a = (float)(r.NextDouble() + 0.25f);
            if (a >= 1.0f)
                a = a - 1.0f;
            color1.a = a;
            dotInfo.ColorOffset = color1;
            float dt1 = maxTime;
            dt1 = dt1 - 0.003f * i % 5;
            dotInfo.speed = runR/dt1;
            dotInfo.setAngle(r.Next(0, 360));
            dotInfo.id = r.Next();
            dotInfos.Add(dotInfo);
        }
        
    }
    void Update()
    {
        if (initMember() == true)
        {
            return;
        }
        if (dotInfos.Count > 0)
        {
            Vector2 vector2 = new Vector2(runR * 2, runR * 2);
            List<int> removeIds = new List<int>();
            int width = (int)((runR + maxDotR) * 2);
            int height = (int)((runR + maxDotR) * 2);
            Image image = GetComponent<Image>();
            Texture2D texture2D = image.sprite.texture;
            //如何保证只修改一次
            Color delColor = baseColor;
            delColor.a = 0;
            Dictionary<int, Color> colors= new Dictionary<int, Color>();
            for (int i = 0; i < dotInfos.Count; i++)
            {
                var dotInfo = dotInfos[i];
                float rFang = dotInfo.r * dotInfo.r;
                for (float x = 0 - dotInfo.r; x < dotInfo.r; x++)
                {
                    float x1 = dotInfo.x + x;
                    if (x1 < (0 - width/2) || x1 > width/2)
                        continue;

                    float xFang = x * x;
                    for (float y = 0 - dotInfo.r; y < dotInfo.r; y += 1f)
                    {
                        float y1 = dotInfo.y + y;
                        if (y1 < (0 - height/2) || y1 > height/2 )
                            continue;
                        if (xFang + y * y < rFang)
                        {
                            texture2D.SetPixel((int)x1 + width / 2, (int)y1 + width / 2, delColor);
                        }
                    }
                }
            }

            for (int i = 0; i < dotInfos.Count; i++)
            {
                var dot = dotInfos[i];
                dot.runByDt(Time.deltaTime);
                if (dot.IsOutArea(vector2))
                {
                    dot.IsOutArea(vector2);
                    removeIds.Add(dot.id);
                }
            }
            if (removeIds.Count > 0)
            {
                foreach (var id in removeIds)
                {
                    for (int i = 0; i < dotInfos.Count; i++)
                    {
                        if (dotInfos[i].id.Equals(id))
                        {
                            dotInfos.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
            int huizhidian = 0;
            int dt1 = 0;
            int dt2 = 0;
            for (int i = 0; i < dotInfos.Count; i++)
            {
                var dotInfo = dotInfos[i];
                float rFang = dotInfo.r * dotInfo.r;
                for (float x = 0-dotInfo.r;x < dotInfo.r; x++) {//小圆的 中心上下前后
                    float x1 = dotInfo.x + x;
                    if (x1 < 0 - width/2 || x1 > width/2)
                        continue;
                    float xFang = x * x;
                    for (float y = 0 - dotInfo.r; y < dotInfo.r; y += 1f)
                    {
                        float y1 = dotInfo.y + y;
                        if (y1 < (0 - height/2) || y1 > height/2)
                            continue;
                        if (xFang + y * y < rFang)
                        {
                            texture2D.SetPixel((int)x1+width/2, (int)y1+width/2, dotInfo.ColorOffset);
                            //huizhidian++;
                            if(dotInfo.x > 0)
                            {
                                dt1++;
                            }
                            else
                            {
                                dt2++;
                            }
                        }else if(dotInfo.x < -1)
                        {
                            if(x < 3 && x > -3 && y < 3 && y > -3 && dotInfo.r > 12)
                            {
                                Debug.Log("为十八小时");
                            }
                        }
                    }
                }
            }
            texture2D.Apply();
        }
        else
        {
            gameObject.SetActive(false);
        }
    }
}
