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

public abstract class AI_Action : MonoBehaviour
{
    public string actionName = "Undefined Action";
    public float cost = 1.0f;
    public GameObject target;
    public string targetTag;
    public float duration = 0;
    public AIState[] preConditions;
    public AIState[] afterEffects;
    public NavMeshAgent agent;

    public Dictionary<string, int> preConditionsDict;
    public Dictionary<string, int> effectsDict;

    public AI_Inventory inventory;
    public AIStates agentWorldStates;

    public bool isRunning = false;

    public AI_Action()
    { 
        preConditionsDict = new Dictionary<string, int>();
        effectsDict = new Dictionary<string, int>();
    }

    public void Awake()
    {
        agent = GetComponent<NavMeshAgent>();
        
        if(preConditions != null)
        {
            foreach (AIState w in preConditions)
            {
                preConditionsDict.Add(w.key, w.value);
            }
        }
        
        if (afterEffects != null)
        {
            foreach (AIState w in afterEffects)
            {
                effectsDict.Add(w.key, w.value);
            }
        }
        
        inventory = GetComponent<AI_Agent>().inventory;
        agentWorldStates = GetComponent<AI_Agent>().agentWorldStates;
    }

    public bool IsAchievable()
    {
        return true;
    }
    
    public bool IsAchievableGiven(Dictionary<string, int> conditions)
    {
        foreach(KeyValuePair<string, int> p in preConditionsDict)
        {
            if (!conditions.ContainsKey(p.Key))
                return false;
        }
        return true;
    }

    public abstract bool PrePerform();
    public abstract bool PostPerform();
}