using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;


class Tractor
{



    private int Spacing, sizeTile, Speed = 1, Rotation = 180, rotationSpeed = 5;
    private float tractorSpeed = 1;
    private String currentTask;

    private Vector2 Position, TargetPosition, Direction, Size, housePos;

    private Path path = new Path();
    private Random r = new Random();
    private Farm farm = new Farm();

    private SmartTractor smartTractor = new SmartTractor();
    private ScoreSystem scoreSystem = new ScoreSystem();


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


    public void init(Rectangle house, Input input)
    {
        sizeTile = input.getTileSize();
        Spacing = input.getSpacing();
        farm.init(new Vector2(100, (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / sizeTile) - 125 / sizeTile));
        Position = housePos;
        TargetPosition = new Vector2(house.X, house.Y);
    }

    // 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)
            {
                updateRotation(0);
            }
            else if (DeltaPosition.Y < 0)
            {
                updateRotation(1);
            }
        }
        else if (DeltaPosition.X > 0)
        {
            updateRotation(2);
        }
        else if (DeltaPosition.X < 0)
        {
            updateRotation(3);
        }

    }

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

        //farm.updateSize(Size, sizeTile, Spacing);
        for (int i = 0; i < Speed; i++) //Where all the choices the tractor does comes from
        {
            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, farm.getCrops(), Size, sizeTile, Spacing, scoreSystem.getScore(), Rotation);
            path = smartTractor.returnChoice();


            // Changes the status of a tile when the tractor reaches its destination
            if (path.getFinalDest() == null && farm.getCrop((int)Position.X / (sizeTile + Spacing), (int)Position.Y / (sizeTile + Spacing)).getCropTimer() == 1)
            {
                farm.setCropStatus(Position.X / (sizeTile + Spacing), Position.Y / (sizeTile + Spacing), Spacing);
            }

            TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
            updateDirection(Size, newPosition);
        }
        else
        {
            TargetPosition = path.Reduce().getCords() * (sizeTile + Spacing);
            updateDirection(Size, newPosition);
        }
    }

    private void updateRotation(int Destination)
    {
        if (Destination == 0)
        {
            if (Rotation == 0)
            {
                Direction = new Vector2(0, 1) * tractorSpeed;
                Position = Position + Direction;
            }
            else
            {
                if (Rotation == 0)
                {
                    //Do nothing
                }
                else if (Rotation  > 180)
                {
                    if (Rotation >= 360)
                    {
                        Rotation = 0;
                    }
                    Rotation = Rotation + rotationSpeed;
                }
                else if (Rotation <= 180 && Rotation > 0)
                {
                    Rotation = Rotation - rotationSpeed;
                }
            }
        }
        else if (Destination == 1)
        {
            if (Rotation == 180)
            {
                Direction = new Vector2(0, -1) * tractorSpeed;
                Position = Position + Direction;
            }
            else
            {
                if (Rotation == 180)
                {
                    //Do nothing
                }
                else if (Rotation >= 0 && Rotation < 180)
                {
                    Rotation = Rotation + rotationSpeed;
                }
                else if (Rotation < 360 && Rotation > 180)
                {
                    Rotation = Rotation - rotationSpeed;
                }
            }

        }
        else if (Destination == 2)
        {
            if (Rotation == 270)
            {
                Direction = new Vector2(1, 0) * tractorSpeed;
                Position = Position + Direction;
            }
            else
            {
                if (Rotation == 270)
                {
                    //Do nothing
                }
                else if (Rotation > 90 && Rotation < 270)
                {
                    Rotation = Rotation + rotationSpeed;
                }
                else if (Rotation < 90 || Rotation < 360)
                {
                    if (Rotation <= 0)
                    {
                        Rotation = 360;
                    }
                    Rotation = Rotation - rotationSpeed;
                }
            }
        }
        else if (Destination == 3)
        {
            if (Rotation == 90)
            {
                Direction = new Vector2(-1, 0) * tractorSpeed;
                Position = Position + Direction;
            }
            else
            {
                if (Rotation == 90)
                {
                    //Do nothing
                }
                else if ( Rotation < 270 && Rotation > 90)
                {
                    Rotation = Rotation - rotationSpeed;
                }
                else if (Rotation >= 0 || Rotation > 270)
                {
                    if (Rotation >= 360)
                    {
                        Rotation = 0;
                    }
                    Rotation = Rotation + rotationSpeed;
                }
            }

        }
    }

    public float getRotation()
    {
        return MathHelper.ToRadians(Rotation);

    }

    public void updateCrops()
    {
        for (int i = 0; i < Speed; i++)
        {
            farm.updateFarm(Size);
        }
    }

    public Vector2 getPos()
    {
        return Position;
    }
    
    public void increaseSpeed()
    {
        Speed++;
    }
    
    public void decreaseSpeed()
    {
        if (Speed > 0)
        {
            Speed--;
        }

    }

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

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

    public int getSpeed()
    {
        return Speed;
    }

    public float getTractorSpeed()
    {
        return tractorSpeed;
    }

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

    public Farm getFarm()
    {
        return farm;
    }

    public Vector2 getTargetPosition()
    {
        return TargetPosition;
    }

    public String getCurrentTask()
    {
        return currentTask;
    }

    public int getScore()
    {
        return scoreSystem.getScore();
    }

    public Path getPath()
    {
        return path;
    }

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