package seabedexplorer.submersible.state.states;

import seabedexplorer.submersible.Submersible;
import seabedexplorer.submersible.equipment.locationtracking.Location;
import seabedexplorer.submersible.state.SubmersibleState;
import seabedexplorer.submersible.state.states.BusyActionState;
import seabedexplorer.submersible.state.states.BusyMoveState;
import seabedexplorer.submersible.state.states.DockedState;
import seabedexplorer.util.movement.MovementEmulation;

public class IdleState implements SubmersibleState
{
    
    private Submersible subContext;
    private MovementEmulation ME;
    
    @Override
    public boolean move(Location loc)
    {
        ME = new MovementEmulation(subContext, loc);
        subContext.changeState(new BusyMoveState(ME));
        ME.move();
        //Wait for thread to finish
        try
        {
            ME.getThread().join();
        }
        catch (Exception e)
        {
            System.out.println("Failed to Join Thread.");
        }
        //Change state to idle, since movement has finished
        subContext.changeState(new IdleState());
        return true;
    }
    
    
    @Override
    public boolean stopMove()
    {
        System.out.println("Not currently moving, cannot stop the movement.");
        return false;
    }
    
    @Override
    public boolean canMove()
    {
        return true;
    }

    @Override
    public void submersibleInstance(Submersible submersible)
    {
        subContext = submersible;
    }
    
    @Override
    public String toString()
    {
        return "Idle";
    }

    @Override
    public boolean returnToVessel()
    {
        //Location of the vessel is implied 0, 0, 0, as this is where a sub starts.
        ME = new MovementEmulation(subContext, new Location(0, 0, 0));
        //Change context to BusyMoveState as the sub is moving
        subContext.changeState(new BusyMoveState(ME));
        ME.move();
        //Wait for thread to finish
        try
        {
            ME.getThread().join();
        }
        catch (Exception e)
        {
            System.out.println("Failed to Join Thread.");
        }
        //Change state to Docked
        subContext.changeState(new DockedState());
        return true;
    }

    @Override
    public boolean collectWater()
    {
        //Change state to BusyAction
        subContext.changeState(new BusyActionState());
        //Do data collection
        subContext.update("ph");
        subContext.update("refraction");
        subContext.update("salinity");
        //Change state to InventoryFull, since it has just used the collection.
        subContext.changeState(new InventoryFullState());
        return true;
    }

    @Override
    public boolean collectMaterial()
    {
        //Change state to BusyAction
        subContext.changeState(new BusyActionState());
        //Do data collection
        subContext.update("scan");
        subContext.update("fluid");
        subContext.update("drill");
        try
        {
            Thread.sleep(2000);
        }
        catch(Exception e)
        {
            System.out.println("Thread failed to sleep.");
        }
        //Change state to InventoryFull, since it has just used the collection.
        subContext.changeState(new InventoryFullState());
        return true;
    }

    @Override
    public boolean collectMappingData()
    {
        //Change state to BusyAction
        subContext.changeState(new BusyActionState());
        //Do data collection
        subContext.update("laser");
        subContext.update("sonar");
        //Change state to Idle since this doesn't use inventory space.
        subContext.changeState(new IdleState());
        return true;
    }

}