package edu.se459grp4.project.cleansweep;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * This manages the various clean sweeps that are currently cleaning the floor. Will handle creating new clean sweeps and 
 * will either start them on a cleaning cycle or end their cleaning cycles
 * 
 * @author Group 4
 * @version 1.8
 *
 */
public class CleanSweepManager {
    
    private static CleanSweepManager instance ;
    private static final Double INIT_POWER_LEVEL = 100.0;
    private static final int INIT_VACUUM_CAPACITY = 1000;
    
    private int csIdCounter = 0;
    private Map<Integer,CleanSweep> allSweeps = new HashMap<>();
    private Map<Integer, Thread> workingSweeps = new HashMap<>(); //a record of the sweeps currently running a cleaning cycle
    
    private CleanSweepManager(){}
    
    /**
     * Get a reference to the instance of the CleanSweepManager Singleton
     * 
     * @return a reference to the single instance
     */
    public static synchronized CleanSweepManager getInstance(){
        if(instance == null){
            instance = new CleanSweepManager();
        }
        return instance;
    }
    
    /**
     * Creates a new clean sweep and starts it off on the given x and y coordinates. Keeps track of this sweep 
     * in its internal record
     * 
     * @param x the x coordinate for the new sweep
     * @param y the y coordinate for the new sweep
     * @return the id that was given to the sweep which was just created
     */
    public synchronized int createCleanSweep(int x,int y){
        int nID = csIdCounter++;
        CleanSweep cs = new CleanSweep(nID,INIT_POWER_LEVEL,INIT_VACUUM_CAPACITY,x,y);
        allSweeps.put(nID, cs);
        return nID;
    }
    
    /**
     * Gets the CleanSweep that is associated with the given id number
     * 
     * @param nID the id number for the CleanSweep you want to obtain
     * @return the CleanSweep that has the corresponding id number
     */
    public synchronized CleanSweep getCleanSweep(int nID){
        return allSweeps.get(nID);
    }

    /**
     * Gets the CleanSweep corresponding to the given id and starts it on a cleaning cycle
     * 
     * @param nID the id of the clean sweep which should be started on its cleaning cycle
     */
    public synchronized void startCleanCycle(int nID){
        CleanSweep cs = allSweeps.get(nID);
        
        if(cs == null){
            return;
        }
        
        Navigator navController = new Navigator(cs);
        
        Thread t = new Thread(navController);
        workingSweeps.put(nID, t);
        t.start();
    }
    
    /**
     * Gets the CleanSweep corresponding to the given id and stops it from cleaning further
     * 
     * @param nID the id of the clean sweep which should be stopped
     */
    public synchronized void stopCleanCycle(int nID){
        Thread cs = workingSweeps.get(nID); //get the clean sweep to stop
        
        if(cs == null){
            return;
        }
        
        cs.interrupt(); //interrupt that Thread
    }
    
    /**
     * Provides a list of all the ID numbers of the clean sweeps currently in service
     * 
     * @return a list of the integer ID numbers of all the current clean sweeps
     */
    public synchronized List<Integer> getAllSweeps(){
    	return new ArrayList<Integer>(allSweeps.keySet());
    }

}

@@ -468,17 +468,16 @@
 instance
- 
 ;%0A    pr
@@ -495,17 +495,17 @@
 c final 
-D
+d
 ouble IN
@@ -603,16 +603,23 @@
 private 
+static 
 int csId
@@ -629,12 +629,8 @@
 nter
- = 0
 ;%0A  
@@ -631,32 +631,38 @@
 er;%0A    private 
+final 
 Map%3CInteger,Clea
@@ -678,34 +678,16 @@
 llSweeps
- = new HashMap%3C%3E()
 ;%0A    pr
@@ -692,16 +692,22 @@
 private 
+final 
 Map%3CInte
@@ -736,26 +736,8 @@
 eeps
- = new HashMap%3C%3E()
 ; //
@@ -807,36 +807,298 @@
     
-private CleanSweepManager()%7B
+/*%0A     * initilize the counter to start with 0 as first id for a sweep, initialize the maps that hold%0A     * the info about all the working sweeps%0A     */%0A    private CleanSweepManager()%7B%0A    %09csIdCounter = 0;%0A    %09allSweeps = new HashMap%3C%3E();%0A        workingSweeps = new HashMap%3C%3E();%0A    
 %7D%0A  
@@ -3614,23 +3614,16 @@
 rayList%3C
-Integer
 %3E(allSwe

Fix code smell issues in CleanSweepManager. Make some of the instance variables final for thread safety, move some instance variable initializations into the constructor