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

public class colorItem : MonoBehaviour
{
    [SerializeField]
    private bool isSelect = false;
    [SerializeField]
    private float nowProgress = 0;
    [SerializeField]
    private string idName = "";
    [SerializeField]
    private Color useColor = new Color();
    public int index = 0;
    public float selectLang = 0.05f;
    private gameGroupEffect effectImg = null;
    private Vector3 normalScale = Vector3.one;
    private List<Vector2> initScaleSize = new List<Vector2>();
    private float offset = 0;
    
    void initMember1()
    {
        if(effectImg == null)
        {
            var selfRectTrans = GetComponent<RectTransform>();
            normalScale = transform.localScale * 0.85f;
            transform.localScale = normalScale;
            Vector2 listViewTempCellSize = utilsTools.getGDataByKey<Vector2>("listViewTempCellSize", Vector2.zero);
            var size1 = listViewTempCellSize;// new Vector2(selfRectTrans.sizeDelta.x * selfRectTrans.localScale.x, selfRectTrans.sizeDelta.y * selfRectTrans.localScale.y);
            for (int i = 0; i < transform.childCount; i++)
            {
                var temp = transform.GetChild(i).GetComponent<RectTransform>();
                temp.localScale = new Vector3(temp.localScale.x * size1.x / 125f / 1.2f,temp.localScale.y * size1.y/125f / 1.2f);
                initScaleSize.Add(new Vector2(temp.localScale.x,temp.localScale.y));
            }
            effectImg = transform.Find("circle").GetComponent<gameGroupEffect>();
            
        }
    }
    void initMember()
    {
        if(effectImg == null)
        {
            Vector2 listViewTempCellSize = utilsTools.getGDataByKey<Vector2>("listViewTempCellSize", Vector2.zero);
            Vector2 listViewContextCellSize = utilsTools.getGDataByKey<Vector2>("listViewContextCellSize", Vector2.zero);
            offset = (listViewContextCellSize.y - listViewTempCellSize.y)/2f;
            effectImg = transform.Find("circle").GetComponent<gameGroupEffect>();
            normalScale = transform.localScale * 0.85f;
            transform.localScale = normalScale;
        }
    }
    public bool IsSelect
    {
        set {
            initMember();
            isSelect = value;
            
            effectImg.LangValue = isSelect ? selectLang : 0;
            effectImg.pause = !isSelect;
            //transform.Find("finish").gameObject.SetActive(!value);
            //transform.Find("minBar").gameObject.SetActive(value);
            transform.Find("nowSelectFlg").gameObject.SetActive(isSelect);
            
        }
        get
        {
            return isSelect;
        }
    }
    public void switchPlayAnimation()
    {
        if (transform == null) return;
        RectTransform rectTransform = (RectTransform)transform;
        float x = transform.localPosition.x;
        if (isSelect)
        {
            transform.localPosition = new Vector2(x, 0 - (transform.localScale.y * rectTransform.rect.height) / 2 - offset);
            transform.localScale = normalScale;
            ActionManages.addMoveByScale(gameObject, normalScale * 1.2f, 0.25f);
        }
        else
        {
            transform.localScale = normalScale * 1.2f;
            transform.localPosition = new Vector2(x, 0 - (transform.localScale.y * rectTransform.rect.height) / 2 - offset);
            ActionManages.addMoveByScale(gameObject, normalScale, 0.25f);
        }
        
        
        
        ActionManages.addCallBackByLen(gameObject, 1000, 0.25f, (float _) =>
        {
            transform.localPosition = new Vector2(x, 0 - (transform.localScale.y * rectTransform.rect.height) / 2 - offset);
        }, (GameObject gobject, int id) =>
        {
            float a = transform.localScale.y * rectTransform.rect.height;
            transform.localPosition = new Vector2(x, 0 - (transform.localScale.y * rectTransform.rect.height) / 2 - offset);
        });
    }
    public void switchPlayAnimation1()
    {
        if (transform == null) return;

        RectTransform rectTrans = transform.GetComponent<RectTransform>();
        //rectTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, 0.5f);
        //rectTrans.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 0.5f);
        if (isSelect)
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                transform.GetChild(i).localScale = initScaleSize[i];
            }
            ActionManages.addCallBackByLen(gameObject, 0.18f, 0.2f, (float value) =>
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    transform.GetChild(i).localScale = new Vector2(initScaleSize[i].x + value, initScaleSize[i].y + value);
                }
            });

        }
        else
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                transform.GetChild(i).localScale = new Vector2(initScaleSize[i].x + 0.18f,initScaleSize[i].y + 0.18f);
            }
            ActionManages.addCallBackByLen(gameObject, -0.18f, 0.2f, (float value) =>
            {
                for (int i = 0; i < transform.childCount; i++)
                {
                    transform.GetChild(i).localScale = new Vector2(initScaleSize[i].x + value, initScaleSize[i].y + value);
                    
                }
            });
        }
        
    }
    public float NowProgress
    {
        set {
            initMember();
            if (value <= 0) 
                value = 0f; 
            else if (value >= 0.99999998) 
                value = 1.0f;
            if (utilsTools.isEqualByValue(value, 1, 0.000001f))
            {
                value = 1.0f;
            }
            nowProgress = value;
            bool isend = this.isFinish();
            effectImg.Height = 1.0f - nowProgress;
            transform.Find("idname").gameObject.SetActive(!isend);
            if (isend)
            {
                effectImg.gameObject.SetActive(false);
            }
            else
            {
            }
        }
        get
        {
            return nowProgress;
        }
    }
    public string IdName
    {
        set { 
            idName = value;

            transform.Find("idname").GetComponent<TextMeshProUGUI>().text = value;
        }
        get { return idName; }
    }
    public Color UseColor
    {
        set
        {
            initMember();
            useColor = value;
            float h;
            float s;
            float v;
            Color.RGBToHSV(useColor, out h, out s, out v);
            v = v * 100;
            s = s * 100;
            if(v < 75)
            {
                v = v + 20;
            }
            else
            {
                v = v - 4;
                if(s < 75)
                {
                    s = s + 20;
                }
                else
                {
                    s = s - 10;
                    v = v - 10;
                }
            }
            s = s / 100f;
            v = v / 100f;
            Color color1 = Color.HSVToRGB(h, s, v);
            effectImg.color2 = color1;
            effectImg.color1 = useColor;
        }
        get
        {
            return useColor;
        }
    }
    // Start is called before the first frame update
    void Start()
    {
        initMember();
        //IsSelect = isSelect;
        //IdName = idName;
        //UseColor = useColor;
        //NowProgress = nowProgress;
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public bool isFinish()
    {
        return utilsTools.isEqualByValue(nowProgress,1.0f,0.0000002f);
    }
    public void onClickAreaGroup()
    {
        if (isSelect == false && isFinish() == false)
        {
            utilsTools.sendEventObjectParams("switchGameAreaGroupId", index);
        }
    }
    public void updateOneUI()
    {
        effectImg.updateTexture();
    }
    public void resetSize(float x,float y)
    {
        //RectTransform rectTransform = GetComponent<RectTransform>();
        //rectTransform.localScale = new Vector3 (100f/x,100f/y, 1);
        int cout = transform.childCount;
        for (int i = 0; i < cout; i++)
        {
            //transform.GetChild(i).localScale= new Vector3(x/100f, y/100f, 1);
        }
    }
}
