using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

class Tractor
{
    private int Spacing, sizeTile, Speed = 1;
    private float tractorSpeed = 1;
    private Vector2 Position, TargetPosition, Size, housePos;
    private Path path = new Path();
    private SmartTractor smartTractor = new SmartTractor();
    private HandleRotation handleRotation = new HandleRotation();

    public void updateSizing(Input input, int Status, Vector2 newHousePos, DayNightCycle Time)
    {
        Spacing = input.getSpacing();
        sizeTile = input.getTileSize();
        Size = input.getSize();
        updatePosition(input.getSize(), Status);
        housePos = newHousePos;
        smartTractor.UpdateCrops(Speed);
    }

    public void init(Rectangle house, Input input)
    {
        sizeTile = input.getTileSize();
        Spacing = input.getSpacing();
        Position = housePos;
        TargetPosition = new Vector2(house.X, house.Y);
        smartTractor.init();
    }

    // Runs when the tractor reaches a tile
    private void updateDirection(Vector2 Size, Vector2 newPosition)
    {
        Vector2 DeltaPosition = TargetPosition - Position;
        
        if (DeltaPosition.X == 0)
        {
            if (DeltaPosition.Y == 0)
            {
                calculateNewPath(newPosition);
            }
            else if (DeltaPosition.Y > 0)
            {
                Position = handleRotation.UpdatePosition(0, tractorSpeed, Position);
            }
            else if (DeltaPosition.Y < 0)
            {
                Position = handleRotation.UpdatePosition(1, tractorSpeed, Position);
            }
        }
        else if (DeltaPosition.X > 0)
        {
            Position = handleRotation.UpdatePosition(2, tractorSpeed, Position);
        }
        else if (DeltaPosition.X < 0)
        {
            Position = handleRotation.UpdatePosition(3, tractorSpeed, Position);
        }
    }

    public void updatePosition(Vector2 Size, int Status) // updates the position
    {

        //farm.updateSize(Size, sizeTile, Spacing);
        for (int i = 0; i < Speed; i++)
        {
            updateDirection(Size, Position);
        }
    }

    public void calculateNewPath(Vector2 newPosition)
    {
        if (path.getCount() == 0)
        {
            if (Position.X / (sizeTile + Spacing) > Size.X)
            {
                Position.X = (Size.X - 1) * (sizeTile + Spacing);
            }
            else if (Position.Y / (sizeTile + Spacing) > Size.Y)
            {
                Position.Y = (Size.Y - 1) * (sizeTile + Spacing);
            }
            smartTractor.updateMap(Position, housePos, Size, sizeTile, Spacing, handleRotation.getRotation());
            path = smartTractor.returnChoice();
            TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
            updateDirection(Size, newPosition);
        }
        else
        {
            TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
            updateDirection(Size, newPosition);
        }
    }

    public void setSpeed(int newSpeed)
    {
        Speed = newSpeed;
    }

    public void setTractorSpeed(tractorPositionCorrector corrector)
    {
        tractorSpeed = corrector.getTractorSpeed();
        Position = corrector.getPosition();
    }


    public void setPos(Vector2 newPos)
    {
        Position = newPos;
    }

    public void setNewHousePos(Vector2 pos, bool newState)
    {
        smartTractor.setNewHousePos(pos, newState);
    }

    public float getRotation()
    {
        return MathHelper.ToRadians(handleRotation.getRotation());
    }

    public Farm getFarm()
    {
        return smartTractor.getFarm();
    }

    public Vector2 getPos()
    {
        return Position;
    }

    public int getSpeed()
    {
        return Speed;
    }

    public float getTractorSpeed()
    {
        return tractorSpeed;
    }

    public Vector2 getTargetPosition()
    {
        return TargetPosition;
    }

    public Path getPath()
    {
        return path;
    }

    public void drawInventory(Input input, SpriteBatch spriteBatch, SpriteFont Bold, Cargo itemStorageDefined)
    {
        smartTractor.drawInventory(input, spriteBatch, Bold, itemStorageDefined);
    }

    public Inventory getInventory()
    {
        return smartTractor.getInventory();
    }
}