package com.softgames.farmninja;


/* FarmNinjaMidlet.java
*
* (c) Softgames Deutschland, - 2013
* Confidential and proprietary.
* 
* Main midlet representing entry point for the application.
*/
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;

import com.kiwavi.mobiledb.MobileHDBR;
import com.kiwavi.mobiledb.RMSMobileStore;
import com.kiwavi.mobiledb.RMSUsingApp;
import com.kiwavi.mobileutils.MobileUtils;

public class FarmNinjaMidlet extends MIDlet implements RMSUsingApp
{
  Display myDisplay;
  Alert alrtMessage;
  FMNPlayScreen playScreen;
  FMNSplashScreen splashScreen;
  FMNSoftgamesScreen softgamesScreen;
  FMNLanguageMenu languageMenu;
  FMNMainMenu mainMenu;
  FMNSoundOption soundOption;
  FMNMenuOptions menuOptions;
  FMNTutorialScreen tutorialScreen;
  FMNAboutScreen aboutScreen;
  FMNGameAssets gameAssets;
  FMNGameText gameText;
  
  Image imggeneralbg;
  
  // dimensions
  int app_width, app_height;
  
  // databases
  MobileHDBR hdbrSettings; 
  String GAME_LANGUAGE = "", APP_VERSION ="2.11";
  boolean firsttime = false, soundon = true, juststarted = true;
  int highscore = 0;
  
  // stop resume
  Displayable myts;
  boolean gameispaused = false;
  
  public FarmNinjaMidlet()
  {
	myDisplay = Display.getDisplay(this);
	alrtMessage = new Alert("MESSAGE");	
	try {
	  hdbrSettings = new MobileHDBR(this, "SETTINGS");		
	  imggeneralbg = Image.createImage("/bg.jpg");
	  gameAssets = new FMNGameAssets(this);
		softgamesScreen = new FMNSoftgamesScreen(this);
		app_width =softgamesScreen.getWidth();
		app_height = softgamesScreen.getHeight();
	  
	  imggeneralbg = MobileUtils.resizeImage(imggeneralbg, app_width, app_height);
	}
	catch (Exception ex)
	{
	  showMessage("Error at FMN.init() " + ex);
	}
    System.out.println("size of screen " + app_height + "   " + app_width);
  }
  
  public void startApp()
  {
	try {
	  // System.out.println("Start app called ...");
	  if (gameispaused == true && myts != null && myts instanceof FMNPlayScreen)
	  {
		// System.out.println("REsuming from pause ...");
		FMNPlayScreen fmyts = (FMNPlayScreen)myts;
		fmyts.repaint();
		fmyts.pausegame = false;
		gameispaused = false;
		myts = null;
		return;
	  }
      myDisplay.setCurrent(softgamesScreen);
      hdbrSettings.load();
	}
	catch (Exception ex)
	{
	  System.out.println("\nError at StartApp " + ex);
	}	
  }
  
  public void pauseApp()
  {
	// System.out.println("Going into pause ...");	  
	myts = myDisplay.getCurrent(); 
	if (myts instanceof FMNPlayScreen)
	{
	  FMNPlayScreen fmyts = (FMNPlayScreen)myts;
	  fmyts.pausegame = true;
	  gameispaused = true;
	}
  }
  
  protected void destroyApp(boolean arg0) // throws MIDletStateChangeException 
  {
	// TODO Auto-generated method stub
	notifyDestroyed();
  }
  
  public void showMessage(String message)
  {
    // System.out.println(message);      
    alrtMessage.setString(message);
    alrtMessage.setType(AlertType.INFO);
    alrtMessage.setTimeout(Alert.FOREVER);
    myDisplay.setCurrent(alrtMessage);
  }  
  
  
  class SUSeq extends Thread
  {
	 public void run()
	 {
	   try {
		 sleep(3000);
		 gotoSplashScreen();
		 softgamesScreen = null;
		 sleep(3000);
		 if (firsttime == true)
		 {
		   gotoLanguageMenu();
		 }
		 else {
		   gotoMainMenu();
		 }
		 splashScreen = null;
	   }
	   catch (Exception ex)
	   {
		 System.out.println("Error at startup sequence " + ex);
	   }
	 }
	  
  }
  
  void gotoSplashScreen()
  {
	splashScreen = new FMNSplashScreen(this);
	myDisplay.setCurrent(splashScreen);
  }
  
 
  void gotoAboutScreen()
  {
	aboutScreen = new FMNAboutScreen(this);
	myDisplay.setCurrent(aboutScreen);
  }  

  void gotoSoftgamesScreen()
  {
	myDisplay.setCurrent(softgamesScreen);
  }

