package businessmodel.assemblyline;

import businessmodel.observer.Observer;
import businessmodel.observer.Subject;

import java.util.Iterator;

public class MaintenanceState implements AssemblyLineState, Observer {

	private AssemblyLine assemblyLine;

    private boolean isReady = false;
	
	/**
	 * Constructor for maintenance state of the assembly line
	 * @param assemblyLine
	 */
	public MaintenanceState(AssemblyLine assemblyLine){
		if (assemblyLine == null) throw new IllegalStateException("Not a valid assembly line.");
		this.assemblyLine = assemblyLine;
        assemblyLine.subscribeObserver(this);
	}
	
	@Override
	public void markAssemblyLineAsBroken() {
		this.assemblyLine.setState(this.assemblyLine.getBrokenState());
		// wait indefinitely
	}

	@Override
	public void markAssemblyLineAsOperational() {
		this.assemblyLine.setState(this.assemblyLine.getOperationalState());
	}

	@Override
	public void markAssemblyLineAsMaintenance() {
		// wait four hours
		
	}

    @Override
    public boolean canPlaceOrder() {
            return this.isReady;
    }

    @Override
    public void initialize() {
        this.assemblyLine.getAssemblyLineScheduler().tempName(4);
        this.isReady = false;
        this.update(this.assemblyLine);
    }

    @Override
    public String toString() {
        return "Maintenance";
    }

    @Override
    public void update(Subject subject) {
        Iterator<WorkPost> posts = this.assemblyLine.getWorkPostsIterator();
        boolean ready = false;
        while (posts.hasNext()){
            WorkPost post = posts.next();
            ready = post.isCompleted();
        }
        if (ready){
            // the assembly line is empty
            // now shift 4 hours and transition to Operational state
            this.assemblyLine.getAssemblyLineScheduler().increaseCurrentTime(4);
            this.assemblyLine.setState(this.assemblyLine.getOperationalState());
        }
        // in the other case just wait till the line is empty.
    }
}

@@ -283,16 +283,55 @@
 = false;
+%0A%0A    private boolean isActive = false;
 %0A%09%0A%09/**%0A
@@ -1146,24 +1146,54 @@
 tialize() %7B%0A
+        this.isActive = true;%0A
         this
@@ -1457,24 +1457,56 @@
  subject) %7B%0A
+        if (this.isActive)%7B%0A    
         Iter
@@ -1578,16 +1578,20 @@
         
+    
 boolean 
@@ -1597,32 +1597,36 @@
  ready = false;%0A
+    
         while (p
@@ -1654,16 +1654,20 @@
         
+    
 WorkPost
@@ -1684,24 +1684,28 @@
 sts.next();%0A
+    
             
@@ -1732,34 +1732,42 @@
 eted();%0A        
-%7D%0A
+    %7D%0A    
         if (read
@@ -1770,16 +1770,20 @@
 ready)%7B%0A
+    
         
@@ -1828,16 +1828,20 @@
         
+   
+ 
 // now s
@@ -1889,16 +1889,20 @@
 l state%0A
+    
         
@@ -1970,24 +1970,67 @@
 entTime(4);%0A
+                this.isActive = false;%0A    
             
@@ -2098,16 +2098,30 @@
 ate());%0A
+            %7D%0A
         

i am sorry