import java.awt.*;
import java.applet.*;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.lang.String;
import java.lang.Math;

public class BinaryClock extends Applet implements Runnable {
	Choice c = new Choice();
	Thread clockThread;
	TextField cc = new TextField(17);
	Image img, img0, img1;
	int width, height;
	int refresh = 1000;
	String datatypes[];
	Objekt[] obj;	
	boolean running = true;
	
	public BinaryClock() {
		String[] dt = {
			"Second", "Minute:Second", "Digital (H:i:s)", "Date (Y:m:d)"
		};
		this.datatypes = new String[dt.length];
		for(int i=0;i<dt.length;i++)
			this.datatypes[i] = dt[i];
	}

	public void init() {
		img0 = getImage(getDocumentBase(),"balls0.jpg");
		img1 = getImage(getDocumentBase(),"balls1.jpg");
		MediaTracker imageTracker = new MediaTracker(this);
		imageTracker.addImage (img0, 0);
		imageTracker.addImage (img1, 0);
		try {
			imageTracker.waitForAll();
		}
		catch (InterruptedException e) {
			System.err.println(e);
		}
		this.width = img0.getWidth(this);
		this.height = img0.getHeight(this);
		for(int i=0;i<this.datatypes.length;i++)
			c.addItem(this.datatypes[i]);
		add(c);
		add(cc);
		this.obj = new Objekt[1];
		this.obj[0] = new Objekt(0, 0, 0);
		clockThread = new Thread(this);
		clockThread.start();
	}
	
	public void destroy() {
		running = false;
		clockThread = null;
	}
	
	public void run() {
		while (running) {
			Date d = new Date();
			DateFormat sekundaFormat = new SimpleDateFormat("s");
			DateFormat minutaFormat = new SimpleDateFormat("m");
			DateFormat hodinaFormat = new SimpleDateFormat("h");
			DateFormat denFormat = new SimpleDateFormat("d");
			DateFormat mesiacFormat = new SimpleDateFormat("M");
			DateFormat rokFormat = new SimpleDateFormat("y");
			String sekunda = sekundaFormat.format(d);
			String minuta = minutaFormat.format(d);
			String hodina = hodinaFormat.format(d);
			String den = denFormat.format(d);
			String mesiac = mesiacFormat.format(d);
			String rok = rokFormat.format(d);
			String time = 
				den+". "
				+mesiac+". "
				+rok+" "
				+hodina+":"
				+minuta+":"
				+sekunda;
			cc.setText(time);
			this.drawclock(this.datatypes[c.getSelectedIndex()], sekunda, minuta, hodina, den, mesiac, rok);
			try {
				clockThread.sleep(refresh);
			} catch (InterruptedException e) {
				System.out.println(e);
			}
		} 
	}

	public void paint(Graphics g) {
		for(int i=0;i<this.obj.length;i++)
			if (this.obj[i].b == 0)
				g.drawImage(img0, this.obj[i].x, this.obj[i].y, this);
			else
				g.drawImage(img1, this.obj[i].x, this.obj[i].y, this);
	}
	
	public String fixbinary(String ret) {
		if (ret.length() == 1) return "0000000" + ret;
		if (ret.length() == 2) return "000000" + ret;
		if (ret.length() == 3) return "00000" + ret;
		if (ret.length() == 4) return "0000" + ret;
		if (ret.length() == 5) return "000" + ret;
		if (ret.length() == 6) return "00" + ret;
		if (ret.length() == 7) return "0" + ret;
		if (ret.length() == 8) return ret;
		return "00000000";
	}
	
	public void drawclock(String s, String sekunda, String minuta, String hodina, String den, String mesiac, String rok) {
		/* BigDEF Start */
		String _sekunda = Integer.toBinaryString(Integer.parseInt(sekunda));
		_sekunda = this.fixbinary(_sekunda);
		String _minuta = Integer.toBinaryString(Integer.parseInt(minuta));
		_minuta = this.fixbinary(_minuta);
		String _hodina = Integer.toBinaryString(Integer.parseInt(hodina));
		_hodina = this.fixbinary(_hodina);
		String _den = Integer.toBinaryString(Integer.parseInt(den));
		_den = this.fixbinary(_den);
		String _mesiac = Integer.toBinaryString(Integer.parseInt(mesiac));
		_mesiac = this.fixbinary(_mesiac);
		if (rok.substring(0,1) == "0" && rok.length() == 2)
			rok = rok.substring(1,2);
		String _rok = Integer.toBinaryString(Integer.parseInt(rok));
		_rok = this.fixbinary(_rok);
		/* BigDEF End */
		int clsize = 8;
		int[] basex = {50, 150, 250};
		int basey = 50; 
		int[] posx = {1, 1, 1, 1, 0, 0, 0, 0};
		int[] posy = {3, 2, 1, 0, 3, 2, 1, 0};
		int objsize = 0;
		if (s == "Second") objsize = clsize;
		if (s == "Minute:Second") objsize = clsize*2;
		if (s == "Digital (H:i:s)") objsize = clsize*3;
		if (s == "Date (Y:m:d)") objsize = clsize*3;
		this.obj = new Objekt[objsize];
		for(int i=0;i<this.obj.length;i++) {
			double xdelenie = i / clsize;
			double delenie = (clsize+i) % clsize;
			int dvys = (int)Math.ceil(delenie);
			int px = basex[(int)Math.ceil(xdelenie)] + posx[dvys] * this.width;
			int py = basey + posy[dvys] * this.height;
			int yndex = clsize-dvys-1;
			char chr = ' ';
			/* BigIF Start */
			if (s == "Second")
				chr = _sekunda.charAt(yndex);
			if (s == "Minute:Second" && i < clsize)
				chr = _minuta.charAt(yndex);
			if (s == "Minute:Second" && i >= clsize)
				chr = _sekunda.charAt(yndex);
			if (s == "Digital (H:i:s)" && i < clsize)
				chr = _hodina.charAt(yndex);
			if (s == "Digital (H:i:s)" && i < clsize*2 && i >= clsize)
				chr = _minuta.charAt(yndex);
			if (s == "Digital (H:i:s)" && i < clsize*3 && i >= clsize*2)
				chr = _sekunda.charAt(yndex);
			if (s == "Date (Y:m:d)" && i < clsize)
				chr = _rok.charAt(yndex);
			if (s == "Date (Y:m:d)" && i < clsize*2 && i >= clsize)
				chr = _mesiac.charAt(yndex);
			if (s == "Date (Y:m:d)" && i < clsize*3 && i >= clsize*2)
				chr = _den.charAt(yndex);
			/* BigIF End */
			String schr = "" + chr;
			this.obj[i] = new Objekt(px, py, Integer.parseInt(schr));
		}
		repaint();
	}

}
