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

public class PizzaBug : MonoBehaviour
{
    [SerializeField]
    private GameObject ParentPizzaPiece;
    [SerializeField]
    private Sprite DeadSprite;

    private GameObject currentPizzaPiece;
    private Vector3 formerPosition;
    private bool smashed;

    // Start is called before the first frame update
    void Start()
    {
        currentPizzaPiece = ParentPizzaPiece;
        formerPosition = transform.position;
        smashed = false;
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void MoveToPizzaPiece(GameObject pizzaPiece, float randomRadius)
    {
        if (!currentPizzaPiece.activeSelf) {
            Vector2 moveToPosition = formerPosition;
            if (pizzaPiece != null)
            {
                Vector2 randOffset = new Vector2(Random.value - 0.5f, Random.value - 0.5f);
                randOffset.Normalize();
                randOffset *= randomRadius * Random.value;
                currentPizzaPiece = pizzaPiece;

                Vector3 center = pizzaPiece.GetComponent<PolygonCollider2D>().bounds.center;

                moveToPosition = new Vector2(center.x + randOffset.x, center.y + randOffset.y);
            }

            float newAngle = Mathf.Atan2(moveToPosition.y - transform.position.y, moveToPosition.x - transform.position.x);

            Quaternion rot = transform.localRotation;
            rot.eulerAngles = new Vector3(0f,0f, Mathf.Rad2Deg * newAngle - 90f);
            transform.localRotation = rot;

            System.Action shakeAction = delegate () {
                LeanTween.moveY(gameObject, transform.position.y + 0.1f, 0.3f + Random.value*0.1f).setEase(LeanTweenType.easeShake)
                    .setLoopClamp().setRepeat(-1);
                Quaternion rot2 = transform.localRotation;
                float newAngle2 = Mathf.Atan2(transform.position.y, transform.position.x);
                //rot2.eulerAngles = new Vector3(0f, 0f, Random.value * 360f);
                rot2.eulerAngles = new Vector3(0f, 0f, newAngle2 * Mathf.Rad2Deg);
                transform.localRotation = rot2;

                GetComponent<BoxCollider2D>().enabled = true;
            };
            
            if (pizzaPiece == null)
            {
                LeanTween.move(gameObject, moveToPosition, 0.7f + 0.5f * Random.value).setEase(LeanTweenType.easeInOutQuad)
                    .setOnComplete(shakeAction).setDelay(Random.value * 0.2f);
            }
            else
            {
                LeanTween.move(gameObject, moveToPosition, 0.7f + 0.5f * Random.value)
                    .setEase(LeanTweenType.easeInOutQuad).setDelay(Random.value*0.2f);
            }
        }
    }

    private void OnMouseDown()
    {
        GetComponent<SpriteRenderer>().sprite = DeadSprite;
        smashed = true;
        GetComponent<BoxCollider2D>().enabled = false;
        GetComponent<AudioSource>().Play();
        LeanTween.cancel(gameObject);
    }
}
