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

public class NPCBrain : MonoBehaviour
{
    // store useful info for npc here

    public float startEnergy = 10f;
    public float energyLossRate = 1f;
    public float energyRecoverRate = 2f;

    public GameObject food;

    public GameObject roam;
    public GameObject rest;
    public GameObject eat;

    Animator anim;
    float energy;

    public float GetEnergy()
    {
        return energy;
    }

    public GameObject GetFood()
    {
        return food;
    }

    void Start()
    {
        energy = startEnergy;
        anim = GetComponent<Animator>();

        if (food == null)
        {
            food = GameObject.FindGameObjectWithTag("food");
        }

        SetEatObject(false);
        SetRestObject(false);
        SetRoamObject(false);
    }


    void Update()
    {
        // update distance to food
        anim.SetFloat("food-distance", Vector3.Distance(food.transform.position, transform.position));
        // update current energy
        anim.SetFloat("energy", energy);
    }

    public void LoseEnergy()
    {
        energy -= energyLossRate;
    }

    public void GainEnergy()
    {
        energy += energyRecoverRate;
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.transform.CompareTag("food"))
        {
            // eating food gives extra energy
            var foodEnergy = collision.transform.GetComponent<SpawnFood>().foodEnergy;
            energy += foodEnergy;
        }
    }

    public void SetRestObject(bool value)
    {
        if (rest != null)
            rest.SetActive(value);
    }

    public void SetEatObject(bool value)
    {
        if (eat != null)
            eat.SetActive(value);
    }

    public void SetRoamObject(bool value)
    {
        if (roam != null)
            roam.SetActive(value);
    }

}
