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

public class CreateObject : MonoBehaviour {
	
//	public Transform Spawnpoint; 		//instead of Transform you can have gameObject
//	public GameObject Prefab; 
	public static GameObject portal1Parent; 
	public static GameObject portal2Parent;
    public GunMovement gunMovement;
    public GameObject gunprefab;

	// © 2017 TheFlyingKeyboard
	// theflyingkeyboard.net
		public GameObject objectToSpawn;
		public GameObject objectToSpawn2;
        public GameObject gun;
    public float initialPortalMovement = 0.01f;
    //temporary
        float distance = 5f;
    Vector3 position;
    SpriteRenderer sprity1;
    SpriteRenderer sprity2;
    private LevelManager levi;
    public float offset;
    public AudioClip[] audioclips;
    public static bool playerDead = false;
    private PortalMovement porta;
    private AudioSource audi;
    public bool canShoot = false;


    //

    private void Awake()
    {
       
    }


    // Use this for initialization
    void Start () {
		portal2Parent = GameObject.FindGameObjectWithTag ("portal2Parent"); 
		portal1Parent = GameObject.FindGameObjectWithTag ("portal1Parent");
        //temporary
        position = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
        sprity1 = portal1Parent.transform.GetChild(0).gameObject.GetComponent<SpriteRenderer>();
        sprity2 = portal2Parent.transform.GetChild(0).gameObject.GetComponent<SpriteRenderer>();
        levi = FindObjectOfType<LevelManager>();
        audi = gameObject.GetComponent<AudioSource>();
        audi.loop = false;

    }

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

        if (Input.GetMouseButtonDown(0) && portal1Parent.transform.childCount <= 1 && canShoot)     //left click to spawn Portal1 at mouse position
        {
            Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            spawnPosition.z = 0.0f;
            GameObject objectInstance = Instantiate(objectToSpawn, new Vector3(gun.transform.position.x, gun.transform.position.y, gun.transform.position.z) + gunMovement.transform.up * 0.01f, Quaternion.identity);
            objectInstance.transform.parent = portal1Parent.transform;
            
            Rigidbody2D rigid = objectInstance.GetComponent<Rigidbody2D>();
            position = Camera.main.ScreenToWorldPoint(position);
            //objectInstance.transform.LookAt(GunMovement.mousePosition);
            Debug.Log("a: " + gunMovement.transform.up);
            rigid.velocity = gunMovement.transform.up * PortalMovement.bulletSpeed;
            Debug.Log("b: " + rigid.velocity);
            //rigid.velocity = new Vector3(GunMovement.mousePosition.x - gun.transform.position.x, GunMovement.mousePosition.y - gun.transform.position.y, GunMovement.angle * PortalMovement.bulletSpeed);

            AudioSource.PlayClipAtPoint(audioclips[3], transform.position);
        }


        //if (//Portal1 already exists) {		//Max one Portal1 REMEMBER TO DO SAME WITH PORTAL 2
        //destroy first clone of Portal1 and make a new clone at new position)
        //}\\
    

        if (portal1Parent.transform.GetChild(0).GetComponent<PortalMovement>().usedIsTrue == true && portal2Parent.transform.GetChild(0).GetComponent<PortalMovement>().usedIsTrue == true)
        {
            Destroy(portal1Parent.transform.GetChild(0).gameObject);
            Destroy(portal2Parent.transform.GetChild(0).gameObject);
        }

        if (Mathf.Sign(gameObject.transform.position.x) == 1) 
		{
			Debug.Log ("Moving Right");
		}

		else
		{
			//Debug.Log ("Moving Left");
		}


		if (Input.GetMouseButtonDown(0) && portal1Parent.transform.childCount == 2)		//left click to spawn Portal1 at mouse position
		{
			Transform oldPortal1 = portal1Parent.transform.GetChild (0);
			Destroy (oldPortal1.gameObject);

            Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            spawnPosition.z = 0.0f;
            GameObject objectInstance = Instantiate(objectToSpawn, new Vector3(gun.transform.position.x, gun.transform.position.y, gun.transform.position.z) + gunMovement.transform.up * 0.01f, Quaternion.identity);
            objectInstance.transform.parent = portal1Parent.transform;
            Rigidbody2D rigid = objectInstance.GetComponent<Rigidbody2D>();
            position = Camera.main.ScreenToWorldPoint(position);
            //objectInstance.transform.LookAt(GunMovement.mousePosition);
            Debug.Log("a: " + gunMovement.transform.up);
            rigid.velocity = gunMovement.transform.up * PortalMovement.bulletSpeed;
            Debug.Log("b: " + rigid.velocity);
            //rigid.velocity = new Vector3(GunMovement.mousePosition.x - gun.transform.position.x, GunMovement.mousePosition.y - gun.transform.position.y, GunMovement.angle * PortalMovement.bulletSpeed);




            //if (//Portal1 already exists) {		//Max one Portal1 REMEMBER TO DO SAME WITH PORTAL 2
            //destroy first clone of Portal1 and make a new clone at new position)
            //}\\
        }


