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

public class CrystalScript : MonoBehaviour
{

    Collider collider;
    public float TestRadius = 1;

    private void Start()
    {
        collider = GetComponent<Collider>();
    }

    void OnCollisionEnter(Collision col)
    {
        if (col.collider.tag == "Particle")
        {
            Physics.IgnoreCollision(col.collider, collider);

            ParticleController pc = col.gameObject.GetComponent<ParticleController>();
            //pc.Direction = Refract(1f, 1.5f, pc.Direction,- col.contacts[0].normal);

            StartCoroutine(Lazr(col.transform.position, pc.Direction, col.contacts[0].normal, col.collider));
        }
        

       // Physics.IgnoreCollision(col.collider, collider, false);
    }

    IEnumerator ResetCollide (Collider col1, Collider col2)
    {
        yield return new WaitForSeconds(0.5f);
        Physics.IgnoreCollision(col1, col2, false);
    }

    IEnumerator Lazr (Vector3 Pos, Vector3 Dir, Vector3 normal, Collider col)
    {
        // refract number one
        Vector3 IDirection = Refract(1f, 5, Dir, normal);
        Debug.DrawLine(Pos, Pos + IDirection, Color.cyan, 5f);

        ParticleController pc = col.gameObject.GetComponent<ParticleController>();
        pc.Direction = IDirection;


        RaycastHit hitInfo;
        bool hitBool = Physics.Raycast(col.transform.position + IDirection.normalized * TestRadius, -IDirection.normalized, out hitInfo, TestRadius, ~(1<<8),QueryTriggerInteraction.Ignore);
        Debug.DrawLine(col.transform.position + IDirection.normalized * TestRadius, col.transform.position + IDirection.normalized * TestRadius - IDirection * TestRadius, Color.blue, 1f);
        if (hitBool)
        {
            print("hit Sumthing " + hitInfo.collider.name);
            if ( hitInfo.transform == transform)
            {
                print("hit self");
                
                pc.rb.velocity = Vector3.zero;
                float distance = Vector3.Distance(Pos, hitInfo.point);
                float speed = 10;//m/s
                int frames = (int) (30 * distance / speed);
                for (int i = 0; i < frames; i++)
                {
                    pc.Direction = IDirection;
                    
                    yield return new WaitForSeconds(1f/30f);
                }
                yield return new WaitForSeconds(0.2f);
                //pc.transform.position = hitInfo.point;
                pc.Direction = Refract(1, 5f, IDirection, hitInfo.normal);
                
            }
            
           
        }
        
        pc.moving = true;
        yield return new WaitForSeconds(0.2f);

        Physics.IgnoreCollision(col, collider, false);

    }

    Vector3 Refract(float n1,float n2, Vector3 Dir, Vector3 Normal)
    {
        float theta = Vector3.Angle(Dir, Normal);
        float theta2 = Mathf.Rad2Deg * Mathf.Asin((n1 / n2) * Mathf.Sin(Mathf.Deg2Rad*theta));

        Vector3 antinormal = new Vector3(Normal.y, - Normal.x, 0);

        Vector3 answer;

        if (Vector3.Angle(antinormal,Dir) > 90)
        {
            answer =Quaternion.Euler(0, 0,  1 * theta2) *  Normal;
            
        }
        else
        {
            answer =Quaternion.Euler(0, 0,- 1 * theta2) *  Normal ;
        }
         

        return answer;
    }

}
