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

public class Shrinking : MonoBehaviour
{
    public float targetScaleX = 2;
    public float targetScaleY = 2;
    public float shrinkSpeed = 1;

    private Vector3 orgpos; 
    private Vector3 orgScale;
    private Vector3 orgPos;

    public bool shrinking = true;
    public bool died = false;

    public Animator anim;
    public float animwait = 0.5f;

    private bool popplayed = false;
    public AudioSource audioclip;
    public AudioClip clip;

    public GameObject[] bubbles = new GameObject[6];

    public string secondLevel;
    //public GameObject win;
    // Use this for initialization
    void Start ()
    {
       // clip = GetComponent<AudioClip>();
        audioclip = GetComponent<AudioSource>();
        anim.GetComponent<Animator>();
        orgpos = transform.position;
        orgScale = transform.localScale;
        orgPos = transform.position;
	}
	
	// Update is called once per frame
	void Update ()
    {
        //Might need on the object themselves
        //if (transform.localScale.x <= 0.1f)
        //{
        //    died = true;
        //    Debug.Log("Die");
        //    mosquito.transform.position = mosquitoPos;
        //    bug.transform.position = bugPos;
        //    bie.transform.position = biePos;
        //    transform.position = orgpos;
        //    transform.localScale = orgScale;
        //}
        //else
        //{
        //    died = false;
        //    shrinking = true;
        //}

        if (shrinking)
        {
            transform.localScale -= Vector3.one * Time.deltaTime * shrinkSpeed;
            if (transform.localScale.x < targetScaleX)
                shrinking = false;
            if (transform.localScale.y < targetScaleY)
                shrinking = false;
        }
        if (transform.localScale.x <= 2f)
        {
           // for(int i = 0; i < 7; i++)
           // {
           //     bubbles[i].SetActive(true);
           // }
           //transform.position = orgPos;
           transform.parent.gameObject.transform.position = orgPos;
           transform.localScale = orgScale;
           shrinking = true;
        }
        //if(mos_scrp.ded)
        //{
        //    Debug.Log("Ded");
        //    mosquito.transform.localScale = orgScale;
        //    shrinking = true;
        //}

    }

    private IEnumerator OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.CompareTag("Bubble"))
        {
            Debug.Log("Crashed in Bubble");
            transform.localScale = orgScale;
            shrinking = true;
            anim = collision.GetComponent<Animator>();
            anim.SetBool("Pop", true);
            //Sound
            audioclip.PlayOneShot(clip);
            yield return new WaitForSeconds(animwait);
            //audioclip.Play();
            collision.gameObject.SetActive(false);
            anim.SetBool("Pop", false);
            //Set animation
            //yield return new WaitForSeconds(animwait);
            //anim.SetBool("Pop", false);
        }
       //if(collision.gameObject.CompareTag("Bie"))
       //{
       //    win.SetActive(true);
       //    Time.timeScale = 0;
       //}
    }
}
