import processing.serial.*;
import java.util.ArrayList;

Serial port; // シリアルポート
int[] dataBuffer = new int[100]; // データを格納するバッファ
int dataIndex = 0; // バッファのインデックス
boolean recording = false; // データ収録のフラグ
int recordDuration = 20; // 収録する時間(秒)
int recordStartTime = millis();
int countBelow40 = 0; // 40cm未満の回数
int countAbove40 = 0; // 40cm以上の回数

ArrayList fireworks = new ArrayList();

void setup() {
  size(800, 700);
  String portName = "COM3";  // Arduinoのポート名を指定
  port = new Serial(this, portName, 9600);
  port.bufferUntil('\n'); // 改行文字までデータを受信
  PFont font = createFont("Meiryo", 50);
  textFont(font);
}

void serialEvent(Serial port) {
  String data = port.readStringUntil('\n');
  if (data != null) {
    data = trim(data);
    if (recording) {
      // データをバッファに格納
      dataBuffer[dataIndex] = int(data);
      dataIndex++;
      println(data);
      // センサデータを分類してカウント
      if (int(data) < 40) {
        countBelow40++;
      } else {
        countAbove40++;
      }
      println(countBelow40);
      println(countAbove40);
    }
  }
}

void draw() {
  // データ収録が開始されている場合
  if (recording) {
    if (millis() < recordStartTime + (recordDuration * 1000)) {
      // データを収録
      // データを表示するためのコードを追加
    } else {
      // 収録時間が終了したら収録を停止
      recording = false;
      println("Recording stopped.");
      port.write('S'); // アルディーノに停止メッセージを送信
    }
  }

  background(72, 209, 204); // 緑色の背景

  for (int i = fireworks.size() - 1; i >= 0; i--) {
    Firework firework = fireworks.get(i);
    firework.update();
    firework.display();
    if (firework.isFinished()) {
      fireworks.remove(i);
    }
  }

  if (random(1) < 0.03) {  // ランダムな確率で花火を打ち上げる
    fireworks.add(new Firework(random(width), height));
  }

  // 距離が閾値未満の場合は正しい姿勢と判定
  if (countAbove40 > countBelow40) {
    fill(255, 105, 180);  // ピンク色で表示
    textSize(50);
    text("正しい画面からの距離です。", 80, 350);
    float x = random(width);    // X座標をランダムに決定
    float y = random(height);   // Y座標をランダムに決定
    float diameter = random(20, 100); // 花丸の直径をランダムに決定

    noStroke();           // 輪郭線なし
    fill(random(255), random(255), random(255), 150); // ランダムな色(透明度付き)を設定
    ellipse(x, y, diameter, diameter);  // 花丸を描画
  } else {
    background(70, 131, 132); // 暗い緑色の背景
    fill(178, 34, 34);  // 赤色で表示
    textSize(50);
    text("画面からの距離が近いです。", 80, 350);
  }
}

class Firework {
  float x, y;
  float vy;
  color c;
  float flowerSize;
  float petals;
  float angleInc;

  Firework(float x, float y) {
    this.x = x;
    this.y = y;
    this.vy = random(-10, -20); // 上方向にランダムな速度で打ち上げ
    this.c = color(random(100, 255), random(100, 255), random(100, 255)); // パステルカラー
    this.flowerSize = random(20, 40); // 花火の大きさ
    this.petals = int(random(8, 12)); // 花の花びらの数
    this.angleInc = TWO_PI / petals;
  }

  void update() {
    y += vy;
    vy += 0.3; // 重力
  }

  void display() {
    noStroke();
    fill(c);
    for (float angle = 0; angle < TWO_PI; angle += angleInc) {
      float petalX = x + cos(angle) * flowerSize;
      float petalY = y + sin(angle) * flowerSize;
      ellipse(petalX, petalY, 10, 10); // 花びらを描画
    }
  }

  boolean isFinished() {
    return vy >= 0;
  }
}

void keyPressed() {
  if (key == 'R' || key == 'r') {
    // レコーディングを開始
    recording = true;
    recordStartTime = millis();
    dataIndex = 0;
    println("Recording started.");
    port.write('R'); // アルディーノにレコード開始メッセージを送信
  }
}