using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Grunt_gun;
using Grunt_bite;
using System;
using UnityEngine.UIElements;



public class StateMachine : MonoBehaviour
{
    [SerializeField] private StateMachine stateMachine;
    [SerializeField] private Grunt_bite.BaseState biteActiveState;
    [SerializeField] private Grunt_gun.BaseState gunActiveState;
    internal object activeState;
    [SerializeField] private Path path;
    private Enemy_1 enemy;
    private string currentState;
    public void Initialize(Path path, EnemyType enemyType)
    {
        switch (enemyType)
        {

            case EnemyType.Grunt_bite:
                ChangeBiteState(new Grunt_bite.PatrolState(path));
                break;
            case EnemyType.Grunt_gun:
                ChangeGunState(new Grunt_gun.PatrolState(path, this));
                break;
            default:
                Debug.LogError("Unsupported enemy type");
                break;
        }
    }
    
    // Start is called before the first frame update
    void Start()
    {

    }
    
    // Update is called once per frame
    void Update()
    {
        Debug.Log("Current active state: " + (gunActiveState != null ? gunActiveState.GetType().Name : "None"));
        // Null checks performing actions on active states
        if (biteActiveState != null)
        {
            biteActiveState.Perform();
        }


        if (gunActiveState != null)
        {
            gunActiveState.Perform();
        }
    }

    public void ChangeBiteState(Grunt_bite.BaseState newState)
    {
        if (biteActiveState != null)
        {
            biteActiveState.Exit();
        }

        biteActiveState = newState;

        if (biteActiveState != null)
        {
            biteActiveState.stateMachine = this;

            if (biteActiveState is Grunt_bite.PatrolState patrolstate)
            {
                biteActiveState = new Grunt_bite.PatrolState(patrolstate.path, GetComponent<Enemy_2>());
                biteActiveState.Enter();
            }
            else
            {
                Debug.LogError("newState to PatrolState failed");
            }
        }


    }
    public void ChangeGunState(Grunt_gun.BaseState newState)
    {
        Debug.Log("Changing gun state. gunActiveState: " + (gunActiveState != null ? gunActiveState.GetType().Name : "null"));
        Debug.Log("New state: " + newState.GetType().Name);
        // Check if there is a currently active state
        

        if (gunActiveState != null)
        {
            gunActiveState.Exit();// Call the Exit method of the currently active state
        }
        else
        {
            Debug.LogWarning("gunActiveState is null in ChangeGunState method");
        }

        gunActiveState = newState;// Set the new state as the active state
        currentState = newState.GetType().Name;
        if (gunActiveState != null)// Check if the new state is not null
        {
            // Set the state machine and enemy references for the new state
            gunActiveState.stateMachine = this;
            gunActiveState.enemy = GetComponent<Enemy_1>();

            // Check if the new state is a PatrolState
            if (gunActiveState is Grunt_gun.PatrolState)
            {
                Grunt_gun.PatrolState patrolState = gunActiveState as Grunt_gun.PatrolState;
                if (patrolState != null)
                {
                    gunActiveState = new Grunt_gun.PatrolState(patrolState.path, GetComponent<Enemy_1>());
                    gunActiveState.Enter();
                }
                else
                {
                    Debug.LogError("Failed to cast gunActiveState to PatrolState in ChangeGunState method");
                }
            }
            // Check if the new state is an AttackState
            else if (GetComponent<Enemy_1>().CanSeePlayer())
            {
                gunActiveState = new Grunt_gun.AttackState(enemy);
                gunActiveState.Enter();
            }
            else // check if the new state is an AttackState
            {
                Grunt_gun.PatrolState patrolState = gunActiveState as Grunt_gun.PatrolState;
                if (patrolState != null)
                {
                    gunActiveState = new Grunt_gun.PatrolState(patrolState.path, GetComponent<Enemy_1>());
                    gunActiveState.Enter(); // Enter AttackState
                }
                else
                {
                    Debug.LogError("Failed to cast gunActiveState to PatrolState in ChangeGunState method");
                }
            }
            
            { //if (stateMachine != null && stateMachine.gunActiveState != null)
              //{
              //Enemy_1 enemy = GetComponent<Enemy_1>();
              //if (enemy.CanSeePlayer())
              //{
              //Debug.Log("Player detected in " + gunActiveState.GetType().Name + ". Transitioning to AttackState");
              //stateMachine.ChangeGunState(new Grunt_gun.AttackState(GetComponent<Enemy_1>()));
              //}
              //}

                //if (gunActiveState.GetType() == typeof(Grunt_gun.AttackState))
                //{
                //Grunt_gun.AttackState attackState = (Grunt_gun.AttackState)gunActiveState;
                //if (attackState.CanSeePlayer())
                //{
                //Debug.Log("Player detected in " + gunActiveState.GetType().Name + ". Transitioning to AttackState");
                //stateMachine.ChangeGunState(new Grunt_gun.AttackState(GetComponent<Enemy_1>()));
                //}
                //}

                //if (gunActiveState != null && gunActiveState.CanSeePlayer())
                //{
                //Debug.Log("player detected in" + gunActiveState.GetType().Name +".transitioning to attackState");
                //StateMachine stateMachine = new StateMachine();
                //stateMachine.ChangeGunState(new Grunt_gun.AttackState(GetComponent<Enemy_1>()));
                //}
            }
        }
        else
        {
                Debug.LogError("newState is null in ChangeGunState method");
        }
        
    }
}

public enum EnemyType 
{
    Grunt_bite,
    Grunt_gun,

}