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

public class BlockManager : MonoBehaviour
{
    public enum BlockType {None, Barricade, Door, Wood, Ammo, FireCamp};
    public BlockType blockType = BlockType.None;
    public float health = 100;
    public float healthRate = 5;
    private float healthTime = 5;
    private Animation animation;
    private PlayerManager player;
    private bool action = false;

    // Start is called before the first frame update
    void Start()
    {
        player = FindObjectOfType<PlayerManager>();
        if (blockType == BlockType.Door)
        {
            animation = this.GetComponent<Animation>();
        }
        else if (blockType == BlockType.Wood)
        {
            healthTime = healthRate;
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (blockType == BlockType.Wood)
        {
            if (health < 100 && healthTime < Time.time)
            {
                health += 10;
                healthTime = healthRate + Time.time;
            }
        }
    }

    public void Action()
    {
        if (blockType == BlockType.Door)
        {
            if (!animation.isPlaying)
            {
                if (!action)
                {
                    animation["Door"].speed = 1;
                    animation["Door"].time = 0;
                    action = true;
                }
                else
                {
                    animation["Door"].speed = -1;
                    animation["Door"].time = animation["Door"].length;
                    action = false;
                }
                animation.Play("Door");
            }
        }
        else if (blockType == BlockType.Wood)
        {
            if (health > 0 && player.barricadeMaterials < 50) {
                health -= 10;
                player.barricadeMaterials += (player.barricadeMaterials <= 40 ? 10 : (50 - player.barricadeMaterials));
            }
        }
    }
}
