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

public class AbilityRange : MonoBehaviour
{


    List<ExWhyCell> cellsInRange;
    List<GameObject> rangeVisuals;
    List<BattleCharacterObject> charactersInRange;
    int range;
    ExWhyCell epicentre;
    ExWhy grid;

    //The prefab used to show range
    GameObject visual;
    
    List<ExWhyCell> visited;

    //Finds all cells in  range. 
    //It just uses a two dimensional for loop adding a cell to each quandrant. 

    public List<ExWhyCell> findCellsInRange()
    {
        int epiX = epicentre.xPosition;
        int epiY = epicentre.yPosition;

        for (int x = 0; x <= range; ++x)
        {
            for (int y = 0; y <= range; ++y)
            {
                ExWhyCell possibleCell;
                if (x + y <= range) {

                    if (!(epiX + x >= grid.xMax || (epiY + y >= grid.yMax))) {
                        possibleCell = grid.gridCells[epiX + x, epiY + y];
                        if (!cellsInRange.Contains(possibleCell))
                        {
                            cellsInRange.Add(grid.gridCells[epiX + x, epiY + y]);
                        }
                    }

                    if (!(epiX - x < 0 || (epiY - y < 0)))
                    {
                        possibleCell = grid.gridCells[epiX - x, epiY - y];
                        if (!cellsInRange.Contains(possibleCell))
                        {
                            cellsInRange.Add(grid.gridCells[epiX - x, epiY - y]);
                        }
                     }

                    if (!((epiX - x < 0)|| (epiY + y >= grid.yMax)))
                    {
                        possibleCell = grid.gridCells[epiX - x, epiY + y];
                        if (!cellsInRange.Contains(possibleCell))
                        {
                            cellsInRange.Add(grid.gridCells[epiX - x, epiY + y]);
                        }
                    }


                    if (!((epiX + x  >= grid.xMax) || (epiY - y < 0)))
                    {
                        possibleCell = grid.gridCells[epiX + x, epiY - y];
                        if (!cellsInRange.Contains(possibleCell))
                        {
                            cellsInRange.Add(grid.gridCells[epiX + x, epiY - y]);
                        }
                    }
                }
            }
        }
        return cellsInRange;
    }

    //This "Jumper Range" is a bredth-First search for finding the walkable range of an ability. 
    //Basically, a path finding algorithm
    //Todo : Add "visted" code to make more efficient
    public void jumperRange(ExWhyCell currentCell, int jumpsLeft)
    {
        // visited = new List<ExWhyCell>();
        cellsInRange.Add(currentCell);


        if (jumpsLeft == 0 || !currentCell.walkable)
        {
            return;
        }

        //Up
        if (currentCell.yPosition != grid.yMax){
            if (grid.gridCells[currentCell.xPosition, currentCell.yPosition + 1].walkable) {
                jumperRange(grid.gridCells[currentCell.xPosition, currentCell.yPosition], jumpsLeft - 1);
            }
        }

        //Down
        if (currentCell.yPosition != 0)
        {
            if (grid.gridCells[currentCell.xPosition, currentCell.yPosition - 1].walkable)
            {
                jumperRange(grid.gridCells[currentCell.xPosition, currentCell.yPosition], jumpsLeft - 1);
            }
        }

        //Left
        if (currentCell.xPosition != 0)
        {
            if (grid.gridCells[currentCell.xPosition - 1, currentCell.yPosition].walkable)
            {
                jumperRange(grid.gridCells[currentCell.xPosition, currentCell.yPosition], jumpsLeft - 1);
            }
        }

        //Right
        if (currentCell.xPosition != grid.xMax)
        {
            if (grid.gridCells[currentCell.xPosition + 1, currentCell.yPosition].walkable)
            {
                jumperRange(grid.gridCells[currentCell.xPosition, currentCell.yPosition], jumpsLeft - 1);
            }
        }



    }

    public void initalize(int rnge, ExWhyCell epcntr, ExWhy grd)
    {
        range = rnge;
        epicentre = epcntr;
        visual = Resources.Load<GameObject>("RangeVisual");
        grid = grd;
        cellsInRange = new List<ExWhyCell>();
        rangeVisuals = new List<GameObject>();
    }

    public void destroyVisual()
    {
        foreach (GameObject go in rangeVisuals)
        {
            Destroy(go);
        }
    }

    //Spawns a blue visial effect for the range of abilities/ 
    public void spawnVisuals()
    {
        foreach(ExWhyCell cell in cellsInRange)
        {
            rangeVisuals.Add(Instantiate(visual, cell.cellGO.transform));
        }

    }

    //Finds the characters in already foudn cells. 
    public List<BattleCharacterObject> findCharactersInRange()
    {
        List<BattleCharacterObject> output = new List<BattleCharacterObject>();
        foreach (ExWhyCell cell in cellsInRange)
        {
            if (cell.occupier)
            {
                output.Add(cell.occupier);
            }
            charactersInRange = output;
        }
            return output;
    }
}