using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class OpeningSequenceController : MonoBehaviour
{
    public Trigger trigger;
    bool isEnabled;
    bool running;

    public RawImage subtitleRawImage;

    public AudioClip openingVoiceline;
    public Texture2D openingVoiceLineSubtitle;
    public Texture2D pressRMB;

    public AudioClip kickSound;
    public AudioSource audioSource;
    public Animator animator;
    public float flyAwaySpeed;
    public AudioClip pressRMBAudioClip;


    
    public float elapsedTime;
    public float openingLineTime;
    public float subtitle1DisappearTime;
    bool openingPlayed;
    bool firstSubtitleDissapear;


    public float doorKickTime;
    bool doorKickPlayed;

    public bool debug_reset;

    public float RMBPressDelay;
    public float timeSinceRMBPress;

    public float doorOpenSpeed;
    public Transform doorTransform;

    public Color noSubtitleColor;
    public Color subtitleColor;

    public float kickSoundDelay;
    public bool kickSoundPlayed;

    public AudioSource musicPlayer;

    private void Start()
    {
        subtitleRawImage.color = noSubtitleColor; // no alpha
    }

    private void Update()
    {
        if (trigger.isTriggered && !running)
        {
            isEnabled = true;
            running = true;
        }

        if (isEnabled || debug_reset)
        {
            
            debug_reset = false;

            elapsedTime += Time.deltaTime;

            if (elapsedTime >= openingLineTime && !openingPlayed)
            {
                animator.SetTrigger("opening voiceline");
                audioSource.clip = openingVoiceline;
                audioSource.Play();
                subtitleRawImage.texture = openingVoiceLineSubtitle;
                subtitleRawImage.color = subtitleColor; // alpha

                openingPlayed = true;
            }


            if (elapsedTime >= subtitle1DisappearTime && !firstSubtitleDissapear)
            {
               
                subtitleRawImage.texture = pressRMB;
                audioSource.clip = pressRMBAudioClip;
                audioSource.Play();

                firstSubtitleDissapear = true;
            }

            if (Input.GetKey(KeyCode.Mouse1) && !doorKickPlayed)
            {
                audioSource.Stop();
                subtitleRawImage.texture = null;
                subtitleRawImage.color = noSubtitleColor; // no alpha

                animator.SetTrigger("kick door");

                doorKickPlayed = true;
            }

            if (doorKickPlayed)
            {
                timeSinceRMBPress += Time.deltaTime;

                if (timeSinceRMBPress >= kickSoundDelay && !kickSoundPlayed)
                {
                    audioSource.clip = kickSound;
                    audioSource.Play();

                    kickSoundPlayed = true;
                }


                if (timeSinceRMBPress >= RMBPressDelay)
                {
                    

                    transform.position += Vector3.up * flyAwaySpeed * Time.deltaTime;
                    doorTransform.position += Vector3.right * doorOpenSpeed * Time.deltaTime;

                    if (!musicPlayer.isPlaying)
                    {
                        musicPlayer.Play();
                    }
                }
                
            }
        }    

    }
}
