using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MeshMakerNamespace;

public class RuntimeCSGFlatlandObject : MonoBehaviour
{
    public GameObject PastLightCone;
    public GameObject crosssection;
    public Mesh m;

    int cnt = 0;

    public CSG csg;
    // Start is called before the first frame update
    void Start()
    {
        //CSG.EPSILON = 1e-5f;   // Adjustable epsilon value 
        //csg = new CSG();
        //csg.Target = transform.gameObject;
        PastLightCone = GameObject.Find("Cone");
        //csg.Brush = PastLightCone;
        //csg.OperationType = CSG.Operation.Intersection;

        //Destroy(crosssection);
        //GameObject tmp = csg.PerformCSG();
        //crosssection = csg.PerformCSG();
        crosssection = new GameObject();

        crosssection.name = "Thecrosssection";

        Physics.IgnoreLayerCollision(8, 8);
        m = new Mesh();
    }

    // Update is called once per frame
    void Update()
    {
        /*if (cnt % 120 == 0)
        {
            cnt = 0;
            Destroy(crosssection);
            GameObject tmp = csg.PerformCSG();
            crosssection = tmp;
        }
        cnt++;*/
    }

    public void OnCollisionStay(Collision collision)
    {
        //Debug.Log("Wow!");
        if (collision.collider.gameObject == PastLightCone)
        {
            crosssection.name = "hit!";

            /*Debug.Log("Contact count : " + collision.contactCount);

            int vertexCount = collision.contactCount;
            Vector3[] vertices = new Vector3[vertexCount];

            int[] indices = new int[vertexCount];

            for (int i = 0; i < vertexCount; i++)
            {
                vertices[i] = collision.GetContact(i).point;
                indices[i] = i;
            }

            m.Clear();
            m.vertices = vertices;
            m.SetIndices(indices, MeshTopology.Points, 0);
            m.RecalculateBounds();
            m.RecalculateNormals();

            MeshFilter mf = GetComponent<MeshFilter>();
            mf.mesh = m;
            Debug.Log(m.bounds);*/
        }
    }

    public void OnTriggerEnter(Collider other)
    {
        //Debug.Log( "Trigger hit!");
        //if(other.gameObject == PastLightCone)
        //{
            //crosssection.name = "hit!";
            //GameObject tmp = csg.PerformCSG();
            //tmp.name = "wow!";
            //crosssection = tmp;
        //}
        //crosssection = csg.PerformCSG();
        //GameObject tmp = csg.PerformCSG();
        //crosssection = tmp;
    }

    
}
