InstrumentListComponent::InstrumentListComponent (Sequencer* sequencer_) :
Component ("InstrumentListComponent"),
sequencer (sequencer_),
lastPattern (nullptr),
lastActiveInstrument (nullptr)
{
    Pattern* pattern = sequencer->getPattern();
    lastPattern = pattern;
    int numInstruments = pattern->getNumInstruments();    
    for (int i = 0; i < numInstruments; i++) {
        Instrument* instrument = pattern->getInstrumentAt (i);
        InstrumentComponent* instrumentComponent = new InstrumentComponent (instrument);
        addAndMakeVisible (instrumentComponent);
        instrumentComponents.add (instrumentComponent);
    }   
    
    for (int i = 0; i < numInstruments; i++) {
        ToggleButton* btn = new ToggleButton ((String)i);
        btn->setRadioGroupId (kRadioGroupId);
        btn->setColour (ToggleButton::textColourId, Colours::white);
        addAndMakeVisible (btn);
        btn->addListener (this);
        activeButtons.add (btn);
    }   
    
    startTimer (100);
}
// Timer methods
void InstrumentListComponent::timerCallback()
{
    Pattern* pattern = sequencer->getPattern();
    if (sequencer->getPattern() != lastPattern) {
        lastPattern = pattern;
        for (int i = 0; i < pattern->getNumInstruments(); i++) {
            Instrument* instrument = pattern->getInstrumentAt (i);
            instrumentComponents[i]->setInstrument (instrument);
        }           
    }
    if (pattern->getActiveInstrument() != lastActiveInstrument) {
        lastActiveInstrument = pattern->getActiveInstrument();
        int i = lastActiveInstrument->getIndex();
        activeButtons[i]->setToggleState (true, false);
    }   
}
void StateWriter::getState (MemoryBlock& destData)
{
    DBG("StateWriter::getState(): Saving state.")
    
    ValueTree settingsTree ("settings");
    
    ValueTree libTree ("library");
    libTree.setProperty ("threshold", library->getThreshold(), nullptr);
    libTree.setProperty ("release", library->getReleaseTicks(), nullptr);
    libTree.setProperty ("velocity", library->getVelocityScale(), nullptr);
    settingsTree.addChild (libTree, -1, nullptr);

    ValueTree patternsTree ("patterns");
    for (int i = 0; i < library->getNumPatterns(); i++) {
        Pattern* pattern = library->getPattern (i);
        ValueTree patternTree ("pattern");
        ValueTree instrumentsTree ("instruments");
        for (int j = 0; j < pattern->getNumInstruments(); j++) {
            Instrument* instrument = pattern->getInstrumentAt (j);
            ValueTree instrumentTree ("instrument");
            instrumentTree.setProperty ("index", instrument->getIndex(), nullptr);
            instrumentTree.setProperty ("name", instrument->getName(), nullptr);
            instrumentTree.setProperty ("noteNumber", instrument->getNoteNumber(), nullptr);            
            instrumentsTree.addChild (instrumentTree, -1, nullptr);
        }
        patternTree.addChild (instrumentsTree, -1, nullptr);
        patternsTree.addChild (patternTree, -1, nullptr);
    }
    settingsTree.addChild (patternsTree, -1, nullptr);
    
    ValueTree seqTree ("sequencer");
    seqTree.setProperty ("sequencerNum", sequencer->getSequencerNum(), nullptr);
    settingsTree.addChild (seqTree, -1, nullptr);
        
    MemoryOutputStream stream (destData, false);
    settingsTree.writeToStream (stream);
}