﻿using UnityEngine;
using System.Collections;

public class BlinkPortalScript : PickupBase 
{
  private GameObject playerGO;
  public GameObject ExitPortalGO;
  private GameObject exitPortal; // The instaniated obj, used to handle destruction
  public float destroyObjIn = 8f;
  //Setting a min and max y spawn offset
  //public float min_ySpawn = -4f;
 // public float max_ySpawn = 4f;

  //our y offsets for the exit point
  public float minOffset_y = -3.5f;
  public float maxOffset_y = 3.5f;
  //The portals exit offset position
  public Vector3 exitPos = new Vector3(3f,2f,0);

  private Vector3 startingPos; // The starting spawn location

	// Use this for initialization
	void Start () {
    //Setting a default position for our pickup spawner
/*    Vector3 tempPos = transform.position;
    tempPos.y = Random.Range(min_ySpawn,max_ySpawn);
    transform.position = tempPos;*/

    //Setting references
    playerGO = GameObject.FindGameObjectWithTag("Player");

    // We get the actual starting position X & Y post spawn
    startingPos = transform.position;
    ProcessPortalExitLocation();

    //Destroy portal after 8 seconds if player doesn't pick it up.
    Destroy(gameObject, destroyObjIn);
    Destroy(exitPortal, destroyObjIn);
	}

  public override Vector3 GenerateSpawnPos(){
    if(playerGO){
      
      //Setting the droprange offset to spawn infront of player position
      float minDropRange_X = Mathf.Abs(playerGO.transform.position.x) + min_OffSetX;
      float maxDropRange_X = Mathf.Abs(playerGO.transform.position.x) + max_OffSetX;
      
      //Setting a random spawn offset
      float spawnPosX = Random.Range(minDropRange_X, maxDropRange_X);
      float spawnPosY = Random.Range(min_ySpawn, max_ySpawn);
      
      // Creating the final spawn pos
      Vector3 tempPos = new Vector3(spawnPosX, spawnPosY, 0f);
      
      return tempPos;
    }
    // Reinit player ref if null
    playerGO = GameObject.FindGameObjectWithTag("Player");
    return new Vector3(0,0,0);
  }

  // Processes the portals exit position
  void ProcessPortalExitLocation(){

    // If the exit location is occupied, move it 2 units over on the x axis
    var hitColliders = Physics.OverlapSphere(exitPos, 2);//2 is purely chosen arbitrarly
    if(hitColliders.Length > 0){
      exitPos.x += 2f;
    }

    // The exit portal defaults to 4 units on the x axis
    exitPos.y = Random.Range(minOffset_y, maxOffset_y);
    exitPos += startingPos;
    SpawnExitPortal();
  }

  void OnTriggerEnter2D(Collider2D coll){
    if (coll.tag == "Player"){

      // We can put this under camera, but we want time to go back to normal before zoom-in
      Invoke("PlayerTeleportEnd", 0.8f); 
      Time.timeScale = 0.5f;  // Slow down time when teleporting

      Camera.main.GetComponent<CameraController>().PlayerTeleport();
      coll.transform.position = exitPos;

      // Add a delay on destruction to complete the invoke
      Destroy(gameObject, 2f); //Destroy the entrance-portal
      Destroy(exitPortal, 2f); //Destroy the exit-portal
    }
  }

  // Returns time back to normal after teleport
  void PlayerTeleportEnd(){
    Time.timeScale = 1.0f;
  }

  // Instantiates the exit portal
  void SpawnExitPortal(){
    exitPortal = (GameObject)Instantiate(ExitPortalGO);
    
    // Set the position of the exit portal
    exitPortal.transform.position = exitPos;
  }
}
