using System;
using System.Collections;
using System.Collections.Generic;
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;
    void initMember()
    {
        if(effectImg == null)
        {
            effectImg = transform.Find("circle").GetComponent<gameGroupEffect>();
        }
    }
    public bool IsSelect
    {
        set {
            initMember();
            isSelect = value;
            effectImg.LangValue = isSelect ? selectLang : 0;            
            //transform.Find("finish").gameObject.SetActive(!value);
            //transform.Find("minBar").gameObject.SetActive(value);
            transform.Find("nowSelectFlg").gameObject.SetActive(isSelect);
            
        }
        get
        {
            return isSelect;
        }
    }
    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 = 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<Text>().text = value;
        }
        get { return idName; }
    }
    public Color UseColor
    {
        set
        {
            initMember();
            useColor = value;

            var textColor = Color.white;
            if((useColor.r > 0.75 && useColor.g > 0.75) || (useColor.g > 0.75 && useColor.b > 0.75) || (useColor.r > 0.75 && useColor.b > 0.75))
            {
                textColor = Color.black;
            }
            transform.Find("idname").GetComponent<Text>().color = textColor;
            float h;
            float s;
            float v;
            Color.RGBToHSV(useColor, out h, out s, out v);
            if(v > 0.6)
            {
                v = v - 0.5f;
            }else if(v < 0.45f)
            {
                v = v + 0.5f;
            }
            else
            {
                v = v - v / 3;
            }
            Color color1 = Color.HSVToRGB(h, s, v);
            effectImg.color2 = useColor;
            effectImg.color1 = color1;
        }
        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);
        }
    }
}
