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

[System.Serializable]
public class PlayerCollision : PlayerComponent
{
    private Vector2 prepos;

    public void Process()
    {
        ObjContext("Main");
        Obj().SetLayerTag("Player", "Player");
        Obj().RectOffset(0f, -0.125f);
        Obj().SetRect(0.5f, 1.25f);

        par.objectC.GetObject("Player:Main").AddComponent<PlayerCollisionDetection>().Init(this);
    }

    public void Detect()
    {
        //@ Raycasting
            if(par.mono.Falling)
            {
                if(Ray(Obj().xypos() + new Vector2(-0.25f, -0.75f), prepos + new Vector2(-0.25f, -0.75f)))
                {par.mono.Fall(ray.point + new Vector2(0.25f, 0.75f));}
                else if(Ray(Obj().xypos() + new Vector2(0.25f, -0.75f), prepos + new Vector2(0.25f, -0.75f)))
                {par.mono.Fall(ray.point + new Vector2(-0.25f, 0.75f));}
                
            }
        //@
        prepos = Obj().xypos();

        if(par.KeyPressed("a"))
        {
            Ray(new Vector2(2.354f, -2.475f), new Vector2(2.354f, -2.523f));
        }
    }
    
    public void Spawn()
    {
        ObjContext("Main");
        //Obj().ColliderOn(true);

        prepos = new Vector2(0f, 0f);
    }

    private RaycastHit2D ray;

    private bool Ray(Vector2 a, Vector2 b)
    {
        RaycastHit2D _ray = World.Raycast(a, b, World.Distance(a, b), "Physical");
        if(_ray.transform != null)
        {
            ray = _ray;
            Debug.DrawLine(new Vector3(a.x, a.y, 0f), new Vector3(b.x, b.y, 0f), Color.red, 60f);
if(par.KeyHold("a")) {X.Log(10, Ut.Types.ParseVector(a) + ", " + Ut.Types.ParseVector(b) + ", " + Ut.Types.ParseVector(ray.point) + ", " + World.Distance(a, b));}
            return true;
        }
        return false;
    }
}

public class PlayerCollisionDetection : MonoBehaviour
{
    private PlayerCollision par;
    public void Init(PlayerCollision par) {this.par = par;}

    void OnCollisionEnter2D(Collision2D ccollision)
    {

    }
}