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

public class Hit : MonoBehaviour
{

    public int ResourceID;
    public int BuildingID;
    public int MaxHealth;
    public string AudioSourceName;
    public int Health;

    [HideInInspector]
    public AudioSource AudioSource;

    void Start()
    {
        if (!string.IsNullOrEmpty(AudioSourceName))
            AudioSource = GameObject.Find(AudioSourceName).GetComponent<AudioSource>();
        Health = MaxHealth;
    }


    void Update()
    {
        
    }

    public void OnHit(GameObject level)
    {
        if (ResourceID >= 0)
        {
            AudioSource?.Play();
            Health--;
            var upgradeLevel = level.GetComponent<Level>().Matrix.Get(Global.PositionToIndex(transform.position).Identifier).Upgrade;
            var upgradePerc = Mathf.Clamp(upgradeLevel / 10f, 0f, 1f);
            var benefit = (int)Mathf.Floor(upgradePerc * MaxHealth);
            if (Health <= benefit)
            {
                Health = 0;
                level.GetComponent<Level>().NotifyDestruction(gameObject, ResourceID, upgradeLevel);
                Destroy(gameObject);
            }
        }
        else if (BuildingID >= 0)
        {
            gameObject.GetComponent<Production>().TryAdd();
        }
    }

}
