import {
  ScoreUi,
  CurrentSongUi
} from './ui.js'

import { KEYS, GRACE_BEAT } from './config.js'
import { PressIndicator, Highway } from './highway.js';
import { Note } from './note.js';
import { ScoreManager } from './score.js'
import chartmgr from './chartmgr.js';

export class GameScreen {
  constructor(chart) {
    this.chart = chart;
    this.score = new ScoreManager();
    this.scoreUi = new ScoreUi();
    this.curBeat = 1;

    this.background = 10;
    this.started = false;

    this.pressIndicators = [];
    // populate press indicators
    KEYS.forEach((_, i) => {
      this.pressIndicators.push(new PressIndicator(i));
    })

    // load song
    this.notes = []
    if(this.chart) {
      this.chart.notes.forEach(n => {
        this.notes.push(new Note(n.lane, n.beat))
      })
    }
    this.song = loadSound("./charts/" + this.chart.id + "/" + this.chart.file, null, null, (percent) => {
      this.loadPercent = percent
      console.log(percent)
    });
    this.loadPercent = 0;
  }

  draw() {
    this.background = lerp(this.background, 50, 0.02)
    background(this.background)
    if(this.background > 40 && 
      !this.started && this.song.isLoaded() && this.chart.id != "test") {
      this.song.play();
      this.started = true;
    }

    Highway.draw(this.curBeat);
  
    // notes
    for(let i = 0; i < this.notes.length; i++) {
      this.notes[i].draw(this.curBeat)
    }
  
    // key indicators
    for(let i = 0; i < this.pressIndicators.length; i++) {
      let pi = this.pressIndicators[i];
      pi.draw(keyIsDown(KEYS[i]))
    };
  
    // draw score
    this.scoreUi.draw();

    if(!this.chart) {
      return debugMsg("no chart data!")
    } else if (!this.song) {
      return debugMsg("audio not loaded!")
    }
    CurrentSongUi.draw(chartmgr.covers.get(this.chart.id), this.chart.name, this.chart.artist);
  }

  calcBeat() {
    if(this.song) {
      let beatSecs = 60 / this.chart.bpm;
      this.curBeat = (this.song.currentTime() / beatSecs) + 1
    }
  }

  checkHit() {
    let pressTime = this.song.currentTime();
    let pressBeat = (pressTime / (60 / this.chart.bpm)) + 1
  
    // scan for notes
    if(this.notes.length < 1) return;
  
    let targetNotes = []
    targetNotes.push(this.notes[0])
    while(true) {
      if(this.notes.length < 2)
        break;

      if(!this.notes[targetNotes.length])
        break;
  
      if(
        this.notes[targetNotes.length].beat == targetNotes[targetNotes.length - 1].beat
        && this.notes[targetNotes.length].beat == targetNotes[targetNotes.length - 1].beat) {
        targetNotes.push(this.notes[targetNotes.length])
      } else {
        break;
      }
    }
  
    for(let i = 0; i < targetNotes.length; i++) {
      let k = targetNotes[i];
      if(targetNotes[0].beat + GRACE_BEAT > pressBeat 
        && targetNotes[0].beat - GRACE_BEAT < pressBeat
        && keyIsDown(KEYS[k.lane])){
        // console.log("HIT", k.lane)
  
        // hit note!
        this.notes.find(n=> n.symbol == k.symbol).pressed = true;
        this.notes.find(n=> n.symbol == k.symbol).remove();
        this.pressIndicators[k.lane].hit = true;
  
        this.score.onHit();
      }
    }
  }
}