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

public class StoryManager : MonoBehaviour
{
    public static StoryManager Instance;

    public AudioClip computerClipAcquiring;
    public AudioClip computerClipAcquired;
    public AudioClip computerClipFailure;

    public AudioClip[] paulClips;

    public AudioClip jumpButtonEnabledClip;
    public AudioClip jumpClip;

    int currentJump = 0;

    public int RemainingJumpsAmount
    { get { return totalJumpsAmount - currentJump; } }

    const int totalJumpsAmount = 9;

    AudioSource computerSource;
    AudioSource paulSource;
    AudioSource jumpButtonEnabledSource;
    AudioSource jumpSource;

    bool waitingForPaulClip;

    void Awake()
    {
        Instance = this;
    }

    void Start()
    {
        computerSource = gameObject.AddComponent<AudioSource>();
        paulSource = gameObject.AddComponent<AudioSource>();
        jumpButtonEnabledSource = gameObject.AddComponent<AudioSource>();
        jumpSource = gameObject.AddComponent<AudioSource>();
        jumpButtonEnabledSource.clip = jumpButtonEnabledClip;
        jumpSource.clip = jumpClip;

        GameEventManager.OnJumpReady += OnJumpReady;
        GameEventManager.OnJumpEnd += OnJumpEnd;

        computerSource.clip = computerClipAcquiring;
        computerSource.Play();
        waitingForPaulClip = true;
    }

    void Update()
    {
        if(waitingForPaulClip && !computerSource.isPlaying)
        {
            waitingForPaulClip = false;
            PlayCurrentPaulClip();
        }
    }

    public int GetLostSystemsAmount()
    {
        if(RemainingJumpsAmount >= totalJumpsAmount - 3)
        {
            return 1;
        }
        else if(RemainingJumpsAmount > 1)
        {
            return 2;
        }
        return 3;
    }

    void OnJumpReady()
    {
        if(currentJump < 3)
        {
            computerSource.clip = computerClipAcquired;
            computerSource.Play();
        }
    }

    public void OnJumpLightOn()
    {
        jumpButtonEnabledSource.Play();
    }

    public void HyperspaceJump()
    {
        if(currentJump < totalJumpsAmount)
        {
            currentJump++;
            GameEventManager.HyperspaceJump();
            jumpSource.Play();
            if(currentJump == totalJumpsAmount)
            {
                //TODO: victory
            }
        }
    }

    void OnJumpEnd()
    {
        Debug.Log("OnJumpEnd, current jump: " + currentJump);
        if(currentJump <= 3)
        {
            computerSource.clip = computerClipFailure;
            computerSource.Play();
            waitingForPaulClip = true;
        }
        else
        {
            PlayCurrentPaulClip();
        }
    }

    void PlayCurrentPaulClip()
    {
        int index = GetCurrentPaulClipIndex();
        if(index >= 0)
        {
            paulSource.clip = paulClips[index];
            paulSource.Play();
        }
    }

    int GetCurrentPaulClipIndex()
    {
        switch(currentJump)
        {
            case 0:
                return 0;
            case 1:
                return 1;
            case 2:
                return 2;
            case 3:
                return 3;
            case 4:
                return 4;
            case 7:
                return 5;
            case 8:
                return 6;
            case 9:
                return 7;
        }
        return -1;
    }
}