        if (Input.GetKeyDown(KeyCode.Mouse1) && portal2Parent.transform.childCount <= 1 && canShoot)		//Right click for spawning Portal2 at mouse position
			{

            Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            spawnPosition.z = 0.0f;
            GameObject objectInstance = Instantiate(objectToSpawn2, new Vector3(gun.transform.position.x, gun.transform.position.y, gun.transform.position.z) + gunMovement.transform.up * 0.01f, Quaternion.identity);
            objectInstance.transform.parent = portal2Parent.transform;
            Rigidbody2D rigid = objectInstance.GetComponent<Rigidbody2D>();
            position = Camera.main.ScreenToWorldPoint(position);
            //objectInstance.transform.LookAt(GunMovement.mousePosition);
            Debug.Log("a: " + gunMovement.transform.up);
            rigid.velocity = gunMovement.transform.up * PortalMovement.bulletSpeed;
            Debug.Log("b: " + rigid.velocity);
            //rigid.velocity = new Vector3(GunMovement.mousePosition.x - gun.transform.position.x, GunMovement.mousePosition.y - gun.transform.position.y, GunMovement.angle * PortalMovement.bulletSpeed);
            AudioSource.PlayClipAtPoint(audioclips[4], transform.position);



            //if (//Portal1 already exists) {		//Max one Portal1 REMEMBER TO DO SAME WITH PORTAL 2
            //destroy first clone of Portal1 and make a new clone at new position)
            //}\\

        }

        if (Input.GetKeyDown(KeyCode.Mouse1) && portal2Parent.transform.childCount == 2)		//left click to spawn Portal1 at mouse position
		{
			Transform oldPortal2 = portal2Parent.transform.GetChild (0);
			Destroy (oldPortal2.gameObject);

            Vector3 spawnPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            spawnPosition.z = 0.0f;
            GameObject objectInstance = Instantiate(objectToSpawn2, new Vector3(gun.transform.position.x, gun.transform.position.y, gun.transform.position.z) + gunMovement.transform.up * 0.01f, Quaternion.identity);
            objectInstance.transform.parent = portal2Parent.transform;
            Rigidbody2D rigid = objectInstance.GetComponent<Rigidbody2D>();
            position = Camera.main.ScreenToWorldPoint(position);
            //objectInstance.transform.LookAt(GunMovement.mousePosition);
            Debug.Log("a: " + gunMovement.transform.up);
            rigid.velocity = gunMovement.transform.up * PortalMovement.bulletSpeed;
            Debug.Log("b: " + rigid.velocity);
            //rigid.velocity = new Vector3(GunMovement.mousePosition.x - gun.transform.position.x, GunMovement.mousePosition.y - gun.transform.position.y, GunMovement.angle * PortalMovement.bulletSpeed);




            //if (//Portal1 already exists) {		//Max one Portal1 REMEMBER TO DO SAME WITH PORTAL 2
            //destroy first clone of Portal1 and make a new clone at new position)
            //}\\
        }

        if (portal1Parent.transform.childCount>1)
			{
			Transform child1 = portal1Parent.transform.GetChild (1);
			Destroy (child1.gameObject); 
			}


		if (portal2Parent.transform.childCount>1)
		{
			Transform child2 = portal2Parent.transform.GetChild (1);
			Destroy (child2.gameObject); 
		}


	}

	void OnTriggerEnter2D(Collider2D collider)
	{
        if (collider.gameObject.CompareTag("Danger"))
        {
            Destroy(gameObject);
           
            playerDead = true;

        }

        if (collider.gameObject.CompareTag("DoorTrigger"))
        {
            Debug.Log("Trigger Works");
            SceneManager.LoadScene("Level1");
            
            canShoot = true;
            Debug.Log(canShoot);    
        }

        if (collider.gameObject.CompareTag("PickUpGun"))
        {
            Instantiate(gunprefab, gameObject.transform);
            gunprefab.transform.parent = gameObject.transform;
            canShoot = true;

           

        }

        

        float additionalMovement;
		float additionalMOption1 = 0f;
		float additionalMOption2 = 0f;

        if (collider.gameObject.CompareTag("Portal1") & portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive == true) 
		{
			if (Mathf.Sign (gameObject.transform.position.x) == 1) 
			{
				additionalMovement = additionalMOption1;
			} 
			else 
			{
				additionalMovement = additionalMOption2;
			}
			Transform portal2Collider = portal2Parent.transform.GetChild (0);
			gameObject.transform.position = new Vector3 (portal2Collider.position.x + additionalMovement, portal2Collider.position.y + offset, portal2Collider.position.z);
            portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal2Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;
            Transform portal1Collider = portal1Parent.transform.GetChild(0);
            portal1Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;

        }
       
		if (collider.gameObject.CompareTag ("Portal2") & portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive == true) 
		{
			if (Mathf.Sign (gameObject.transform.position.x) == 1) 
			{
				additionalMovement = additionalMOption1;
			} 
			else 
			{
				additionalMovement = additionalMOption2;
			}
			Transform portal1Collider = portal1Parent.transform.GetChild (0);
			gameObject.transform.position = new Vector3(portal1Collider.position.x + additionalMovement, portal1Collider.position.y +offset, portal1Collider.position.z);
            portal1Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal2Parent.transform.GetChild(0).gameObject.GetComponent<PortalMovement>().portalIsActive = false;
            portal1Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;
            Transform portal2Collider = portal2Parent.transform.GetChild(0);
            portal2Collider.gameObject.GetComponent<PortalMovement>().usedIsTrue = true;


        }
        
       
        
	}
        void OnCollisionEnter2D(Collision2D collision)
        {
        if (collision.gameObject.CompareTag("Enemy"))
        {
          
            
            Destroy(gameObject);
            SceneManager.LoadScene("End");
            playerDead = true;
            
           

        }
    }

    void LoadEndScene()
    {
        levi.LoadScenewithDelay(3f);
    }

    void SetPlayerDeadTrue()
    {
        playerDead = true;
    }

}