var GroveSensor = require('jsupm_grove');
var touchSensor = require('jsupm_ttp223');
var button = new touchSensor.TTP223(2);
var motor = new GroveSensor.GroveLed(3);
var GCL = require("jsupm_grovecircularled");
var circle = new GCL.GroveCircularLED(6, 5);
var LCD  = require("jsupm_i2clcd");
var lcd = new LCD.Jhd1313m1(6, 0x3E, 0x62);
var f3js = require('f3js')
  , c = f3js.createContainer()
  , x = 10
  , y = 10
  , width = 160
  , height = 105
  , thickness = 42
  , jw = 7 /* かみ合わせ幅 [5, 10]  */
  , jh = 2 /* 板厚[mm] [1, 5] */;
c.x = x;
c.y = y;
// 表の面を作る
var p = c.jrc(0, 0, width, height, jw, jh);
/*
// 上記は次のコードと同じ
//var p = c.drawJointRectangle(0, 0, width, height, jw, jh);
// または次のコードと同じ
var p = c.createPath();
p.moveTo(0, 0);
p.jointTo(width, 0, jw, jh);
p.jointTo(width, height, jw, jh);
p.jointTo(0, height, jw, jh);
p.jointTo(0, 0, jw, jh);
p.close();
*/
// 表面にいろいろ配置する
c.add(circle, { x: 130, y: 30 });
c.add(lcd, { x: 50, y: 30 });
c.add(motor, 20, 80);
c.add(button, { x: 140, y: 80 });
// 配線用の穴を開ける
c.drawRectangle(15, 59, 10, 6);
c.drawRectangle(135, 59, 10, 6);
// 側面と裏面を作る
var ps = p.extrude(45);
ps[0].x = width + 5;
ps[0].y = jh;
ps[1].x = width + 5;
ps[1].y = (thickness + 5) * 2 + jh;
ps[2].x = width + 5;
ps[2].y = thickness + 5 + jh;
ps[3].x = width + 5;
ps[3].y = (thickness + 5) * 3 + jh;
ps[4].x = 0; // 最後の要素が裏面
ps[4].y = height + 5; // 表の面の真下に配置する
for (var i = 0; i < ps.length; i ++) {
    c.add(ps[i]);
}
// 側面の一つに電源用の穴を開ける
ps[1].drawRectangle(jh + 5, thickness - jh - 5, height - jh*2 - 10, -10);
// 以降はゲームのソースコード
var idleInterval = false;
function startIdle() {
    idleInterval = setInterval(function() {
        if (button.value() > 0) {
            clearInterval(idleInterval);
            idleInterval = false;
            startGame();
        }
    }, 100);
}
var interval = false, score;
function stopGame() {
    if (interval) {
        clearInterval(interval);
        interval = false;
    }
    if (!idleInterval) {
        startIdle();
    }
}
function startGame() {
    for (i = 0; i < 2; i++) {
        lcd.setCursor(i, 0);
        lcd.write('                     ');
    }
    
    var level = 0, jump = 0, pause = 0;
    var obstacles = [];
    score = 0;
    
    interval = setInterval(function() {
        if (pause > 1) {
            pause -= 1;
            return;
        } else if (pause === 1) {
            motor.write(0);
            lcd.setColor(255, 255, 255);
            jump = 0;
            obstacles = [];
            pause -= 1;
            stopGame();
            return;
        }
        
        //circle.setSpinner(level);
        //level = (level + 1) % 24;
        
        if (jump === 0 && button.value() > 0)
            jump = 3;
        else if (jump !== 0)
            jump -= 1;
        console.log('jump: ' + obstacles);
        
        if (Math.random() < 0.1)
            obstacles.push(15);
        
        for (i = 0; i < 2; i++) {
            lcd.setCursor(i, 0);
            lcd.write('                     ');
        }
        
        if (jump > 0)
            lcd.setCursor(0, 0);
        else
            lcd.setCursor(1, 0);
        lcd.write('O');
        
        for (i = 0; i < obstacles.length; i++) {
            lcd.setCursor(1, obstacles[i]);
            lcd.write('X');
            
            obstacles[i] -= 1;
        }
        
        if (jump === 0 && obstacles[0] === -1) {
            lcd.setColor(255, 0, 0);
            motor.write(1);
            pause = 10;
        }
        while (obstacles[0] === -1) {
            obstacles.shift();
            score = (score + 1) % 24;
        }
        circle.setLevel(score);
    }, 100);
}
startIdle();
process.on('SIGINT', function() {
    if (interval) {
        clearInterval(interval);
    }
    
    circle.setLevel(0);
    circle = null;
    GCL.cleanUp();
    
    lcd.setColor(255, 255, 255);
    motor.write(0);
    
    process.exit(0);
});