package devices.LCDDotMatrix;

import java.util.Timer;
import java.util.TimerTask;


public class LCDString {
	private int rowindex;
	private int maxchar = 16;
	private LCDStringEvent javaevent;
	private String message;
	private Timer lcdtimer;
	private TimerTask lcdtask;
	private int menutag = 0;
	private final long scrolltime = 1000;			// ms
	private int startindex = 0;
	private boolean arahmaju = true;
	
	/**
	 * Create LCD String object
	 * @param row_index : which row, start from 0
	 * @param maximum_char : how much characters per row. Default value is 16
	 */
	public LCDString(int row_index, int maximum_char) {
		this.rowindex = row_index;
		if (maximum_char > 0) this.maxchar = maximum_char;
		lcdtimer = new Timer();
		
	}
	
	/**
	 * Get Row index associated 
	 * @return row index
	 */
	public int RowIndex() {
		return rowindex;
	}
	
	/**
	 * Get Maximum characters in this row
	 * @return maximum characters
	 */
	public int MaximumChars() {
		return maxchar;
	}
	
	/**
	 * Close LCDString and cancel all timer tasks
	 */
	public void Close() {
		if (lcdtimer instanceof Timer) {
			lcdtimer.cancel();
			lcdtimer = null;
		}
		if (lcdtask instanceof TimerTask) {
			lcdtask.cancel();
			lcdtask = null;
		}
	}
	
	public String CurrentContent() {
		if (message!=null) {
			return message;
		}
		return "";
	}
	
	/**
	 * Update this row content
	 * @param msg : string message
	 * @param currentmenutag : menu tag, for multiple menu selection. If not sure, set 0
	 */
	public void UpdateContent(String msg, int currentmenutag) {
		this.message = msg;
		this.menutag = currentmenutag;
		int msglen = this.message.length();
		
		if (lcdtimer instanceof Timer) {
			lcdtimer.cancel();
			lcdtimer.purge();
			lcdtimer = null;
		}
		if (lcdtask instanceof TimerTask) {
			lcdtask.cancel();
			lcdtask = null;
		}
		
		if (msglen<maxchar) {
			// lebih pendek dari maxchar, harus padding
			byte[] bb1 = this.message.getBytes();		// source
			byte[] bb2 = new byte[maxchar];				// destination
			
			// isi bb2 dengan spasi
			for(int ii=0;ii<bb2.length;ii++) bb2[ii] = 32;
			
			// copy dari bb1 ke bb2 sesuai panjang bb1
			for(int ii=0;ii<bb1.length;ii++) bb2[ii] = bb1[ii];
			
			raise_update_lcd(new String(bb2));
		} else if (msglen == maxchar) {
			// sama dengan maxchar
			raise_update_lcd(this.message);
		} else {
			// lebih panjang, butuh timer untuk scrolling
			
			startindex = 0;			// will start from column 0
			
			// re-create timertask
			lcdtask = new TimerTask() {
				@Override
				// Pattern String scroll ke kiri
				public void run() {
					if (arahmaju) {
						int count = maxchar;
						String result = "";
						int startbaca = startindex;
						while (count>0) {
							result+= message.charAt(startbaca);
							count-= 1;
							if (startbaca < (message.length()-1)) {
								startbaca+=1;								
								}
							else {
								startbaca=0;
								startindex = message.length();		// stop, dan mulai dari awal
								break;
								}
								
							
						}
						
						
						raise_update_lcd(result);
						//BA.Log("LCD TimerTask row="+rowindex+", hasil="+result);
						
						if (startindex< (message.length()-1))
							startindex+=1;
						else
							startindex = 0;
					}
				}
			};
			
			// recreate lcdtimer
			lcdtimer = new Timer();
			lcdtimer.scheduleAtFixedRate(lcdtask, 0, scrolltime);		
			//BA.Log("Message panjang "+msglen+" character, pakai timer");		
		}
	}
	
	/**
	 * Set Event handler for this object
	 * @param lcd_event : LCDStringEvent object
	 */
	public void SetJavaEvent(LCDStringEvent lcd_event) {
		this.javaevent = lcd_event;
	}
	
	private void raise_update_lcd(String msg) {
		if (javaevent instanceof LCDStringEvent) {
			javaevent.UpdateLCD(rowindex, msg, menutag);
		}
	}
}
