package sillysagiri.scene;
import java.io.IOException;

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

import sillysagiri.Easing;
import sillysagiri.Scene;
import sillysagiri.Utils;

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

  private Image img_logo = null;
  private Image img_ez = null;
  private Image img_theworld = null;
  private Image img_vol1 = null;
  private Image img_project = null;

  public Intro() throws IOException
  {
    img_logo = Image.createImage("/t_1.png");
    img_ez = Image.createImage("/pEZ.png"); 
  }

  public void Destroy()
  {
    img_logo = null;
    img_ez = null;
    img_theworld = null;
    img_vol1 = null;
    img_project = null;
  }

  public void Update(long dt, Graphics g)
  {
    counter += dt;
    
    switch (state) {
      case 0:
      {
        if (counter > 1800)
        {
          counter = 0;
          state = 1;
          break;
        }

        Utils.Clear_Screen(g, 73, 0, 0);

        int halfW = img_logo.getWidth()/2;
        int halfH = img_logo.getHeight()/2;
        float r = Easing.CubicEaseInOut((float)counter/1800);
        int pos = (int)Math.ceil(((Utils.GetScreenWidth()/2)+halfW) * r) - halfW;

        g.drawRegion(
          img_logo, 
          0, 0, img_logo.getWidth(), halfH, Sprite.TRANS_NONE, 
          pos, Utils.GetScreenHeight()/2-1, 
          Graphics.BOTTOM | Graphics.HCENTER);

        g.drawRegion(
          img_logo, 
          0, halfH, img_logo.getWidth(), img_logo.getHeight()-halfH, Sprite.TRANS_NONE, 
          Utils.GetScreenWidth() - pos, Utils.GetScreenHeight()/2, 
          Graphics.TOP | Graphics.HCENTER);
      }
      break;

      case 1:
      {
        if (counter > 500)
        {
          counter = 0;
          state = 2;

          try {
            img_logo = Image.createImage("/t_0.png");
            img_theworld = Image.createImage("/t_2.png");
            img_project = Image.createImage("/t_3.png");
            img_vol1 = Image.createImage("/t_4.png");
          }
          catch(IOException e) {}
        }
      }
      break;

      case 2:
      {
        Utils.Clear_Screen(g, 146, 36, 0);
        g.drawImage(
          img_logo, 
          Utils.GetScreenWidth()/2, Utils.GetScreenHeight()/2-35,
          Graphics.HCENTER | Graphics.VCENTER);
          
          g.drawImage(
            img_theworld,
          Utils.GetScreenWidth()/2, Utils.GetScreenHeight()/2,
          Graphics.HCENTER | Graphics.TOP);
          
          g.drawImage(img_project, 5, Utils.GetScreenHeight()-5, Graphics.LEFT | Graphics.BOTTOM);
          g.drawImage(img_vol1, 5, 5, Graphics.LEFT | Graphics.TOP);
          
          if (counter >= 1000) counter = 0;
          if (counter < 600)
            g.drawImage(
              img_ez,
              Utils.GetScreenWidth()/2, (int)(Utils.GetScreenHeight()*0.75),
              Graphics.VCENTER | Graphics.HCENTER);
        }
      break;

      default: break;
    }
  }
}
