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

public class RitualCode : MonoBehaviour {
  //UI Handler
  public GameObject UIHandler;
  private UIHandlerScript UIHS;

  //Audio Component
  private AudioSource AS;
  public AudioClip BanishmentScript;
  public AudioClip SatanicLanguage;

  //Final door
  public GameObject ExcapeDoor;
  private DoorScript EDoor;

  //Right VR Controller
  public GameObject RightController;
  private VRTK_InteractGrab RCG;
  private VRTK_ControllerEvents RCE;

  //Left VR Controller
  public GameObject LeftController;
  private VRTK_InteractGrab LCG;
  private VRTK_ControllerEvents LCE;

  //Objects snapped to the VR Controllers
  public GameObject GrabLeft;
  public GameObject GrabRight;

  //Ritual Itemlist
  public GameObject Salt;
  public GameObject Blood;
  public GameObject Candle1;
  public GameObject Candle2;
  public GameObject Candle3;
  public GameObject Candle4;
  public GameObject Candle5;
  public GameObject CandleFire;
  public GameObject Hitler;

  //ToDo list
  private bool isSalt = false;
  private bool isBlood = false;
  private bool isCandle1 = false;
  private bool isCandle2 = false;
  private bool isCandle3 = false;
  private bool isCandle4 = false;
  private bool isCandle5 = false;
  private bool isCandleFire = false;
  private bool isChanting = false;
  private bool isHitler = false;

  //if entered room
  public bool inRitualSpace = false;

  //Current Ritual step
  public int RitualStep = 0;

  //Press Button
  private bool MayButton = true;
  public float waitbuttonTime = 1f;

  //Random events
  public GameObject RandomLockEvent;
  public GameObject RandomAudioEvent;

  //Steps in order:
  //0 Place Blood
  //1 Place Salt
  //2 Place Candle1
  //3 Place Candle2
  //4 Place Candle3
  //5 Place Candle4
  //6 Place Candle5
  //7 Set Fire
  //8 Chant the banishment script

  //When this object spawns
  void Awake () {
   RCG = RightController.GetComponent();
   LCG = LeftController.GetComponent();
   RCE = RightController.GetComponent ();
   LCE = LeftController.GetComponent ();
   UIHS = UIHandler.GetComponent ();
   EDoor = ExcapeDoor.GetComponent ();
   AS = GetComponent ();
  }

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

  //Set in-game visibility of objects
  void CheckActives(){
   Salt.SetActive (isSalt);
   Blood.SetActive (isBlood);
   Candle1.SetActive (isCandle1);
   Candle2.SetActive (isCandle2);
   Candle3.SetActive (isCandle3);
   Candle4.SetActive (isCandle4);
   Candle5.SetActive (isCandle5);
   CandleFire.SetActive (isCandleFire);
   Hitler.SetActive (isHitler);
  }

