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

public class gameGroupEffect : MonoBehaviour
{
    // Start is called before the first frame update
    public Color color1 = new Color(1, 1, 0,1);//颜色1
    public Color color2 = new Color(1, 0, 1,1);//颜色2
    private int state = 0;
    [SerializeField]
    private float height = 0.25f;//当前高度
    [SerializeField]
    private float langValue = 0.2f;//波峰-波谷的插值

    public float Height
    {
        get { return height; }
        set
        {
            if (value >= 1)
            {
                height = 1 + langValue * 2;
                return;
            }
            if (value <= 0)
            {
                height = 0 - langValue * 2;
                return;
            }
            height = value;
        }
    }

    public float LangValue
    {
        get { return langValue; }
        set
        {
            if (value < 0 || value > 1) return;
            langValue = value;
            Height = height;
        }
    }
    public short radius = 100;//显示的圆形大小
    private Texture2D texture2D = null;
    private float nowOffsetX = 0;
    public float speed = 0.2f;//更新频率
    public bool pause = true;
    void initMembers()
    {
        if(texture2D == null)
        {
            speed = speed * 4f;
            Image image = GetComponent<Image>();
            RectTransform rectTransform = GetComponent<RectTransform>();
            rectTransform.sizeDelta = new Vector2(radius * 2, radius * 2);
            texture2D = new Texture2D(radius * 2, radius * 2);
            updateTexture(ref texture2D);
            Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, radius * 2, radius * 2), new Vector2(0.5f, 0.5f));
            var oldSprite = image.sprite;
            image.sprite = sprite;
            texture2D.Apply();
            GameObject.Destroy(oldSprite);
            oldSprite = null;
            state = 1;
        }
    }
    void Start()
    {
        try
        {
            initMembers();
        }
        catch (Exception e)
        {
            Debug.Log(e);
        }

    }
    public bool updateTexture()
    {
        initMembers();
        updateTexture(ref texture2D);
        texture2D.Apply();
        return true;
    }
    bool updateTexture(ref Texture2D texture2D)
    {
        Color colorEmpty = new Color(0, 0, 0, 0);
        float qujian = langValue * radius * 2;
        float height1 = height * radius * 2;
        nowOffsetX = (nowOffsetX + speed) % 1080;
        //边界值问题 
        for (int x = 0 - radius; x < radius; x++)
        {
            double huduValue = ((x + this.nowOffsetX) % 360 * Math.PI) / 180.0;
            double y1 = Math.Sin(huduValue);//-1 ---- +1
            double fenge = height1 - qujian/2 + y1 * qujian/2;
            for (int y = 0 - radius; y < radius; y++)
            {
                if ((x * x + y * y) < (radius * radius)){
                    if(langValue == 0)
                    {
                        if((y + radius) > height1)
                        {
                            texture2D.SetPixel(x + radius, y + radius, color1);
                        }
                        else
                        {
                            texture2D.SetPixel(x + radius, y + radius, color2);
                        }
                    }
                    else
                    {
                        if((y+radius) > fenge)
                        {
                            texture2D.SetPixel(x + radius, y + radius, color1);
                        }
                        else
                        {
                            texture2D.SetPixel(x + radius, y + radius, color2);
                        }
                    }
                }
                else
                {
                    texture2D.SetPixel(x+radius, y+radius, colorEmpty);
                }
            }
        }
        return true;
    }
    private void FixedUpdate()
    {
        initMembers();
        state++;
        if(state % 4 == 0) {
            state = 0;
            if (pause == false && updateTexture(ref texture2D) == true)
            {
                texture2D.Apply();
            }
        }   
    }
}
