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

public enum EFaction
{
    player = 0,
    enemy,
    neutral,
    MAX,
}

public enum ECreatureAIState
{
    chillin = 0,
    aimless_wander,
    looking_for_work,
    moving_to_work,
    working,
    celebrating,
    fleeing,
    chasing,
}

public class LDBaseCreatureAI : LDGameActor
{
    private ECreatureAIState aiState;
    ECreatureAIState GetState() { return aiState; }
    private LDAIDoActionComponent[] listActionComponents;
    private LDAIDoActionComponent myCurrentAction;
    private GameManager theGame;

    private float fTimeUntilChangeFromChillin = 1.0f;

    [Header("Base AI Values")]
    
    [Tooltip("Time between deciding what to do next if just chillin'")]
    public Vector2 MinMaxChillTime;
    [Tooltip("Timer to return from celebrating to actual work")]
    public Vector2 MinMaxCelebrationTime;

    [Header("Debug Jibba")] 
    public TextMeshPro txtThinking;

    private float fStuckTime;

    private float fAintGotShitToDoTimer;

    protected LDGameActor myAnchorActor;

    protected override void Awake()
    {
        base.Awake();
        theGame = GameManager.GetGame();
    }

	// Use this for initialization
    protected override void Start()
	{
        base.Start();
	    aiState = ECreatureAIState.chillin;
        listActionComponents = GetComponentsInChildren<LDAIDoActionComponent>();

        theGame.theAIManager.RegisterAI(this);
	    if (listActionComponents != null)
	    {
	        for (int t = 0; t < listActionComponents.Length; t++)
	        {
	            listActionComponents[t].OnRegisterAI();
	        }
	    }

	}
	
	// Update is called once per frame
    protected override void Update()
    {
		base.Update();

        if (theGame.GetGameState() != EGameState.gameplay)
        {
            return;
        }

        
        if (txtThinking != null)
        {
            txtThinking.text = "";

            /*
            string str = GetState().ToString();
            if (myCurrentAction != null)
            {
                str += " " + myCurrentAction.GetThoughtText();
            }

            txtThinking.text = str;
            */
        }
        

        //bad bad lol
        if (aiState == ECreatureAIState.chillin)
        {
            fTimeUntilChangeFromChillin -= Time.deltaTime;
            if (fTimeUntilChangeFromChillin <= 0f)
            {
                for (int t = 0; t < listActionComponents.Length; t++)
                {
                    if (listActionComponents[t].FigureOutWhereToGo())
                    {
                        fAintGotShitToDoTimer = 0f;
                        //if the state transition works, then this is my new action
                        if (SetState(listActionComponents[t].myStateIfActive) == listActionComponents[t].myStateIfActive)
                        {
                            myCurrentAction = listActionComponents[t];
                        }
                    }
                    else
                    {
                        fAintGotShitToDoTimer += Time.deltaTime;

                        if (fAintGotShitToDoTimer > 2.0f)
                        {
                            //I guess we're chillin?
                            fTimeUntilChangeFromChillin = Random.Range(MinMaxChillTime.x, MinMaxChillTime.y);
                            myCurrentAction = null;
                            SetState(ECreatureAIState.chillin);

                            if (myMovementComponent != null)
                            {
                                Vector2 vSkyveOffDir = Random.insideUnitCircle * Random.Range(10, 20);
                                myMovementComponent.SetGoalPosition((Vector2)vWorldPos + vSkyveOffDir);
                            }
                            
                        }
                    }
                }
            }
        }
        else if( myCurrentAction != null )
        {
            //ask my current action if I should do something else
            SetState(myCurrentAction.TakeAction(GetState()));
        }

        //tell our move component what we wanna do 
        if (myMovementComponent != null)
        {
            if (myCurrentAction != null)
            {
                myMovementComponent.SetGoalPosition(myCurrentAction.WhereShouldIGo());
            }

            if (myMovementComponent.WasIStuckThisTick() && myCurrentAction != null)
            {
                fStuckTime += Time.deltaTime;
                if (fStuckTime > 3.0f)
                {
                    vWorldPos = myCurrentAction.WhereShouldIGo();
                }
            }
            else
            {
                fStuckTime -= Time.deltaTime;
                if (fStuckTime < 0f)
                {
                    fStuckTime = 0f;
                }
            }
        }
    }

    public ECreatureAIState SetState(ECreatureAIState newState)
    {
        aiState = newState;
        return aiState;
    }

    public LDAIDoActionComponent GetCurrentAction()
    {
        return myCurrentAction;
    }

    //Am I shooting at something? Running from something?
    public override LDGameActor GetLastTarget()
    {
        if (myCurrentAction == null)
        {
            return null;
        }

        return myCurrentAction.GetTargetActor();
    }

    public override void SetAnchorActor(LDGameActor anchor)
    {
        base.SetAnchorActor(anchor);

        if (myCurrentAction != null)
        {
            myCurrentAction.SetAnchorActor(anchor);
        }
    }
}
