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

public class Player : MonoBehaviour
{
    public static bool generatingStick {get; private set;}
    public GameObject stickPrefab;
    public Vector3 stickOffset;
    public Vector3 stickGenPosition {get { return transform.position + stickOffset;}}
    public Vector3 stickGrowRate;
    private Transform curStick;
    public Vector3 currentEdge;
    public static bool walking{get;private set;}
    void Start()
    {
        CameraFollower.Target= transform;    
    }

    void Update()
    {
        if(walking){return;}
        if(!generatingStick && Input.GetKeyDown(KeyCode.Space)){
            OnStartStick();
            generatingStick = true;
        }
        if(generatingStick && Input.GetKeyUp(KeyCode.Space)){
            generatingStick=false;
            OnStopStick();
        }

        RaycastHit2D hit= Physics2D.Linecast(transform.position, transform.position- new Vector3(0,2,0));
        if(hit!=null && hit.collider != null){
            currentEdge = hit.collider.bounds.center + (hit.collider.bounds.size/2f);
            Debug.DrawLine(transform.position, currentEdge);
        }
    }

    void FixedUpdate(){
        if(generatingStick){
            if(curStick != null){
                curStick.localScale += stickGrowRate;
            }
        }
    }


    void OnStartStick(){
        curStick = Instantiate(stickPrefab, currentEdge, Quaternion.identity).transform;

    }

    IEnumerator walk(Vector3 pos){
        walking=true;
        float t =0;
        Vector3 startPos = transform.position;
        while(Vector3.Distance(transform.position, pos)> 0.01f){
            t += Time.deltaTime;
            transform.position = Vector3.Lerp(startPos, pos,  t);
            yield return new WaitForFixedUpdate();
        }
        walking=false;
        CameraFollower.Target=transform;

    }

    IEnumerator fall(Vector3 stickEnd){
        walking=true;
        float t =0;
        Vector3 startPos= transform.position;
        while(Vector3.Distance(transform.position, stickEnd)> 0.01f){
            t += Time.deltaTime;
            transform.position = Vector3.Lerp(startPos, stickEnd, t);
            yield return new WaitForFixedUpdate();
        }
        while(transform.position.y > -200){
            transform.position-= new Vector3(0,0.1f,0);
            yield return new WaitForFixedUpdate();

        }
    }

    void OnStopStick(){
        Rigidbody2D rb = curStick.GetComponent<Rigidbody2D>();
        rb.AddTorque(-30);
        rb.simulated = true;
        curStick.GetComponent<HingeJoint2D>().connectedAnchor = transform.position;
        StartCoroutine(CheckStick(curStick));

        curStick=null;
    }

    IEnumerator CheckStick(Transform stick){
        CameraFollower.Target=null;

        Rigidbody2D rb = stick.GetComponent<Rigidbody2D>();
        yield return new WaitForSeconds(0.5f);
        bool shorten=false;
        while(Mathf.Abs(rb.velocity.magnitude) > 0.01f){
            Debug.Log(rb.velocity.magnitude);
            if(stick.GetChild(1).transform.position.x < stick.position.x){shorten=true;break;}
            yield return new WaitForSeconds(0.1f);
        }
        rb.simulated=false;
        RaycastHit2D hit = Physics2D.Linecast(stick.GetChild(1).transform.position, stick.GetChild(1).transform.position- new Vector3(0,1f,0));
        if(hit.collider == null){
            Debug.Log("Fall down");
            StartCoroutine(fall((!shorten) ? stick.GetChild(1).position : stick.position + new Vector3(0,0.5f)));            

        }else{
            Debug.Log("Travel to point : " + hit.point + " : " + hit.collider.name);

            StartCoroutine(walk(hit.point + new Vector2(0,0.5f)));            
        }
        
    }
}
