class HighscoreManager {
  constructor() {
    this._deserialize();
  }

  _deserialize() {
    let raw = localStorage.getItem('rhythm.hst');
    if(!raw) {
      this._highscores = {};
      this._serialize();
    } else {
      this._highscores = JSON.parse(atob(raw));
    }
  }

  _serialize() {
    localStorage.setItem('rhythm.hst', btoa(JSON.stringify(this._highscores)));
  }

  getHighscore(songId) {
    return this._highscores[songId]
  }

  setHighscore(songId, name, score) {
    let existing = this.getHighscore(songId);
    if(existing != undefined && existing.score <= score)
      return;

    this._highscores[songId] = {
      name,
      score
    }
    this._serialize();
  }
}

export default new HighscoreManager();