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

public class TambourineBehavior : MonoBehaviour {

    private Rigidbody2D rb;
    // private float timer;

    private Animator animator;
    
    private GameObject collidedObject;
    private float timeLerped = 0.0f;
    private float timeToLerp = 0.5f;

    private GameObject player;

    public bool pinned = false;


    void Awake() {
        this.gameObject.GetComponent<CircleCollider2D>().enabled = true;
        rb = this.gameObject.GetComponent<Rigidbody2D>();
        animator = this.gameObject.GetComponent<Animator>();
        player = GameObject.FindGameObjectWithTag("Player");
    }

    
    void Start() {
        // rb.AddForce(new Vector2(horizSpeed, vertSpeed), ForceMode2D.Impulse);
        StartCoroutine(CheckToDestroy());
    }

    void Update() {
        // if (Input.GetKeyUp(KeyCode.K)) {
        //     Destroy(this.gameObject);
        // }
        if (collidedObject != null && collidedObject.tag != "grappleSurface") {
            rb.constraints = RigidbodyConstraints2D.FreezeAll;
            // this.gameObject.transform.position = col.transform.position;
            timeLerped += Time.deltaTime;
            this.gameObject.transform.position = Vector2.Lerp(this.gameObject.transform.position, collidedObject.transform.position, timeLerped/timeToLerp);
            if (this.gameObject.transform.position.x == collidedObject.transform.position.x && this.gameObject.transform.position.y == collidedObject.transform.position.y) {
                animator.SetBool("pinned", true);
                pinned = true;
            } else {
                // print("pinned, but not same position: " + this.gameObject.transform.position + " / " + collidedObject.transform.position);
            }
        }
    }

    void OnTriggerEnter2D(Collider2D col) {
        collidedObject = col.gameObject;
        if (collidedObject.tag == "Enemy") {
            this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
            collidedObject.GetComponent<EnemyPatrol>().pinned = true;
        } else if (collidedObject.tag == "Projectile") {
            // print("pinned");
            this.gameObject.GetComponent<CircleCollider2D>().enabled = false;
            collidedObject.GetComponent<ProjectileBehavior>().Pin();
        } 
    }

    void OnCollisionEnter2D(Collision2D col) {
        if (col.gameObject.tag == "wall") {
            DestroySelf();
        }
    }

    IEnumerator CheckToDestroy() {
        yield return new WaitForSeconds(5f);
        print("waited 5");
        if (!player.GetComponent<PlayerBehavior>().grapplingRope.isGrappling) {
            DestroySelf();
        }
    }

    public void DestroySelf() {
        if (collidedObject != null && collidedObject.tag == "Enemy") {
            collidedObject.GetComponent<EnemyPatrol>().pinned = false;
        } else if (collidedObject != null && collidedObject.tag == "Projectile") {
            collidedObject.GetComponent<ProjectileBehavior>().Explode();
        }
        player.GetComponent<PlayerBehavior>().hasTambourine = true;
        player.GetComponent<PlayerBehavior>().grapplingGun.ReleaseGrapple();
        Destroy(this.gameObject);
    }
}
