using Microsoft.MixedReality.Toolkit.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO.Pipes;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;

public class GraphManager : BackButton
{
    [SerializeField] GameObject optionMenuScreen;
    [SerializeField] List<TextMeshPro> graphKindTexts;
    [SerializeField] List<TextMeshPro> graphFormulaTexts;
    [SerializeField] CrossManager crossManager;
    [SerializeField] GameObject errorWindow;

    public GraphData[] graphDatas;

    private void Start()
    {
        graphDatas = new GraphData[3];
    }

    public void pressedButton(int i)
    {
        if (graphDatas[i] != null)
        {
            if (graphDatas[i].isDataListInputted() == true)
            {
                optionMenuScreen.GetComponent<GraphOptionMenu>().Initialize(graphDatas[i], i);
                optionMenuScreen.SetActive(true);
            }
        }
    }

    public void addNewGraphData(GraphData graphData, int index)
    {
        if (index == -1)
        {
            int num = 0;

            foreach (GraphData gd in graphDatas)
            {
                if (gd != null)
                {
                    if (gd.isDataListInputted() == true)
                    {
                        num++;
                    }
                }

            }

            if (num < 3)//0,1,2なら
            {
                setGraphInfomation(num, graphData);
            }
            else
            {
                errorWindow.GetComponent<ErrorWindow>().displayErrorMessage("もう追加できません！！");
                base.pressedBackButton();
            }
            
        }
        else
        {
            setGraphInfomation(index, graphData);
        }
    }

    public void removeGraphData(int index)
    {
        
        switch (index)
        {
            case 0:
                Destroy(graphDatas[0].getgraphObject());
                graphDatas[0] = new GraphData();
                graphKindTexts[0].text = " ";
                graphFormulaTexts[0].text = " ";

                if (graphDatas[1] != null)
                {
                    if (graphDatas[1].isDataListInputted() == true)
                    {
                        graphDatas[0] = graphDatas[1];
                        graphKindTexts[0].text = graphKindTexts[1].text;
                        graphFormulaTexts[0].text = graphFormulaTexts[1].text;

                        if (graphDatas[2] != null)
                        {
                            if (graphDatas[2].isDataListInputted() == true)
                            {
                                graphDatas[1] = graphDatas[2];
                                graphKindTexts[1].text = graphKindTexts[2].text;
                                graphFormulaTexts[1].text = graphFormulaTexts[2].text;

                                
                                graphDatas[2] = new GraphData();
                                graphKindTexts[2].text = " ";
                                graphFormulaTexts[2].text = " ";

                            }
                        }
                        else
                        {
                            graphDatas[1] = new GraphData();
                            graphKindTexts[1].text = " ";
                            graphFormulaTexts[1].text = " ";
                        }
                    }

                }
                
                break;
            case 1:
                Destroy(graphDatas[1].getgraphObject());
                graphDatas[1] = new GraphData();
                graphKindTexts[1].text = " ";
                graphFormulaTexts[1].text = " ";

                if (graphDatas[2] != null)
                {
                    if (graphDatas[2].isDataListInputted() == true)
                    {
                        graphDatas[1] = graphDatas[2];
                        graphKindTexts[1].text = graphKindTexts[2].text;
                        graphFormulaTexts[1].text = graphFormulaTexts[2].text;

                        graphDatas[2] = new GraphData();
                        graphKindTexts[2].text = " ";
                        graphFormulaTexts[2].text = " ";
                    }
                }
                
                break;
            case 2:
                Destroy(graphDatas[2].getgraphObject());
                graphDatas[2] = new GraphData();
                graphKindTexts[2].text = " ";
                graphFormulaTexts[2].text = " ";
                break;
        }
        crossManager.crossChecker(graphDatas);
    }
    private void setGraphInfomation(int index, GraphData data)
    {
        int graphNum = data.getGraphNum();
        if (index > 2)
        {
            return;
        }

        switch (graphNum)
        {
            case 0: //直線
                Vector3 p = data.getLinePassingPoint();
                Vector3 d = data.getLineDirection();

                graphKindTexts[index].text = "直線";
                graphFormulaTexts[index].text
                    = "点(" + p.x + " ," + p.y + " ," + p.z + ")を通る" +
                        "\nベクトル(" + d.x + " ," + d.y + " ," + d.z+ ")\nに平行な直線";
                break;
            case 1: //平面
                Vector4 c = data.getPlaneCoefficient();

                graphKindTexts[index].text = "平面";

                graphFormulaTexts[index].text
                    = c.x + "x ";
                if(Mathf.Sign(c.y) == -1)
                {
                    graphFormulaTexts[index].text
                        += c.y + "y ";
                }
                else
                {
                    graphFormulaTexts[index].text
                        += "+" + c.y + "y ";
                }

                if (Mathf.Sign(c.z) == -1)
                {
                    graphFormulaTexts[index].text
                        += c.z + "z ";
                }
                else
                {
                    graphFormulaTexts[index].text
                        += "+" + c.z + "z ";
                }

                if (Mathf.Sign(c.w) == -1)
                {
                    graphFormulaTexts[index].text
                        += c.w + " = 0";
                }
                else
                {
                    graphFormulaTexts[index].text
                        += "+" + c.w + " = 0";
                }


                break;
            case 2: //その他

                break;
        }

        data.setDataListInputted(true);
        graphDatas[index] = data;

        crossManager.crossChecker(graphDatas);
    }

    public GraphData getGraphData(int i)
    {
        return graphDatas[i];
    }

    public GraphData[] getAllGraphData()
    {
        return graphDatas;
    }
}
