import java.io.*;
import java.util.*;
import javax.microedition.io.*;

import com.nttdocomo.ui.*;
import com.nttdocomo.opt.ui.*;

public class Clock extends MApplication {
	boolean isActive;
	boolean isConcierge;
	C c;

	public Clock() {
		isConcierge = (getLaunchType() == LAUNCHED_AS_CONCIERGE);
		c = new C(this, isConcierge);
	}

    public void start() {
        Display.setCurrent(c);
        c.start();

		if (!isConcierge) { 
			// 待受けじゃない場合はイベントが発生しないので自分でまわす。
			while (true) {
				try {
					Thread.sleep(1000);
				} catch (Exception e) {}
				c.draw();
			}
		} else {
			// 待受けの場合は clocktick に任せてOK
			setClockTick(true);
		}
    }

	public void processSystemEvent(int type, int param) {
		if (type == CLOCK_TICK_EVENT) {
			c.draw();
		}
		if (type == FOLD_CHANGED_EVENT) {
			c.changeFoldState();
		}
		try {
			sleep();
		} catch (Exception e) {}
	}
}

class C extends Canvas {
	static int w = Display.getWidth();
	static int h = Display.getHeight();
	static int sw = SubDisplay.getWidth();
	static int sh = SubDisplay.getHeight();

	boolean isConcierge;
	boolean ready;
    static MApplication app;
	static Image img;
	static Image subImg = Image.createImage(sw, sh);

	Graphics g2 = getGraphics();
	Graphics g2sub = subImg.getGraphics();

	int hour, min, sec;

	public void draw2keta(Graphics g, int x, int y, int number) {
		int x1 = (number / 10 % 10) * 6;
		int x2 = (number % 10) * 6;
		
		g.drawImage(img, x, y, x1, 25, 6, 12);
		g.drawImage(img, x + 6, y, x2, 25, 6, 12);
	}

	public void calcTime() {
		int x = (int)((System.currentTimeMillis() / 1000 + 32400) % 86400);
		sec = x % 60;
		min = x / 60 % 60;
		hour = x / 3600;
	}

	public synchronized void refresh() {
		draw();
	}
    
    public synchronized void processEvent(int type, int param) {
        if (type == Display.KEY_RELEASED_EVENT) {
			if (param == Display.KEY_SOFT2)  app.terminate();
			if (param == Display.KEY_IAPP && isConcierge) {
				try {
					app.deactivate();
				} catch (Exception e) {}
			}
		}
    }
    
	// 折りたたみ状態変化。
	// 現在はまったく意味のないメソッドだが、
	// 開いたときにいろいろ動くとかするときには必要
    public synchronized void changeFoldState() { 
		refresh();
	}

	public synchronized void draw() {
		calcTime();

		g2.lock();
		g2.setColor(g2.getColorOfRGB(255,255,255)); 
		g2.fillRect(0, 0, w, h);

		g2sub.setColor(g2.getColorOfRGB(255,255,255)); 
		g2sub.fillRect(0, 0, sw, sh);
		g2sub.drawImage(img, 0, 0, 0, 0, 96, 25);
		draw2keta(g2sub, 54, 13, hour);
		g2sub.drawImage(img, 66, 13, 60, 25, 6, 12);
		draw2keta(g2sub, 72, 13, min);

		g2.drawImage(subImg, (w - sw) / 2, (h - sh) / 2);
		SubDisplay.setImage(subImg);
		g2.unlock(true);
	}

    public void paint(Graphics g) {
		if (ready) draw();
    }
    
    C(MApplication app, boolean isc) {
		this.app = app;
		this.isConcierge = isc;
		setSoftLabel(Frame.SOFT_KEY_1, "");
		setSoftLabel(Frame.SOFT_KEY_2, "おわり");
    }

    public void start() {
		MediaImage mImg = MediaManager.getImage("resource:///aibon.gif");
		try {
			mImg.use();
		} catch (com.nttdocomo.io.ConnectionException e) {
			System.out.println("MediaImage.use() に失敗");
		}
		img = mImg.getImage();
		ready = true;
		draw();
	}
}
