using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;

public class DebugSystem : MonoBehaviour
{
    //動作確認してません
    [SerializeField] GraphManager graphManager;
    [SerializeField] GraphAdderWindow graphAdderWindow;
    [SerializeField] GameObject graphs;


    public GraphData[] graphDatas;

    [ContextMenu("resetGraphDatas")]
    public void resetGraphDatass()
    {
        int i = -1;
        foreach (GraphData data in graphDatas)
        {
            if (data != null)
            {
                if (data.isDataListInputted())
                {
                    i++;
                }
            }
        }
        if(i>=0) { graphManager.removeGraphData(i); }
        

    }

    [ContextMenu("addDummyLineData (0,0,0,1,1,1)")]
    public void setLine1()
    {
        setDummyLine(1);
    }

    [ContextMenu("addDummyLineData (1,1,1,-1,-1,-1)")]
    public void setLine2()
    {
        setDummyLine(2);
    }

    [ContextMenu("addDummyLineData (-1,-1,-1,1,0,1)")]
    public void setLine3()
    {
        setDummyLine(3);
    }

    [ContextMenu("addDummyLineData (2,0,0,-1,-1,-1)")]
    public void setLine4()
    {
        setDummyLine(4);
    }

    [ContextMenu("addDummyPlaneData (0,0,1,0)")]
    public void setPlane1()
    {
        setDummyPlane(1);
    }
    [ContextMenu("addDummyPlaneData (0,1,0,0)")]
    public void setPlane2()
    {
        setDummyPlane(2);
    }
    [ContextMenu("addDummyPlaneData (1,0,0,0)")]
    public void setPlane3()
    {
        setDummyPlane(3);
    }
    [ContextMenu("addDummyPlaneData (1,1,1,1)")]
    public void setPlane4()
    {
        setDummyPlane(4);
    }

    public void setDummyLine(int k)
    {
        int i = 0;
        GraphData data = new GraphData();
        data.setGraphNum(0);
        switch (k)
        {
            case 1:
                data.setLinePassingPoint(new Vector3(0, 0, 0));
                data.setLineDirection(new Vector3(1, 1, 1));
                break;
            case 2:
                data.setLinePassingPoint(new Vector3(1, 1, 1));
                data.setLineDirection(new Vector3(-1, -1, 1));
                break;
            case 3:
                data.setLinePassingPoint(new Vector3(-1, -1, -1));
                data.setLineDirection(new Vector3(1, -1, 1));
                break;
            case 4:
                data.setLinePassingPoint(new Vector3(2, 0, 0));
                data.setLineDirection(new Vector3(-1, -1, -1));
                break;
        }
        


        graphDatas = graphManager.getAllGraphData();
        foreach (GraphData d in graphDatas)
        {
            if (d != null)
            {
                if (d.isDataListInputted())
                {
                    i++;
                }
            }
        }

        Vector3 inLinePoint = new Vector3();
        GameObject ln = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        ln.transform.parent = graphs.transform;

        if (data.getLineDirection().y != 0)
        {

            float uY0 = -1 * data.getLinePassingPoint().y / data.getLineDirection().y;


            inLinePoint = new Vector3(
                data.getLinePassingPoint().x + uY0 * data.getLineDirection().x,
                0,
                data.getLinePassingPoint().z + uY0 * data.getLineDirection().z);

        }
        else if (data.getLineDirection().x != 0)
        {
            float uX0 = -1 * data.getLinePassingPoint().x / data.getLineDirection().x;

            inLinePoint = new Vector3(
                0,
                data.getLinePassingPoint().y + uX0 * data.getLineDirection().y,
                data.getLinePassingPoint().z + uX0 * data.getLineDirection().z);

        }
        else if (data.getLineDirection().z != 0)
        {
            float uZ0 = -1 * data.getLinePassingPoint().z / data.getLineDirection().z;

            inLinePoint = new Vector3(
                data.getLinePassingPoint().x + uZ0 * data.getLineDirection().x,
                data.getLinePassingPoint().y + uZ0 * data.getLineDirection().y,
                0);

        }
        else
        {
            //全て0の場合
            Debug.Log("係数が無効です");
        }

        ln.transform.localRotation = Quaternion.FromToRotation(Vector3.up, data.getLineDirection());
        ln.transform.localPosition = inLinePoint;
        ln.transform.localScale = new Vector3(0.01f, 20, 0.01f);
        Destroy(data.getgraphObject());
        data.setGraphObject(ln);
        data.setDataListInputted(true);
        graphManager.addNewGraphData(data, i);
    }

    public void setDummyPlane(int k)
    {
        int i = 0;
        GraphData data = new GraphData();
        data.setGraphNum(1);
        switch (k)
        {
            case 1:
                data.setPlaneCoefficient(new Vector4(0,0,1,0) );
                break;
            case 2:
                data.setPlaneCoefficient(new Vector4(0, 1, 0, 0));
                break;
            case 3:
                data.setPlaneCoefficient(new Vector4(1, 0, 0, 0));
                break;
            case 4:
                data.setPlaneCoefficient(new Vector4(1, 1, 1, 1));
                break;
        }



        graphDatas = graphManager.getAllGraphData();
        foreach (GraphData d in graphDatas)
        {
            if (d != null)
            {
                if (d.isDataListInputted())
                {
                    i++;
                }
            }
        }

        Vector3 nomalVector3 = new Vector3(
                    data.getPlaneCoefficient().x,
                    data.getPlaneCoefficient().y,
                    data.getPlaneCoefficient().z);
        nomalVector3.Normalize();

        Vector3 inPlanePoint = new Vector3();


        if (data.getPlaneCoefficient().y != 0)
        {
            inPlanePoint = new Vector3(
                0,
                -1 * data.getPlaneCoefficient().w / data.getPlaneCoefficient().y,
                0);


        }
        else if (data.getPlaneCoefficient().x != 0)
        {
            inPlanePoint = new Vector3(
                -1 * data.getPlaneCoefficient().w / data.getPlaneCoefficient().x,
                0,
                0);


        }
        else if (data.getPlaneCoefficient().z != 0)
        {
            inPlanePoint = new Vector3(
                0,
                0,
                -1 * data.getPlaneCoefficient().w / data.getPlaneCoefficient().z);

        }
        else
        {
            //全て0の場合
            Debug.Log("error");
        }

        GameObject pl = GameObject.CreatePrimitive(PrimitiveType.Cube);
        pl.transform.parent = graphs.transform;
        pl.transform.localPosition = inPlanePoint;
        pl.transform.localRotation = Quaternion.FromToRotation(Vector3.up, nomalVector3);
        pl.transform.localScale = new Vector3(5, 0.001f, 5);
        Destroy(data.getgraphObject());
        data.setGraphObject(pl);
        data.setDataListInputted(true);
        graphManager.addNewGraphData(data, i);
    }

    
}
