using System;
using Microsoft.Xna.Framework;
using System;

class SmartTractor
{
    private Crops[,] crops;
    private Vector2 housePos;
    private Vector2 tractorPos;
    private Vector2 Size;
    private Vector2 Target;
    private Path path;
    private int tileSize;
    private int Score;
    private int Spacing;
    private Random r = new Random();
    private Astar astar = new Astar();


    //What to do next
    public Path returnChoice(int task)
    {
        if (task == 0)  
        {
            //To the house
            getTargetPosition((int)housePos.X / (tileSize + Spacing), (int)housePos.Y / (tileSize + Spacing));
        }
        else
        {
            //To the fields
            getTargetPosition(r.Next(0, (int)Size.X), r.Next(0, (int)Size.Y));
        }
        astar.update(crops, Size, tractorPos / (tileSize + Spacing), housePos / (tileSize + Spacing), Target/(tileSize+Spacing));
        //astar.FindPath();
        return astar.FindPath();
    }






    //Updates the variables every frame
    public void updateMap(Vector2 newTractorPos, Vector2 newHousePos, Crops[,] newCropsStatus, Vector2 newSize, int newTileSize, int newSpacing, int newScore)
    {
        crops = newCropsStatus;
        housePos = newHousePos;
        tractorPos = newTractorPos;
        Size = newSize;
        tileSize = newTileSize;
        Spacing = newSpacing;
        Score = newScore;

        
    }

    private void getTargetPosition(int x, int y)
    {
        Target = new Vector2(x, y) * (tileSize + Spacing);
    }






    /*
    //Only for testing without obstacles
    private void createPath()
    {
        path = new Path();
        Vector2 targetPos = Target / (tileSize + Spacing);
        Vector2 currentPath = tractorPos / tileSize;
        currentPath.X = (float)Math.Round(currentPath.X);
        currentPath.Y = (float)Math.Round(currentPath.Y);
        do
        {
            if (currentPath.X == targetPos.X)
            {
                //found X pos
                if (currentPath.Y == targetPos.Y)
                {
                    //found y pos
                }
                else if (currentPath.Y < targetPos.Y)
                {
                    currentPath = new Vector2(currentPath.X, currentPath.Y + 1);
                    path.setNode(currentPath, crops);
                }
                else if (currentPath.Y > targetPos.Y)
                {
                    currentPath = new Vector2(currentPath.X, currentPath.Y - 1);
                    path.setNode(currentPath, crops);
                }
            }
            else if (currentPath.X < targetPos.X)
            {
                currentPath = new Vector2(currentPath.X + 1, currentPath.Y);
                path.setNode(currentPath, crops);
            }
            else if (currentPath.X > targetPos.X)
            {
                currentPath = new Vector2(currentPath.X - 1, currentPath.Y);
                path.setNode(currentPath, crops);
            }
        } while (currentPath != targetPos);


    }

    public void setNode(Vector2 newNode, Crops[,] Crop)
    {

        nodes[Count] = new Nodes(Crop[(int)newNode.X, (int)newNode.Y].getCostOnMovement(), newNode);
        Count++;
    }

    */
}