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

public class Cooownter : MonoBehaviour {

    static public int Coownter = 0;
    private Text text;
    private Text texter;
    

	// Use this for initialization
	void Start () {
        texter = FindObjectOfType<Text>();
        if (texter.CompareTag("cownter"))
        { text = texter; }

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

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            Coownter += 1;
            text.text = "Cownter :" + Coownter.ToString();
            Debug.Log(Coownter);
            Destroy(gameObject, 0.5f);
        }

        if (collision.gameObject.CompareTag("Portal2") & CreateObject.portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive == true)
        {

            Transform portal1Collider = CreateObject.portal1Parent.transform.GetChild(0);
            gameObject.transform.position = new Vector3(portal1Collider.position.x, portal1Collider.position.y + 0.5f, portal1Collider.position.z);
            CreateObject.portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            CreateObject.portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal1Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;
            Transform portal2Collider = CreateObject.portal2Parent.transform.GetChild(0);
            portal2Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;


        }

        if (collision.gameObject.CompareTag("Portal1") & CreateObject.portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive == true)
        {

            Transform portal2Collider = CreateObject.portal2Parent.transform.GetChild(0);
            gameObject.transform.position = new Vector3(portal2Collider.position.x, portal2Collider.position.y + 0.5f, portal2Collider.position.z);
          
            Transform portal1Collider = CreateObject.portal1Parent.transform.GetChild(0);
          

        }
    }


    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            Coownter += 1;
            text.text = "Cownter :"+ Coownter.ToString();

            Debug.Log(Coownter);
            Destroy(gameObject, 0.5f);
        }

        if (collision.gameObject.CompareTag("Portal2") & CreateObject.portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive == true)
        {
          
            Transform portal1Collider = CreateObject.portal1Parent.transform.GetChild(0);
            gameObject.transform.position = new Vector3(portal1Collider.position.x, portal1Collider.position.y + 0.5f, portal1Collider.position.z);
            CreateObject.portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            CreateObject.portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal1Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;
            Transform portal2Collider = CreateObject.portal2Parent.transform.GetChild(0);
            portal2Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;


        }

        if (collision.gameObject.CompareTag("Portal1") & CreateObject.portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive == true)
        {

            Transform portal2Collider = CreateObject.portal2Parent.transform.GetChild(0);
            gameObject.transform.position = new Vector3(portal2Collider.position.x, portal2Collider.position.y + 0.5f, portal2Collider.position.z);
            CreateObject.portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            CreateObject.portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal2Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;
            Transform portal1Collider = CreateObject.portal1Parent.transform.GetChild(0);
            portal1Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;


        }
    }
}
