using Microsoft.MixedReality.Toolkit.UI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeColorScreen : BackButton
{
    [SerializeField] GameObject rSlider;
    [SerializeField] GameObject gSlider;
    [SerializeField] GameObject bSlider;
    [SerializeField] GameObject aSlider;
    
    private Renderer graphRenderer;

    private Color defaultColor = new Color();
    private Color currentColor = new Color();

    public void Initialize(GraphData data)
    {
        defaultColor = data.getgraphObject().GetComponent<MeshRenderer>().material.color;
        pressedDefaultButton();
        graphRenderer = data.getgraphObject().GetComponent<MeshRenderer>();
        setMaterialSetting(graphRenderer.material);
    }


    private void Update()
    {
        graphRenderer.material.color = currentColor;
    }

    public void OnValueUpdated(SliderEventData eventData)
    {

        if (eventData.Slider.name.Equals("RColorController"))
        {
            currentColor.r = eventData.NewValue;
        }
        else if(eventData.Slider.name.Equals("GColorController"))
        {
            currentColor.g = eventData.NewValue;
        }
        else if (eventData.Slider.name.Equals("BColorController"))
        {
            currentColor.b = eventData.NewValue;
        }
        else if (eventData.Slider.name.Equals("AColorController"))
        {
            currentColor.a = eventData.NewValue;
        }

    }

    public void pressedDefaultButton()
    {
        rSlider.GetComponent<PinchSlider>().SliderValue = defaultColor.r;
        gSlider.GetComponent<PinchSlider>().SliderValue = defaultColor.g;
        bSlider.GetComponent<PinchSlider>().SliderValue = defaultColor.b;
        aSlider.GetComponent<PinchSlider>().SliderValue = defaultColor.a;

        currentColor = defaultColor;
    }



    private void setMaterialSetting(Material material)
    {
        material.SetOverrideTag("RenderType", "Transparent");
        material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);
        material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);
        material.SetInt("_ZWrite", 0);
        material.DisableKeyword("_ALPHATEST_ON");
        material.EnableKeyword("_ALPHABLEND_ON");
        material.DisableKeyword("_ALPHAPREMULTIPLY_ON");
        material.renderQueue = 3000;
    }
}
