using UnityEngine;
using UnityEngine.Events;

public enum InteractablesState{Enabled, Disabled}

[RequireComponent(typeof(Collider))]
public class Interactables : MonoBehaviour{

    const int MAXCOUNT = 3;

    public Element element;
    public UnityEvent reactionCallback;
    public UnityEvent repairCallback;

    InteractablesState state = InteractablesState.Disabled;
    int count = 1;

    public InteractablesState getState(){
        return state;
    }

    bool isWeakness(Element e){
        if(element == Element.Fire && e == Element.Ice){
            return true;
        }

        if(element == Element.Ice && e == Element.Fire){
            return true;
        }

        return false;
    }


    void OnTriggerEnter(Collider other) {
        Projectile p = other.GetComponent<Projectile>();
        if(p!= null){
            if(count >= MAXCOUNT && state == InteractablesState.Disabled){
                state = InteractablesState.Enabled;
                repair();
            }else if(count <= 0 && state == InteractablesState.Enabled){
                state = InteractablesState.Disabled;
                reaction();
            }

            if(isWeakness(p.getElement())){
                if(count > 0 )
                    count --;
            }else{
                if(count < MAXCOUNT)
                    count ++;
            }
            
        }
    }

    void reaction(){
        reactionCallback.Invoke();
    }

    void repair(){
        repairCallback.Invoke();
    }

}
