package sillysagiri.scene;

import java.io.IOException;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import sillysagiri.BMF;
import sillysagiri.Scene;
import sillysagiri.Utils;

public class MainMenu extends Scene {
  private long counter = 0;
  private int state = 0;

  private Image img_bg;
  private Image img_bandai;
  private Image img_ez;
  private BMF bmf;
  
  public void Preload() {
    try {
      bmf = new BMF("/silly/bmf/nds12.png", "/silly/bmf/nds12.fnt");
      Image img_loop = Image.createImage("/m_2.png");
      img_bandai = Image.createImage("/c_0.png");
      img_ez = Image.createImage("/pEZ.png");

      int loopCount = (int)Math.ceil((float)Utils.GetScreenHeight()/img_loop.getHeight());

      img_bg = Image.createImage(img_loop.getWidth(), Utils.GetScreenHeight());
      Utils.Clear_Image(img_bg, 255, 108, 0);
      Graphics g_bg = img_bg.getGraphics();

      for (int i = 0; i<loopCount; i++)
        g_bg.drawImage(img_loop, 0, i*img_loop.getHeight(), Graphics.LEFT | Graphics.TOP);
    }
    catch(Exception e)
    {
      e.printStackTrace();
      sagiri.Set_Scene(new DeathError(e));
    }
  }

  public void Destroy() {
  }

  public void Update(long dt, Graphics g) {
    counter += dt;

    switch (state) {
      case 0:
      {
        Utils.Clear_Screen(g, 255, 108, 0);
        g.drawImage(img_bg, 0, 0, Graphics.TOP | Graphics.LEFT);

        g.drawImage(
          img_bandai,
          Utils.GetScreenWidth()/2, Utils.GetScreenHeight()/2,
          Graphics.VCENTER | Graphics.HCENTER);

        if (counter >= 600) counter = 0;
        if (counter < 400)
          g.drawImage(
            img_ez,
            Utils.GetScreenWidth()/2, (int)(Utils.GetScreenHeight()*0.75),
            Graphics.VCENTER | Graphics.HCENTER);

        bmf.Draw(g, "hello world\nthis is test text!\nthe fox jumped over the lazy dog", 5, 10);
        
        if (sagiri.input.IsKeyDownAny())
        {
          counter = 0;
          state = 1;
        }
      }
      break;
    
      default: break;
    }
  }
}

