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

public class ChargerBehaviour : EnemyController
{

    public int m_spacesPerTurn = 3;

    public enum ChargerState
    {
        IDLE,
        CHARGING,
        COOLDOWN
    }

    private enum ChargeDirection
    {
        UP,
        RIGHT,
        DOWN,
        LEFT
    }

    private ChargerState m_state;
    private ChargeDirection m_direction;

    public override Vector2 GetTarget()
    {
        RaycastHit2D rch2d = Physics2D.Raycast(transform.position, PlayerController.Instance.transform.position - transform.position);

        switch (m_state)
        {

            case ChargerState.IDLE:
                if (PlayerController.Instance.m_previousPosition.x == m_position.x || PlayerController.Instance.m_previousPosition.y == m_position.y)
                {
                    if (rch2d.transform != null)
                    {
                        if (rch2d.transform.gameObject == PlayerController.Instance.gameObject)
                        {
                            if (PlayerController.Instance.m_previousPosition.x < m_position.x)
                            {
                                m_direction = ChargeDirection.LEFT;
                            }
                            else if (PlayerController.Instance.m_previousPosition.x > m_position.x)
                            {
                                m_direction = ChargeDirection.RIGHT;
                            }
                            else if (PlayerController.Instance.m_previousPosition.y < m_position.y)
                            {
                                m_direction = ChargeDirection.UP;
                            }
                            else
                            {
                                m_direction = ChargeDirection.DOWN;
                            }
                            m_state = ChargerState.CHARGING;
                            return GetChargingTarget();
                        }
                    }
                }
                return GetIdleTarget();
            case ChargerState.CHARGING:
                if (PlayerController.Instance.m_previousPosition.x != m_position.x && PlayerController.Instance.m_previousPosition.y != m_position.y)
                {
                    m_state = ChargerState.COOLDOWN;
                    return GetChargingTarget();
                }
                if (rch2d.transform != null)
                {
                    if (PlayerController.Instance.m_previousPosition.x != m_position.x && PlayerController.Instance.m_previousPosition.y != m_position.y)
                    {
                        if (rch2d.transform.gameObject != PlayerController.Instance.gameObject)
                        {
                            m_state = ChargerState.COOLDOWN;
                        }
                    }
                }
                return GetChargingTarget();
            case ChargerState.COOLDOWN:
                m_state = ChargerState.IDLE;
                return GetChargingTarget();
        }
        return m_position;
    }

    Vector2 GetIdleTarget()
    {
        int moveDir = Random.Range(-3, 4);
        Vector2 newPos = m_position;
        switch (moveDir)
        {
            case 0:
                newPos.y--;
                break;
            case 1:
                newPos.x++;
                break;
            case 2:
                newPos.y++;
                break;
            case 3:
                newPos.x--;
                break;
        }
        return newPos;
    }

    Vector2 GetChargingTarget()
    {
        int maxDist = Utility.CalcDistance(PlayerController.Instance.m_previousPosition, m_position) < m_spacesPerTurn ? (int)Utility.CalcDistance(PlayerController.Instance.m_previousPosition, m_position) : m_spacesPerTurn;
        switch (m_direction)
        {
            case (ChargeDirection.UP):
                for (int i = maxDist; i > 0; --i)
                {
                    GameObject found = m_level.GetObjectAt(m_position.x, m_position.y - i);
                    if (found != null)
                    {
                        if (found.GetComponent<GridObject>().m_layer != gridLayer.STATIC && found.GetComponent<GridObject>().m_layer != gridLayer.ITEMS && found.GetComponent<GridObject>().m_layer != gridLayer.EXITS)
                        {
                            return new Vector2(m_position.x, m_position.y - i);
                        }
                    }
                }
                break;
            case (ChargeDirection.RIGHT):
                for (int i = maxDist; i > 0; --i)
                {
                    GameObject found = m_level.GetObjectAt(m_position.x + i, m_position.y);
                    if (found != null)
                    {
                        if (found.GetComponent<GridObject>().m_layer != gridLayer.STATIC && found.GetComponent<GridObject>().m_layer != gridLayer.ITEMS && found.GetComponent<GridObject>().m_layer != gridLayer.EXITS)
                        {
                            return new Vector2(m_position.x + i, m_position.y);
                        }
                    }
                }
                break;
            case (ChargeDirection.DOWN):
                for (int i = maxDist; i > 0; --i)
                {
                    GameObject found = m_level.GetObjectAt(m_position.x, m_position.y + i);
                    if (found != null)
                    {
                        if (found.GetComponent<GridObject>().m_layer != gridLayer.STATIC && found.GetComponent<GridObject>().m_layer != gridLayer.ITEMS && found.GetComponent<GridObject>().m_layer != gridLayer.EXITS)
                        {
                            return new Vector2(m_position.x, m_position.y + i);
                        }
                    }
                }
                break;
            case (ChargeDirection.LEFT):
                for (int i = maxDist; i > 0; --i)
                {
                    GameObject found = m_level.GetObjectAt(m_position.x - i, m_position.y);
                    if (found != null)
                    {
                        if (found.GetComponent<GridObject>().m_layer != gridLayer.STATIC && found.GetComponent<GridObject>().m_layer != gridLayer.ITEMS && found.GetComponent<GridObject>().m_layer != gridLayer.EXITS)
                        {
                            return new Vector2(m_position.x - i, m_position.y);
                        }
                    }
                }
                break;
        }
        return m_position;
    }
}
