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

public class EnemyMovement : MonoBehaviour {

	

	public float distanceMultiplier;
	public float speedMultiplier; 

	float xStart; 
	float timeOffset;
    public float minX;
    public float maxX;
    public bool goRight = true;
   

	void Start()
	{
		//movement
		xStart = transform.position.x;
		timeOffset = Random.Range (-1f, 1f);
	}

	void Update()
	{
		//movement
		/*Vector2 position = transform.position;

		var myTime = Time.time + timeOffset; 

		position.x = xStart + Mathf.Sin (myTime * speedMultiplier) * distanceMultiplier; 

		transform.position = position;*/
        if (goRight == true)
        {
            transform.Translate(Vector3.right * Time.deltaTime);
            gameObject.transform.localScale = new Vector3(-1, 1, 1);
        }
        if (!goRight)
        {
            transform.Translate(Vector3.left * Time.deltaTime);
            gameObject.transform.localScale = new Vector3(1, 1, 1);
        }

        if (transform.position.x >= maxX )
        {
            goRight = false;
        }

        if (transform.position.x <= minX)

        {
            goRight = true;
        }

    }
		

	//damaging player
	private void OnCollisionEnter2D(Collision2D collision)
	{
      
	}

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.CompareTag("Danger") || collision.gameObject.CompareTag("Projectiles"))
        {
            Destroy(gameObject);
            AudioSource audi = GetComponent<AudioSource>();
            
            AudioSource.PlayClipAtPoint(audi.clip, transform.position, 1f);
        }

        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, 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;

        }

        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, 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;


        }
    }
}