  void gotoTutorialScreen()
  {  
	tutorialScreen = new FMNTutorialScreen(this);
	myDisplay.setCurrent(tutorialScreen);
  }
  
  void gotoLanguageMenu()
  {  
	languageMenu = new FMNLanguageMenu(this);
	myDisplay.setCurrent(languageMenu);
  }
  
  void gotoMainMenu()
  { 
	mainMenu = new FMNMainMenu(this);
	myDisplay.setCurrent(mainMenu);
  }  
  
  void gotoMenuOptions()
  { 
	menuOptions = new FMNMenuOptions(this);
	myDisplay.setCurrent(menuOptions);
  } 
  
  /* void gotoSoundOption()
  { 
	soundOption = new FMNSoundOption(this);
	myDisplay.setCurrent(soundOption);
  }  */
  
  void gotoPlayScreen()
  {	
	playScreen = new FMNPlayScreen(this);
	myDisplay.setCurrent(playScreen);
	Thread animThread = new Thread(playScreen);
	animThread.start();
  } 
  
  public String getLanguage()
  {
	return GAME_LANGUAGE;
  }
  
  public String getAppName() 
  {
	return "SOFTGAMES FARMNINJA";
  }
  
  public String getAppVersion() 
  {
	return APP_VERSION;
  }  
  
  public void setLanguage(String gameLanguage)
  {
	try {
	this.GAME_LANGUAGE = gameLanguage;
	this.hdbrSettings.set("GAME_LANGUAGE", gameLanguage);
	this.hdbrSettings.persistData();
	}
	catch (Exception ex)
	{
	  System.out.println("Error at FarmNinjaMidlet.setLanguage() " + ex);
	}
  }

  public void loadRMSData(String dbname, RMSMobileStore pxstore) 
  {
	RecordStore rsData = null;
	String dataString = "";
	try {
	  rsData = RecordStore.openRecordStore(dbname, true);
	  if (rsData.getNumRecords() >= 1)
	  {
		dataString = new String(rsData.getRecord(1));
		pxstore.RMSDataLoaded(dataString);
		// System.out.println("\n\nData loaded successfully " + dataString);
		rsData.closeRecordStore();
	  }
	  else {
		rsData.closeRecordStore();
		pxstore.RMSDataLoaded(dataString);
	  }
	  
	}
	catch (Exception ex)
	{
	  showMessage("Error at FarmNinjaMidlet.loadRMSData():openRecordStore " + ex);
	  try {
		if (rsData != null)
		rsData.closeRecordStore();
	  }
	  catch (Exception ex2)
	  {
		showMessage("Error at FarmNinjaMidlet.loadRMSData():closeRecordStore " + ex2);
	  }
	}
	
  }

public void persistRMSData(String dbname, String data) 
{
  RecordStore rsData = null;
  try {
    rsData = RecordStore.openRecordStore(dbname, true);
	byte[] bdata = data.getBytes();
    if (rsData.getNumRecords() >= 1)
	{
	  rsData.setRecord(1,  bdata, 0, bdata.length);
	  // System.out.println("\n\nData saved successfully " + data);
	}
    else {
      rsData.addRecord(bdata, 0, bdata.length);
    }
	rsData.closeRecordStore();
  }
  catch (Exception ex)
  {
    showMessage("\nError at FarmNinjaMidlet.saveRMSData():openRecordStore " + ex);
	  try {
		if (rsData != null)
		rsData.closeRecordStore();
	  }
	  catch (Exception ex2)
	  {
		showMessage("\nError at FarmNinjaMidlet.saveRMSData():closeRecordStore " + ex2 + "  \n" + rsData);
	  }
	}
}

  public void continueAfterLoad(String message) 
  {
	try {
	  // TODO Auto-generated method stub
	  System.out.println("\n\nContinuing after load " + message + "   " + this.hdbrSettings);
	  if (message.equals("SETTINGS"))
	  {
		this.highscore = MobileUtils.getNumberValue(""+this.hdbrSettings.get("HIGH_SCORE"));
		// System.out.println("The high score is " + highscore);
		
	    this.GAME_LANGUAGE = ""+this.hdbrSettings.get("GAME_LANGUAGE");
	    if (this.GAME_LANGUAGE == null || this.GAME_LANGUAGE.toUpperCase().equals("NULL"))
	    {
	      this.GAME_LANGUAGE = "ENG";
	      firsttime = true;
	    }
	    
	    gameText = new FMNGameText(this);

	    SUSeq suseq = new SUSeq();
	    suseq.start();
	    return;
	  }
 
    }
	catch (Exception ex2)
	{
	  showMessage("Error at FarmNinjaMidlet.continueAfterLoad() " + ex2);
	}
	
  }
  
}