#pragma once

#include <render/sprite/atlas.h>
#include <render/sprite/image.h>
#include <game/defs.hpp>
#include <soloud_wavstream.h>
#include "defs.hpp"
#include "noteRecorder.h"

namespace editor {
class NotesTemplate
{
public:
    std::string name;
    std::list<game::noteInfo*> notes;

    ~NotesTemplate();
};

class LevelEditor
{
private:
    Editor *root;
    NoteRecorder *recorder;
    std::list<game::noteInfo*> notes, notesOnScreen;
    std::list<NotesTemplate*> templates;
    const game::levelInfo *info;

    float songPosition;
    float songLength;
    bool active, musicPlaying;

    bool showNewTemplate, showTemplateList;
    float templateTiming, templateDuration;
    char templateName[256];

    render::sprite::image *background;
    render::sprite::icon buttonIcons[2][4], placeholderIcons[2][4];
    render::sprite::icon longIcon;

    SoLoud::WavStream *music;
    SoLoud::handle playingMusic;

    std::list<game::noteInfo*>::iterator activeNote;
public:
    LevelEditor(const game::levelInfo &level, Editor *root);
    ~LevelEditor();

    bool ShouldClose();

    void Update();
    void Render();
private:

    void UpdateGui();
    void UpdateNoteEditor();
    void UpdateNoteSelector();
    void UpdateMaterTime();
    void UpdateSubtitle();
    void UpdateTimeline();
    void UpdateTemplates();

    void Save();
    void Load();

    void LoadNotes();

    void CopyNotes();
    void InsertDefaultNote(float time);

    void PrepareOnScreenNotes();
    void RenderTimeline();
    void RenderGrid();

    void StartRealTimeRecord(float time);
    void StopRealTimeRecord();

    void StartMusic(float time);
    void SeekMusic(float time);
    void StopMusic();

    void LoadTemplates();
    void SaveTemplates();
    void CreateTemplate();

    void InsertTemplate(const NotesTemplate *);

    void ShowNewTemplate();

    void SortNotes();
};


}
