using Microsoft.MixedReality.Toolkit.UI;
using TMPro;
using UnityEngine;

public class PlotScale : BackButton
{
    [SerializeField] GameObject scaleSlider;
    [SerializeField] TextMeshPro valueText;
    [SerializeField] GameObject lineBoldObjects;
    [SerializeField] GameObject lineBoldSlider;
    [SerializeField] TextMeshPro lineBoldTitle;
    [SerializeField] TextMeshPro lineBoldValue;
    [SerializeField] GameObject backPlate;

    private float lineBold;
    private GraphData graphData;
    private Vector3 defaultScale;

    public void Initialize(GraphData data)
    {
        setGraphData(data);

        if (data.getGraphNum() == 0) //直線なら
        {

            lineBoldObjects.SetActive(true);
            lineBoldTitle.text = "直線の太さ";
            lineBoldValue.text = " ";
            lineBold = data.getgraphObject().transform.localScale.y;
            backPlate.transform.localScale = new Vector3(0.3f, 0.1f, 0.01f);
            backPlate.transform.localPosition = new Vector3(0, -0.02f, 0.01f);
        }
        else if(data.getGraphNum() == 1)//平面なら
        {
            lineBoldObjects.SetActive(false);
            lineBoldTitle.text = " ";
            lineBoldValue.text = " ";
            backPlate.transform.localScale = new Vector3(0.3f, 0.06f, 0.01f);
            backPlate.transform.localPosition = new Vector3(0, 0, 0.01f);
        }else if(data.getGraphNum() == 3) //点なら
        {
            lineBoldObjects.SetActive(false);
            lineBoldTitle.text = " ";
            lineBoldValue.text = " ";
            backPlate.transform.localScale = new Vector3(0.3f, 0.06f, 0.01f);
            backPlate.transform.localPosition = new Vector3(0, 0, 0.01f);
        }

        pressedDefaultButton();
    }

    public void setGraphData(GraphData data)
    {
        graphData = data;
        defaultScale = data.getgraphObject().transform.localScale;

        switch (data.getGraphNum())
        {
            case 0:
                valueText.text = (defaultScale.y).ToString();
                break;
            case 1:
                valueText.text = (defaultScale.x).ToString();
                break;
            case 3:
                valueText.text = (defaultScale.x).ToString();
                break;
        }
    }

    public void OnValueUpdated(SliderEventData eventData)
    {
        /*対数目盛を使用
         * スライダー：0->1
         * 3倍：0->3
         * 指数へ：10^0 -> 10^3
         * 
         * 点の場合は
         * 線の太さと同様
         * 
         * ->いずれユーザーから設定できるように。。。？
         * そこまでしなくていいか
         */

        if (eventData.Slider.name.Equals("ScaleController")) {
            switch (graphData.getGraphNum())
            {
                case 0:
                    float tmpXY = graphData.getgraphObject().transform.localScale.x;
                    graphData.getgraphObject().transform.localScale = new Vector3(tmpXY, Mathf.Pow(10, eventData.NewValue * 3), tmpXY);
                    valueText.text = Mathf.Pow(10, eventData.NewValue * 3).ToString("F4");
                    break;
                case 1:
                    float tmpY = graphData.getgraphObject().transform.localScale.y;
                    graphData.getgraphObject().transform.localScale = new Vector3(Mathf.Pow(10, eventData.NewValue * 3), tmpY, Mathf.Pow(10, eventData.NewValue * 3));
                    valueText.text = Mathf.Pow(10, eventData.NewValue * 3).ToString("F4");
                    break;
                case 3:
                    float tmp = graphData.getgraphObject().transform.localScale.x;
                    float f = Mathf.Pow(2, eventData.NewValue) - 0.9999f;
                    graphData.getgraphObject().transform.localScale = new Vector3(f,f,f);
                    valueText.text = (Mathf.Pow(2, eventData.NewValue) - 0.9999f).ToString("F4");
                    break;
            }
        }
        else if (eventData.Slider.name.Equals("LineBoldController"))
        {
            /*対数目盛使用
             * 0->1
             * 2^0 - 0.999 -> 2^1 -0.999
             */
            float tmpY = graphData.getgraphObject().transform.localScale.y;
            lineBold = Mathf.Pow(2,eventData.NewValue) -0.9999f ;
            graphData.getgraphObject().transform.localScale = new Vector3(lineBold, tmpY ,lineBold);
            lineBoldValue.text = lineBold.ToString("F4");
        }

    }

    public void pressedDefaultButton()
    {
        graphData.getgraphObject().transform.localScale = defaultScale;
        float mainSliderDefaultValue;
        float lineBoldDefaultValue;

        switch (graphData.getGraphNum())
        {
            case 0: //直線
                mainSliderDefaultValue = Mathf.Log10(defaultScale.y) / 3;
                lineBoldDefaultValue = Mathf.Log10(defaultScale.x + +0.9999f) / Mathf.Log10(2);

                scaleSlider.GetComponent<PinchSlider>().SliderValue = mainSliderDefaultValue;
                lineBoldSlider.GetComponent<PinchSlider>().SliderValue = lineBoldDefaultValue;
                
                valueText.text = mainSliderDefaultValue.ToString("F4");
                lineBoldValue.text = lineBoldDefaultValue.ToString("F4");

                break;
            case 1: //平面
                mainSliderDefaultValue = Mathf.Log10(defaultScale.x) / 3;
                scaleSlider.GetComponent<PinchSlider>().SliderValue = mainSliderDefaultValue;
                valueText.text = mainSliderDefaultValue.ToString("F4");
                break;
            case 3: //点
                mainSliderDefaultValue = Mathf.Log10(defaultScale.x + +0.9999f) / Mathf.Log10(2); ;
                scaleSlider.GetComponent<PinchSlider>().SliderValue = Mathf.Log10(defaultScale.x + +0.9999f) / Mathf.Log10(2); ;
                valueText.text = mainSliderDefaultValue.ToString("F4");
                break;
        }

    }

}
