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

public class PortalMovement : MonoBehaviour {

    private Rigidbody2D rigi;
    public static float bulletSpeed = 20f;
    public bool usedIsTrue = false;
    public bool portalIsActive = false;
    private GameObject gun;
    private Collider2D colli;
    public AudioClip[] clips;
    private bool isVisible = false;
    private SpriteRenderer sprity;
    public Sprite[] sprites;

	// Use this for initialization
	void Start () {
        rigi = GetComponent<Rigidbody2D>();
        gun = GameObject.FindObjectOfType<GunMovement>().gameObject;
        colli = GetComponent<Collider2D>();
        sprity = GetComponent<SpriteRenderer>();
	}
	
	// Update is called once per frame
	void Update () {
        Debug.Log(gameObject.layer);
     
        
        if (isVisible == false)
        {
            sprity.sprite = sprites[0];
        }

        if (isVisible == true)
        {
            sprity.sprite = sprites[1];
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        Rigidbody2D rigid = GetComponent<Rigidbody2D>();
        rigid.velocity = new Vector2(0, 0);
        portalIsActive = true;
        rigid.isKinematic = true;
        colli.isTrigger = true;
        
        gameObject.layer = 0;
        if (collision.gameObject.CompareTag("Ground")) { AudioSource auda = GetComponent<AudioSource>(); AudioClip clip = clips[0]; AudioSource.PlayClipAtPoint(clip, transform.position); isVisible = true; }
        if (collision.gameObject.CompareTag("Enemy") || collision.gameObject.CompareTag("Danger")) { AudioSource auda = GetComponent<AudioSource>(); AudioClip clip1 = clips[1]; AudioSource.PlayClipAtPoint(clip1, transform.position); }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        /* Rigidbody2D rigid = GetComponent<Rigidbody2D>();
         rigid.velocity = new Vector2(0, 0);
         portalIsActive = true;
         colli.isTrigger = true;
         rigid.isKinematic = true;
         gameObject.layer = 0;
         */
        
        if (collision.gameObject.CompareTag("Ground")) { AudioSource auda = GetComponent<AudioSource>(); AudioClip clip = clips[0]; AudioSource.PlayClipAtPoint(clip, transform.position); isVisible = true; }
        if (collision.gameObject.CompareTag("Enemy") || collision.gameObject.CompareTag("Danger")) { AudioSource auda = GetComponent<AudioSource>(); AudioClip clip1 = clips[1]; AudioSource.PlayClipAtPoint(clip1, transform.position); }
    }

    private void OnBecameInvisible()
    {
        Destroy(gameObject);
    }
}
