package otherSupport;

import java.text.DecimalFormat;


/** A little common status outputter, for classes that don't know if they're going to be taking a long time or not */
public final class StatusOutput {
	
	//private static DecimalFormat fmt = new DecimalFormat("##.###");  
		
	private long initTime;
	private long n;
	private String firstText;
	private long lastTime;
	private boolean active;
	private int nOnLine;
	
	private int attentionSpan,updatePeriod;
	
	private void defaultTiming(){
		attentionSpan =  Integer.parseInt(SettingsManager.defaultGlobal().getProperty("minerva.user.attentionSpanMilisecs", "5000"));
		updatePeriod = Integer.parseInt(SettingsManager.defaultGlobal().getProperty("minerva.user.updatePeriod", "1000"));
	}
	
	public StatusOutput(Class caller, long n) {
		this.firstText = caller.getCanonicalName();
		this.n = n;
		this.initTime = System.currentTimeMillis();
		this.active = false;
		defaultTiming();
	}
	
	public StatusOutput(String firstText, long n) {
		this.firstText = firstText;
		this.n = n;
		this.initTime = System.currentTimeMillis();
		this.active = false;
		defaultTiming();
	}
	
	public StatusOutput(Class caller, long n, int attentionSpan, int updatePeriod) {
		this.firstText = caller.getCanonicalName();
		this.n = n;
		this.initTime = System.currentTimeMillis();
		this.active = false;
		this.attentionSpan = attentionSpan;
		this.updatePeriod = updatePeriod;
	}
		
	public StatusOutput(String firstText, long n, int attentionSpan, int updatePeriod) {
		this.firstText = firstText;
		this.n = n;
		this.initTime = System.currentTimeMillis();
		this.active = false;
		this.attentionSpan = attentionSpan;
		this.updatePeriod = updatePeriod;
	}
	
	public final void doStatus(int i){
		long now = System.currentTimeMillis();
		if(!active && (now - initTime) > attentionSpan){
			active = true;
			System.out.print(firstText + ": ");
			nOnLine = 2; //space for the text, ish
			lastTime = 0;
		}
		if(active && (now - lastTime) > updatePeriod){
			System.out.print((i*100.0 / n) + "% ");
			nOnLine++;
			if(nOnLine >= 10){
				System.out.println();
				nOnLine = 0;
			}
			lastTime = System.currentTimeMillis();
		}
	}

	public void done() {
		if(active)
			System.out.println("Done [" + firstText + "]\n");
	}

}
