#include <Arduino.h>
#include <U8g2lib.h>
#include <NonBlockingRtttl.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include "mobilesystem.h"
// U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // All Boards without Reset of the Display
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
const char* PROGMEM tname[3]={
  "Tetris",
  "Arkanoid",
  "Mario"

};
const char* PROGMEM tones[3]={
  "tetris:d=4,o=5,b=160:e6,8b,8c6,8d6,16e6,16d6,8c6,8b,a,8a,8c6,e6,8d6,8c6,b,8b,8c6,d6,e6,c6,a,2a,8p,d6,8f6,a6,8g6,8f6,e6,8e6,8c6,e6,8d6,8c6,b,8b,8c6,d6,e6,c6,a,a",
  "Arkanoid:d=4,o=5,b=140:8g6,16p,16g.6,2a#6,32p,8a6,8g6,8f6,8a6,2g6",
  "mario:d=4,o=5,b=100:16e6,16e6,32p,8e6,16c6,8e6,8g6,8p,8g,8p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,16p,8c6,16p,8g,16p,8e,16p,8a,8b,16a#,8a,16g.,16e6,16g6,8a6,16f6,8g6,8e6,16c6,16d6,8b,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16c7,16p,16c7,16c7,p,16g6,16f#6,16f6,16d#6,16p,16e6,16p,16g#,16a,16c6,16p,16a,16c6,16d6,8p,16d#6,8p,16d6,8p,16c6"

};
int menupos=0;
int playscr=0;
#define audio_width 32
#define audio_height 24
static unsigned char audio_bits[] = {
   0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00,
   0x00, 0xf0, 0x03, 0x00, 0x00, 0xf0, 0x0f, 0x00, 0x00, 0x90, 0x0f, 0x00,
   0x00, 0x10, 0x0e, 0x00, 0xf0, 0xff, 0xff, 0x00, 0x08, 0x10, 0x88, 0x01,
   0x08, 0x10, 0x88, 0x02, 0x08, 0x10, 0x88, 0x04, 0x08, 0x10, 0x88, 0x0f,
   0x08, 0x1f, 0x08, 0x0a, 0x88, 0x1e, 0x08, 0x08, 0x88, 0x1f, 0x08, 0x0c,
   0x08, 0x8f, 0x0f, 0x08, 0x08, 0x40, 0x0f, 0x0c, 0x08, 0xc0, 0x0f, 0x08,
   0x08, 0x80, 0x07, 0x0c, 0x08, 0x00, 0x00, 0x08, 0x08, 0x00, 0x00, 0x0c,
   0x48, 0x55, 0x55, 0x09, 0xf0, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00 };

void setup(void) {
  pinMode(12,INPUT_PULLUP);
  pinMode(13,INPUT_PULLUP);
  pinMode(14,INPUT_PULLUP);
  pinMode(15,INPUT_PULLUP);
  pinMode(26,OUTPUT);
  u8g2.begin();
  //u8g2.enableUTF8Print();		// enable UTF8 support for the Arduino print() function
  //u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
  //u8g2.setFontDirection(0);



}
int redraw=1;
int curPlay=0;
void loop(void) {
    u8g2.clearBuffer();
  u8g2.setFont(mobilesystem);
  if(digitalRead(15)==0){
    menupos++;
    if(rtttl::isPlaying())
      rtttl::stop();
    delay(10);
    redraw=1;
  }
  if(digitalRead(14)==0){
    menupos--;
    if(rtttl::isPlaying())
      rtttl::stop();
    delay(10);
    redraw=1;
  }
  if(menupos>2){
    menupos=0;
  }
  if(menupos<0){
    menupos=2;
  }
  if(digitalRead(12)==0){
    if(!rtttl::isPlaying()){
      rtttl::begin(26, tones[menupos]);
      playscr=1;
      redraw=1;
    }
    //playscr==1;
    
  }
  if(digitalRead(13)==0){
    if(rtttl::isPlaying()){
      rtttl::stop();
    }else if(playscr==1){
      playscr=0;
    }
    redraw=1;
  }
  if(rtttl::isPlaying()!=curPlay){
    curPlay=rtttl::isPlaying();
    redraw=1;
  }
  //playscr==1;
  if(redraw){
  if(playscr==0){
  for(int i=0;i<3;i++){
    u8g2.setCursor(0,(i+1)*12);
    u8g2.println(tname[i]);
    if(i==menupos){

      u8g2.setDrawColor(2);
      u8g2.drawBox(0,i*12,127,12);
      u8g2.setDrawColor(1);
    }
    

  }
  //u8g2.drawXBMP(48,20,32,24,audio_bits);
  }else{
    u8g2.setCursor(64-(u8g2.getStrWidth(tname[menupos])/2),12);
    u8g2.print(tname[menupos]);
    u8g2.drawXBMP(48,20,32,24,audio_bits);
  }
  if(!rtttl::isPlaying()){
    u8g2.setCursor(0,63);
    u8g2.print("Play");
    u8g2.setCursor(127-u8g2.getStrWidth("Back"),63);
    u8g2.print("Back");
  }else{
    u8g2.setCursor(127-u8g2.getStrWidth("Stop"),63);
    u8g2.print("Stop");
  }

  //u8g2.drawStr(0,10,"Hello World!M");
  //u8g2.drawXBMP(10, 10, 40, 18, logo_48X16); //繪圖
  u8g2.sendBuffer();
  redraw=0;
  }
  if(rtttl::isPlaying()){
    rtttl::play();
  }
}