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 int counter = 0;
  private int state = 0;

  private Image img_logo;

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

  public void Destroy()
  {
    img_logo = null;
  }

  public void Update()
  {
    // TODO: use time based counter instead
    counter++;
    if (counter > 100) counter = 0;

    switch (state) {
      case 0:
        {
          if (counter > 25)
          {
            // img_logo = Image.createImage("/pEZ.png");
            counter = 0;
            state = 1;
          }
        }
        break;
    
      default: break;
    }
  }

  public void Draw(Graphics g)
  {
    Utils.Clear_Screen(g, 73, 0, 0);
    switch (state) {
      case 0:
      {
        int halfW = img_logo.getWidth()/2;
        int halfH = img_logo.getHeight()/2;
        float r = Easing.CubicEaseOut((float)counter/25);
        int pos = (int)(((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:
      {
        g.drawImage(
          img_logo,
          Utils.GetScreenWidth()/2,
          Utils.GetScreenHeight()/2,
          Graphics.VCENTER | Graphics.HCENTER
        );

        break;
      }
    
      default: return;
    }
  }
  
}
