using System.Collections;
using ColorUiTools;
using UnityEngine;
using UnityEngine.UI;

public class GUIBox : MonoBehaviour
{

    public Image[] imagesWidth;
    public Image[] imagesHeight;

    private Vector2 initVector2;

    public ColorPicker colorPicker;

    public Slider sliderObj;

    public Text colorPickerBtnText;

    public Color oldColor;
    public float oldSliderValue;

    float remainingTime = 0;//记录的剩余时间
    bool bCanSave = false;
    float totalTime = 3f;//总共时间s


    public GameObject CrossHairImage;



    void Awake()
    {
        oldSliderValue = PlayerPrefs.GetFloat("GUIBoxSliderValue", 0);
        oldColor = new Color(
         PlayerPrefs.GetFloat("GUIBoxColor_R", 1f),
         PlayerPrefs.GetFloat("GUIBoxColor_G", 1f),
         PlayerPrefs.GetFloat("GUIBoxColor_B", 1f),
         PlayerPrefs.GetFloat("GUIBoxColor_A", 1f));

        Debug.Log("oldSliderValue 读取值：" + oldSliderValue);
        Debug.Log("oldColor 读取值：" + oldColor);

        CrossHairImage.SetActive(PlayerPrefs.GetInt("CrossHairImageActive", 1) == 1);
    }

    void Start()
    {

        colorPicker?.onPicker.AddListener(color =>
        {

            changeColor(color);
            if (color != oldColor)
            {
                Debug.Log(color);
                oldColor = color;
                remainingTime = totalTime;
                bCanSave = true;

            }

        });
        if(colorPicker) colorPickerBtnText.text = colorPicker.gameObject.activeSelf ? "关闭选择颜色" : "开启选择颜色";
       
        initVector2 = new Vector2(
            imagesWidth[0].GetComponent<RectTransform>().sizeDelta.x,
            imagesHeight[0].GetComponent<RectTransform>().sizeDelta.y);

        if(sliderObj)sliderObj.value = oldSliderValue;

        changeSize(oldSliderValue);
        changeColor(oldColor);

        colorPicker?.onPickerStart.AddListener(color =>
        {
            //colorPicker.Color = oldColor;
            colorPicker.SetNoniusPositionByColor(oldColor);
        });
    }

    void Update()
    {
        if (remainingTime > 0)
        {
            remainingTime -= Time.deltaTime;

        }
        else
        {
            if (bCanSave)
            {
                bCanSave = false;
                Debug.Log("记录滑块值：" + oldSliderValue);
                PlayerPrefs.SetFloat("GUIBoxSliderValue", oldSliderValue);
                Debug.Log("记录颜色值：" + oldColor);
                PlayerPrefs.SetFloat("GUIBoxColor_R", oldColor.r);
                PlayerPrefs.SetFloat("GUIBoxColor_G", oldColor.g);
                PlayerPrefs.SetFloat("GUIBoxColor_B", oldColor.b);
                PlayerPrefs.SetFloat("GUIBoxColor_A", oldColor.a);
            }
        }
    }


    public void onShowColorPicker()
    {
        colorPicker.gameObject.SetActive(!colorPicker.gameObject.activeSelf);
        colorPickerBtnText.text = colorPicker.gameObject.activeSelf ? "关闭选择颜色" : "开启选择颜色";

        // if (colorPicker.gameObject.activeSelf)
        //     StartCoroutine(colorIEnumerator());
    }

    IEnumerator colorIEnumerator()
    {
        yield return new WaitForEndOfFrame();
        colorPicker.Color = oldColor;
    }

    public void onSliceValue(Slider slider)
    {

        var _value = slider.value;
        changeSize(_value);
        if (oldSliderValue != _value)
        {
            oldSliderValue = _value;
            remainingTime = totalTime;
            bCanSave = true;
        }

    }

    void changeSize(float step)
    {
        for (int i = 0; i < imagesWidth.Length; i++)
        {
            var widthTran = imagesWidth[i].GetComponent<RectTransform>();
            widthTran.sizeDelta = new Vector2(initVector2.x + step, widthTran.sizeDelta.y);
        }

        for (int i = 0; i < imagesHeight.Length; i++)
        {
            var heightTran = imagesHeight[i].GetComponent<RectTransform>();
            heightTran.sizeDelta = new Vector2(heightTran.sizeDelta.x, initVector2.y + step);
        }
    }

    void changeColor(Color color)
    {
        for (int i = 0; i < imagesWidth.Length; i++)
        {
            imagesWidth[i].color = color;
        }

        for (int i = 0; i < imagesHeight.Length; i++)
        {
            imagesHeight[i].color = color;
        }
    }


    public void onChangeCrossHairImage()
    {

  
        CrossHairImage.SetActive(!CrossHairImage.activeSelf);
        if (CrossHairImage.activeSelf)
        {
            ScreenLocate.Main.mUIManagerSingle?.onShowCrosshair();
        }
        else {
            ScreenLocate.Main.mUIManagerSingle?.onHideCrosshair();
        }
        //PlayerPrefs.SetInt("CrossHairImageActive", CrossHairImage.activeSelf ? 1 : 0);
    }

    public void onSetCrossHairImage(bool bActive)
    {

        CrossHairImage.SetActive(bActive); 
    }
}