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

public class Plateau : MonoBehaviour {
  //Storage for the layed objects
  public GameObject Obj1;
  public GameObject Obj2;

  //Possible output objects
  public GameObject BloodsackFull;
  public GameObject BlackCandle;

  //In-game output settings
  public string output;
  public Vector3 middlePosition;

  //Animation Component
  public GameObject CombiMachiene;
  private Animator animCombie;

  //Audio Component
  private AudioSource AS;
  public AudioClip CombiAudio;

  //Particle effects
  public GameObject ParticleObject;
  public bool inCombi = false;

  //Doors of the machine
  public GameObject DoorLeft;
  public Vector3 DoorLeftClosed;
  public GameObject DoorRight;
  public Vector3 DoorRightClosed;

  //When this object spawns
  void Awake(){
   animCombie = CombiMachiene.GetComponent ();
   AS = GetComponent ();
  }

  // Update is called once per frame
  void Update () {
   if (Obj1 != null && Obj2 != null)
   {
    CheckCombination();
   }
   CheckInCombi ();
  }

  //Check if the combination has an output
  void CheckCombination(){
   //WhiteCandle + Ink
   if ((Obj1.tag == "WhiteCandle" && Obj2.tag == "Ink") || (Obj2.tag == "WhiteCandle" && Obj1.tag == "Ink")) {
    output = "BlackCandle";
    ExecuteCombination ();
   }
   //Needle + Bloodsack
   if ((Obj1.tag == "Bloodsack" && Obj2.tag == "Needle") || (Obj2.tag == "Bloodsack" && Obj1.tag == "Neelde")) {
    output = "BloodsackFull";
    ExecuteCombination ();
   }
  }

  //Start Combining
  void ExecuteCombination(){
   //Delete content
   Destroy(Obj1);
   Destroy(Obj2);
   Obj1 = null;
   Obj2 = null;
   inCombi = true;
  
   //Spawn new Object
   if (output == "BlackCandle") {
    Instantiate (BlackCandle, middlePosition, Quaternion.identity);
   }
   if (output == "BloodsackFull") {
    Instantiate (BloodsackFull, middlePosition, Quaternion.identity);
   }
  
   //Animation + Freeze
   StartCoroutine(UpDoor());
  }

  //Rise plateu
  IEnumerator UpDoor(){
   AS.PlayOneShot (CombiAudio);
   yield return new WaitForSeconds (5f);
   animCombie.SetBool("Open", true);
   inCombi = false;
   StartCoroutine (DownDoor ());
  }

  //Set plateau
  IEnumerator DownDoor(){
   yield return new WaitForSeconds (8f);
   animCombie.SetBool ("Open", false);
  }

  //When combining
  void CheckInCombi()
  {
   if (inCombi)
   {
    ParticleObject.SetActive(true);
    if (DoorLeft != null)
    {
     DoorLeft.transform.localEulerAngles = DoorLeftClosed;
    }
    if (DoorRight != null)
    {
     DoorRight.transform.localEulerAngles = DoorRightClosed;
    }
   }
   if (!inCombi)
   {
    ParticleObject.SetActive(false);
   }
  }

  //Sense objects entering
  void OnCollisionEnter(Collision Target)
  {
   if (Target.gameObject.tag == "WhiteCandle" || Target.gameObject.tag == "Bloodsack"
   || Target.gameObject.tag == "Ink" || Target.gameObject.tag == "Needle")
   {
    if (Obj1 == null)
    {
     Obj1 = Target.gameObject;
    }
    else if (Obj2 == null)
    {
     Obj2 = Target.gameObject;
    }
   }
  }

  //Sense objects leaving
  void OnCollisionExit(Collision Target)
  {
   if (Target.gameObject == Obj1)
   {
    Obj1 = null;
   }
   if (Target.gameObject == Obj2)
   {
    Obj2 = null;
   }
  }

}//CLASS