#ifndef cwPresetSel_h
#define cwPresetSel_h


namespace cw
{
  namespace preset_sel
  {
    typedef handle< struct preset_sel_str > handle_t;

    typedef struct preset_str
    {
      bool     playFl;      // play this preset
      bool     seqFl;       // play this preset during sequencing.
      unsigned preset_idx;  // preset index into preset_labelA[].
      unsigned order;       //
    } preset_t;

    typedef struct frag_str
    {
      unsigned         fragId;      // Unique fragment id 
      unsigned         guiUuId;     // GUI UUId associated with this fragment
      unsigned         endLoc;      // The endLoc is included in this fragment. The begin loc is f->prev->endLoc+1
      time::spec_t     endTimestamp;
      
      double           igain;
      double           ogain;
      double           wetDryGain;
      double           fadeOutMs;
      unsigned         begPlayLoc;
      unsigned         endPlayLoc;
      char*            note;

      preset_t*        presetA;
      unsigned         presetN;

      bool             uiSelectFl;
      bool             seqAllFl; // Set if all preset.seqFl's should be treated as though they are set to true.

      struct frag_str* link;
      struct frag_str* prev;
    } frag_t;


    enum {
      kGuiUuIdVarId,
      kBegLocVarId,
      kEndLocVarId,
      kInGainVarId,
      kOutGainVarId,
      kFadeOutMsVarId,
      kWetGainVarId,
      kBegPlayLocVarId,
      kEndPlayLocVarId,
      kPlayBtnVarId,
      kPlaySeqBtnVarId,
      kPlaySeqAllBtnVarId,
      kNoteVarId,
      
      kPresetOrderVarId,     //  preset order value
      kPresetSelectVarId,    //  select a preset to play
      kPresetSeqSelectVarId, //  sequence preset selections to play
      kPlayEnableVarId,      //  include in the segment to play
      kDryFlVarId,           //  play this fragment dry


      kBaseMasterVarId,       // All 'master' variables have id's greater than kBaseMasterVarId
      kMasterWetInGainVarId,
      kMasterWetOutGainVarId,
      kMasterDryGainVarId,
      kMasterSyncDelayMsVarId
    };
    
    rc_t create(  handle_t& hRef, const object_t* cfg  );
    rc_t destroy( handle_t& hRef );

    unsigned    preset_count( handle_t h );
    const char* preset_label( handle_t h, unsigned preset_idx );
    
    unsigned      fragment_count(    handle_t h );
    const frag_t* get_fragment_base( handle_t h );
    const frag_t* get_fragment(      handle_t h, unsigned fragId );
    const frag_t* gui_id_to_fragment(handle_t h, unsigned guiUuId );

    unsigned frag_to_gui_id( handle_t h, unsigned fragId, bool showErrorFl=true );
    unsigned gui_to_frag_id( handle_t h, unsigned guiUuId, bool showErrorFl=true );
    
    rc_t create_fragment( handle_t h, unsigned end_loc, time::spec_t endTimestamp, unsigned& fragIdRef );
    rc_t delete_fragment( handle_t h, unsigned fragId );

    bool is_fragment_loc( handle_t h, unsigned loc );

    // Return the fragment id of the 'selected' fragment.
    unsigned ui_select_fragment_id( handle_t h );

    // Set the 'select_flag' on this fragment and remove it from all others.
    void     ui_select_fragment(    handle_t h, unsigned fragId, bool selectFl );
    

    rc_t set_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, bool     value );
    rc_t set_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, unsigned value );
    rc_t set_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, double   value );
    rc_t set_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, const char*   value );

    rc_t get_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, bool&        valueRef );
    rc_t get_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, unsigned&    valueRef );
    rc_t get_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, double&      valueRef );
    rc_t get_value( handle_t h, unsigned fragId, unsigned varId, unsigned presetId, const char*& valueRef );

    // Call this function to determine which fragment the timestamp 'ts' is contained by.
    // This function is optimized to be called in time sensitive functions where 'ts' is expected to be increasing.
    // If 'ts' is past the last defined fragment then the last fragment is returned.
    // If no fragments are defined 'frag_Ref' is set to nullptr.
    // The return value is true when the value of frag_Ref changes from the previous call.
    void track_timestamp_reset( handle_t h );
    bool track_timestamp( handle_t h, const time::spec_t& ts, const cw::preset_sel::frag_t*& frag_Ref );

    // Same as track_timestamp_???() but tracks the score 'loc' instead of timestamp.
    void track_loc_reset( handle_t h );
    bool track_loc( handle_t h, unsigned loc, const cw::preset_sel::frag_t*& frag_Ref );

    // Return the preset index marked to play on this fragment.
    unsigned fragment_play_preset_index( const frag_t* frag, unsigned preset_seq_idx=kInvalidIdx );

    // Return the count of presets whose 'seqFl' is set.
    unsigned fragment_seq_count( handle_t h, unsigned fragId );
    
    rc_t write( handle_t h, const char* fn );
    rc_t read(  handle_t h, const char* fn );
    rc_t report( handle_t h );
    
  }
}


#endif