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

public class AnimatorController : MonoBehaviour
{
    private WaypointSystem m_waypointSystem;
    private Vision m_vision;
    private Animator m_animator;
    private BlackBoard m_blackboard;

    private Timer m_timer;
    public AudioSource LoseSound;
    private void Start()
    {
        m_waypointSystem = transform.GetComponent<WaypointSystem>();
        m_vision = transform.GetComponent<Vision>();
        m_animator = transform.GetComponent<Animator>();
        m_blackboard = (BlackBoard)FindObjectOfType(typeof(BlackBoard));
        m_waypointSystem.onCanMove += OnCanMoveResponse;
        m_waypointSystem.onStopMove += OnStopMoveResponse;
        m_vision.onFoundPlayer += OnFoundPlayerResponse;
    }

    private void OnCanMoveResponse()
    {
        m_animator.SetBool(AnimationHash.Walking, true);
    }
    
    private void OnStopMoveResponse()
    {
        m_animator.SetBool(AnimationHash.Walking, false);
    }

    private void OnFoundPlayerResponse()
    {
        m_animator.SetTrigger(AnimationHash.Scare);
        m_blackboard.player.GetComponent<Player>().Animator.SetTrigger(AnimationHash.Scare);
        m_blackboard.player.GetComponent<Player>().StopMove(0);
        LoseSound.Play();
        m_timer = new Timer(1.5f, Time.time);
    }

    private void Update()
    {
        if (m_timer != null)
        {
            if(m_timer.IsFinished())
                EndGame();
        }
    }

    public void EndGame()
    {
        m_blackboard.EndGame();
    }
}
