#pragma once
#ifndef SIMPLEINSTRUMENTSTATE_H
#define SIMPLEINSTRUMENTSTATE_H

#include "Jukebox.h"
#include "OmRack.h"

//#include <set>
//#include <map>
//#include <vector>


typedef TJBox_UInt32 u32;
typedef TJBox_Int32 s32;
typedef TJBox_Float32 f32;



#define HUESTEPS 12
#define BRIGHTSTEPS 10

#define FIRST_LAMP M_036_PROPERTY_TAG
#define FIRST_LAMP_PITCH 36
#define LAST_LAMP M_107_PROPERTY_TAG
#define FIRST_REAR_LAMP B_036_PROPERTY_TAG

#define FIRST_FOLDED_LAMP F_0_PROPERTY_TAG
#define LAST_FOLDED_LAMP F_6_PROPERTY_TAG
#define FIRST_FOLDED_REAR_LAMP G_0_PROPERTY_TAG

#include "List.h"
#include "Map.h"
#include "Lamp.h"




//class LampCollection;
//class Lamp;

class CRackExtension
{
public:
    // Init the rack
    CRackExtension();
    // Render 64 frames
    void RenderBatch(const TJBox_PropertyDiff iPropertyDiffs[], TJBox_UInt32 iDiffCount);


private:
    Om *om;
    int init; // set to zero at beginning
    
//    this->lamps = new LampCollection(LAST_LAMP - FIRST_LAMP + 1, this->om, FIRST_LAMP, FIRST_REAR_LAMP);
//    this->foldedLamps = new LampCollection(LAST_FOLDED_LAMP - FIRST_FOLDED_LAMP + 1, this->om, FIRST_FOLDED_LAMP, FIRST_FOLDED_REAR_LAMP);

    LampCollection<LAST_LAMP - FIRST_LAMP + 1> lamps;
    LampCollection<LAST_FOLDED_LAMP - FIRST_FOLDED_LAMP + 1> foldedLamps;
    
    u32 guitarRows[6];
    int lastMode = -1;
    int wasArecibo = 0;
    u32 areciboX = 0;
    u32 areciboY = 0;

    bool audInConnected;
    bool audOutConnected;
#define AUDIO_HISTORY_BATCHES 128
#define AUDIO_HISTORY_SAMPLES (kBatchSize * AUDIO_HISTORY_BATCHES)
#define FFT_MAX_SIZE 32768
    
    u32 audInPointer = 0; // is left pointing to current buffer after each read.
    TJBox_AudioSample *audIn = 0;
    TJBox_AudioSample *fftMem = 0;
    bool gotAudIn = 0;
    
    bool pattern010WasNoteOn = 0;
    u32 pattern010WasPitch = 0;
    
    int pattern1BatchCounter = 1;
    int rowCaught[6] = {0};
    
    int batchTicker = 0;
    
    u32 bigLampLastNoteOn = 0;
    
    int wasAnyCvConnected = 0;
    f32 lastGateCv = 0;
    f32 lastPitchCv = 0;
    
    u32 audioRenderTicks = 32; // cpu miser cranks it higher

    u32 mainHue = 0;

    // methods

    void modeChanged(int newMode);
    
    void doPattern0();
    void doArecibo();
    void doPattern1();
    void doPattern010BigLamp();
    void doPattern3();
    void doAud0();
    void doAud1();
    void doAud2();
    void doAud3();

    void doFoldedLampNotes();

    void maybeGetAud();
    void mode0Hues();
};

#endif
