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

public class StoryPoint : MonoBehaviour {

    public bool lastPoint = false;

    public CanvasGroup endCG;

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Player")
        {
            FlightController2D.instance.pause = true;   

            //print("`story point found ");
            //play clip
            AudioSource source = GetComponent<AudioSource>();
            source.Play();

            MusicManager.instance.ass.volume /= 3;

            //turn off environment sounds
            MusicManager.instance.music.volume = 0;
            Invoke("SwitchMusicOn", source.clip.length);

            GetComponent<Collider2D>().enabled = false;

           // transform.GetChild(0).gameObject.SetActive(  true);
           ChangeLight(true);
            
          

            //pause interaction

            //register waypoint
            foreach(GameStates.SaveStates s in GameStates.instance.states)
            {
                if(s.posTrans == transform)
                {
                    s.reached = true;
                }
            }
        }
    }

    void Awake()
    {
        //get light
        
        lightTransform = transform.GetChild(0);
        l = lightTransform.GetComponentInChildren<Light>();
        //get original brightness
            originalBrightness = l.intensity;
    }
    
    public Transform lightTransform;
    public Light l ;
    public float originalBrightness;
    void ChangeLight(bool on)
    {
        lightTransform.gameObject.SetActive(true);
        if(on)
        {
            //set light to active
            l.gameObject.SetActive(true);
            
            l.intensity = originalBrightness;
            
        }
        else 
        {
            //dim
            l.intensity = originalBrightness/2;
            //get value and divide by two        
        }
    }  

    void SwitchMusicOn()
    {
        //transform.GetChild(0).gameObject.SetActive(false);
        ChangeLight(false);

        MusicManager.instance.ass.volume *= 3;    

        FlightController2D.instance.pause = false;
        
        MusicManager.instance.music.volume = .7f;

          if(lastPoint)
            {
                StartCoroutine( GameStates.instance.FadeCanvas(endCG, true));
            }
    }
}
