package com.eParitta;

import java.io.IOException;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.ImageItem;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class eParitta extends MIDlet implements CommandListener{
	Display display;
	List list;
	Form imgForm[] = new Form[15];
	
	public Command exitCmd = new Command("Exit", Command.EXIT,1);
	public Command backCmd = new Command("Back", Command.BACK,1);
	
	public Command namakaragatha = new Command("Namakaragatha", Command.OK, 1);
	public Command puja_gatha = new Command("Puja-Gatha", Command.OK, 1);
	public Command pubbabhaganamakara = new Command("Pubbabhaganamakara", Command.OK, 1);
	public Command tisarana = new Command("Tisarana", Command.OK, 1);
	public Command pancasila = new Command("Pancasila", Command.OK, 1);
	public Command buddhanussati = new Command("Buddhanussati", Command.OK, 1);
	public Command dhammanussati = new Command("Dhammanussati", Command.OK, 1);
	public Command sanghanussati = new Command("Sanghanussati", Command.OK, 1);
	public Command saccakiriya_gatha = new Command("Saccakiriya-Gatha", Command.OK, 1);
	public Command mangala_sutta = new Command("Mangala Sutta", Command.OK, 1);
	public Command karaniyameta_sutta = new Command("Karaniyameta Sutta", Command.OK, 1);
	public Command brahmaviharapharana = new Command("Brahmaviharapharana", Command.OK, 1);
	public Command abhinhapaccavekkhana = new Command("Abhinhapaccavekkhana", Command.OK, 1);
	public Command ettavata = new Command("Ettavata", Command.OK, 1);
	
	public Ticker ticker = new Ticker("Sabbe Satta Bhavantu Sukhitatta");

	public eParitta() {
		try{
			//Untuk membuat layar
			for(int i=1;i<15;i++){
				imgForm[i] = new Form("");
			}
			
			//Untuk mengambil gambar
			Image img[] = new Image[15];
			for(int i=1;i<15;i++){
				img[i] = Image.createImage("/"+i+".jpg");
			}
			
			//Untuk memodifikasi tiap2 gambar
			ImageItem imgI[] = new ImageItem[15];
			for(int i=1;i<15;i++){
				imgI[i] = new ImageItem("",img[i],Item.LAYOUT_EXPAND,"");
			}
			
			//Untuk menaruh gambar pada tiap2 layar
			for(int i=1;i<15;i++){
				imgForm[i].append(imgI[i]);
			}
		}
		catch(IOException ioe){
			ioe.printStackTrace();
		}
		
		//Untuk menambah tombol Back pada tiap layar yang telah ditaruh gambar
		for(int i=1;i<15;i++){
			imgForm[i].addCommand(backCmd);
			imgForm[i].setCommandListener(this);
		}

		//Untuk membuat daftar2 sutta (khususnya yang sering digunakan untuk kebaktian Minggu)
		list = new List("Silahkan Pilih Sutta:", List.IMPLICIT);
		list.append("1. Namakara-Gatha",null);
		list.append("2. Puja-Gatha",null);
		list.append("3. Pubbabhaganamakara",null);
		list.append("4. Tisarana",null);
		list.append("5. Pancasila",null);
		list.append("6. Buddhanussati",null);
		list.append("7. Dhammanussati",null);
		list.append("8. Sanghanussati",null);
		list.append("9. Saccakiriya-Gatha",null);
		list.append("10.Mangala Sutta",null);
		list.append("11.Karaniyameta Sutta",null);
		list.append("12.Brahmaviharapharana",null);
		list.append("13.Abhinhapaccavekkhana",null);
		list.append("14.Ettavata",null);
		
		//Untuk memasang kalimat bergerak
		list.setTicker(ticker);
		
		//Untuk menambah tombol 'Exit'
		list.addCommand(exitCmd);
		
		list.addCommand(namakaragatha);
		list.addCommand(puja_gatha);
		list.addCommand(pubbabhaganamakara);
		list.addCommand(tisarana);
		list.addCommand(pancasila);
		list.addCommand(buddhanussati);
		list.addCommand(dhammanussati);
		list.addCommand(sanghanussati);
		list.addCommand(saccakiriya_gatha);
		list.addCommand(mangala_sutta);
		list.addCommand(karaniyameta_sutta);
		list.addCommand(brahmaviharapharana);
		list.addCommand(abhinhapaccavekkhana);
		list.addCommand(ettavata);
		
		list.setCommandListener(this);
		// TODO Auto-generated constructor stub
	}

	protected void destroyApp(boolean unconditional)
			throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub
	}

	protected void startApp(){
		//Menampilkan layar untuk memilih sutta ketika aplikasi dijalankan
		if(display==null){
			display = Display.getDisplay(this);
			display.setCurrent(list);
		}
		// TODO Auto-generated method stub

	}

	public void commandAction(Command c, Displayable d) {
//Untuk menjelaskan kegunaan tiap2 tombol yang telah dibuat
		
		//Untuk keluar dari aplikasi ketika tombol 'Exit' ditekan
		if(c==exitCmd){
			try{
				destroyApp(true);
			}
			catch(MIDletStateChangeException e){
				e.printStackTrace();
			}
			notifyDestroyed();
		}
		
		//Untuk kembali ke halaman memilih sutta
		if(c==backCmd){
			display = Display.getDisplay(this);
			display.setCurrent(list);
		}
		
		//Untuk menampilkan sutta yang dipilih
		if(c==List.SELECT_COMMAND){
			int idx = list.getSelectedIndex();
			for(int i=1;i<15;i++){
				if((idx+1)==i){
					display = Display.getDisplay(this);
					display.setCurrent(imgForm[i]);
				}
			}
		}
		// TODO Auto-generated method stub
	}
}