  //The ritual per step
  void SetActives(){
   //Items in hand
   GrabRight = RCG.GetGrabbedObject();
   GrabLeft = LCG.GetGrabbedObject();
  
   //Begin
   if (RitualStep == 0 && UIHS.QuestStep == 11) {
    UIHS.QuestStep = 110;
   }
  
   //Righthand
   if (GrabRight != null)
   {
    if (GrabRight.gameObject.tag == "BloodsackFull" && inRitualSpace && RCE.triggerClicked && RitualStep == 0 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 1;
     isBlood = true;
     UIHS.QuestStep = 111;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "Salt" && inRitualSpace && RCE.triggerClicked && RitualStep == 1 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 2;
     isSalt = true;
     UIHS.QuestStep = 112;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "BlackCandle" && inRitualSpace && RCE.triggerClicked && RitualStep == 2 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 3;
     isCandle1 = true;
     UIHS.QuestStep = 113;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "BlackCandle" && inRitualSpace && RCE.triggerClicked && RitualStep == 3 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 4;
     isCandle2 = true;
     UIHS.QuestStep = 114;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "BlackCandle" && inRitualSpace && RCE.triggerClicked && RitualStep == 4 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 5;
     isCandle3 = true;
     UIHS.QuestStep = 115;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "BlackCandle" && inRitualSpace && RCE.triggerClicked && RitualStep == 5 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 6;
     isCandle4 = true;
     UIHS.QuestStep = 116;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "BlackCandle" && inRitualSpace && RCE.triggerClicked && RitualStep == 6 && MayButton)
    {
     Destroy(GrabRight.gameObject);
     GrabRight = null;
     RitualStep = 7;
     isCandle5 = true;
     UIHS.QuestStep = 117;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "Grimoire" && inRitualSpace && RCE.triggerClicked && RitualStep == 7 && MayButton)
    {
     RitualStep = 8;
     isCandleFire = true;
     UIHS.QuestStep = 118;
     StartCoroutine(WaitButton());
    }
    if (GrabRight.gameObject.tag == "Grimoire" && inRitualSpace && RCE.triggerClicked && RitualStep == 8 && MayButton)
    {
     StartCoroutine (WaitForHitler ());
     isChanting = true;
     AS.PlayOneShot(BanishmentScript);
     StartCoroutine(WaitButton());
     RandomAudioEvent.SetActive (false);
     RandomLockEvent.SetActive (false);
    }
   }
  
   //Lefthand
   if (GrabLeft != null)
   {
    if (GrabLeft.gameObject.tag == "BloodsackFull" && inRitualSpace && LCE.triggerClicked && RitualStep == 0 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 1;
     isBlood = true;
     UIHS.QuestStep = 111;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "Salt" && inRitualSpace && LCE.triggerClicked && RitualStep == 1 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 2;
     isSalt = true;
     UIHS.QuestStep = 112;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "BlackCandle" && inRitualSpace && LCE.triggerClicked && RitualStep == 2 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 3;
     isCandle1 = true;
     UIHS.QuestStep = 113;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "BlackCandle" && inRitualSpace && LCE.triggerClicked && RitualStep == 3 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 4;
     isCandle2 = true;
     UIHS.QuestStep = 114;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "BlackCandle" && inRitualSpace && LCE.triggerClicked && RitualStep == 4 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 5;
     isCandle3 = true;
     UIHS.QuestStep = 115;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "BlackCandle" && inRitualSpace && LCE.triggerClicked && RitualStep == 5 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 6;
     isCandle4 = true;
     UIHS.QuestStep = 116;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "BlackCandle" && inRitualSpace && LCE.triggerClicked && RitualStep == 6 && MayButton)
    {
     Destroy(GrabLeft.gameObject);
     GrabLeft = null;
     RitualStep = 7;
     isCandle5 = true;
     UIHS.QuestStep = 117;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "Grimoire" && inRitualSpace && LCE.triggerClicked && RitualStep == 7 && MayButton)
    {
     RitualStep = 8;
     isCandleFire = true;
     UIHS.QuestStep = 118;
     StartCoroutine(WaitButton());
    }
    if (GrabLeft.gameObject.tag == "Grimoire" && inRitualSpace && LCE.triggerClicked && RitualStep == 8 && MayButton)
    {
     StartCoroutine (WaitForHitler ());
     isChanting = true;
     AS.PlayOneShot(BanishmentScript);
     StartCoroutine(WaitButton());
     RandomAudioEvent.SetActive (false);
     RandomLockEvent.SetActive (false);
    }
   }
  
   //End
   if (RitualStep == 9) {
    isHitler = true;
    AS.PlayOneShot (SatanicLanguage);
    EDoor.Locked = false;
    StartCoroutine (WaitForExcape ());
    RitualStep = 10;
   }
  }

  //Button spam protection
  IEnumerator WaitButton()
  {
   print("Waiting");
   MayButton = false;
   yield return new WaitForSeconds(waitbuttonTime);
   MayButton = true;
  }

  //Wait time to continue to step 9
  IEnumerator WaitForHitler(){
   yield return new WaitForSeconds (5f);
   RitualStep = 9;
   UIHS.QuestStep = 119;
  }

  //Wait time to continue to step 10
  IEnumerator WaitForExcape(){
   yield return new WaitForSeconds (8f);
   UIHS.QuestStep = 12;
   isHitler = false;
  }

  //Check player entered
  void OnTriggerEnter(Collider Target){
   if (Target.gameObject.tag == "Player") {
    inRitualSpace = true;
   }
  }

  //Check player stays
  void OnTriggerStay(Collider Target){
   if (Target.gameObject.tag == "Player") {
    inRitualSpace = true;
   }
  }

  //Check player exited
  void OnTriggerExit(Collider Target){
   if (Target.gameObject.tag == "Player") {
    inRitualSpace = false;
   }
  }

}//CLASS