
#include "config.h"
#include "sequencer.h"
#include <LCDMenuLib2.h>
#include <LiquidCrystal_I2C.h>

extern LCDMenuLib2 LCDML;
extern LiquidCrystal_I2C lcd;
extern config_t configuration;
extern uint8_t drum_midi_channel;
extern void handleNoteOn(byte , byte , byte );
extern void handleNoteOff(byte , byte , byte );
extern void UI_func_sequencer(uint8_t);
extern void UI_func_arpeggio(uint8_t);
extern const char* seq_find_shortname(uint8_t);
boolean interrupt_swapper = false;

void sequencer_part1(void)
{
  //if (seq_note_in > 0 && seq_note_in < 62 && seq_recording == false ) {
  //handleNoteOff(configuration.dexed[0].midi_channel, seq_data[3][seq_step] + seq_transpose , 0);
  //handleNoteOff(configuration.dexed[0].midi_channel, seq_data[3][seq_step - 1] + seq_transpose , 0);
  //if (seq_note_in>65)seq_note_in=seq_note_in-12;
  //seq_transpose = seq_note_in % 12 ;
  //seq_transpose=seq_transpose-12;
  //seq_note_in = 0;
  //}

  //record to sequencer if sequencer menu is active and recording is active
  if (seq_note_in > 0 && seq_recording == true  && LCDML.FUNC_getID() == LCDML.OTHER_getIDFromFunction(UI_func_sequencer)) {
    seq_data[seq_active_track][seq_step] = seq_note_in;
    seq_vel[seq_active_track][seq_step] = seq_note_in_velocity;
    seq_note_in = 0;
    seq_note_in_velocity = 0;
  }

  for (uint8_t d = 0; d < 4; d++)
  {
    if ( seq_track_type[d] == 0) { // drum track
      if (seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step] > 0 )
      {
        handleNoteOn(drum_midi_channel, seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step] , seq_vel[ seq_patternchain[seq_chain_active_step][d] ][seq_step]);
      }
    }
    else {
      if (seq_data[seq_patternchain[seq_chain_active_step][d]][seq_step] > 0 )  // instrument track
      {
        if (seq_track_type[d] == 1 || (seq_track_type[d] == 3 && arp_play_basenote) )
        {
          handleNoteOn(configuration.dexed[seq_inst_dexed[d]].midi_channel, seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step], seq_vel[ seq_patternchain[seq_chain_active_step][d] ][seq_step]);
          seq_prev_note[d] = seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step];
          seq_prev_vel[d] = seq_vel[ seq_patternchain[seq_chain_active_step][d] ][seq_step];
        }
        else if (seq_track_type[d] == 2  ) //Chords
        {
          if (seq_vel[ seq_patternchain[seq_chain_active_step][d]][seq_step] > 199)
          {

            //handleNoteOn(configuration.dexed[seq_inst_dexed[d]].midi_channel, seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step], seq_chord_velocity);  // basenote

            for (uint8_t x = seq_element_shift; x < seq_element_shift + seq_chord_key_ammount; x++) //play chord notes
            {
              handleNoteOn(configuration.dexed[seq_chord_dexed_inst].midi_channel, seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step] + (seq_oct_shift * 12) + seq_arps[seq_vel[ seq_patternchain[seq_chain_active_step][d] ][seq_step] - 200][x], seq_chord_velocity);
            }
            seq_prev_note[d] = seq_data[seq_patternchain[seq_chain_active_step][d]][seq_step] + (seq_oct_shift * 12);
            seq_prev_vel[d] = seq_vel[seq_patternchain[seq_chain_active_step][d]][seq_step];
          }
        }
        if (seq_track_type[d] == 3) {  //Arp
          arp_step = 0;
          arp_counter = 0;

          arp_note = seq_data[ seq_patternchain[seq_chain_active_step][d] ][seq_step] + (seq_oct_shift * 12);
          // if (seq_vel[seq_patternchain[seq_chain_active_step][d] ][seq_step] - 200 >= 0)
          arp_chord = seq_vel[seq_patternchain[seq_chain_active_step][d] ][seq_step] - 200;
        }
      }

      // after here not triggered by a key input -  arp only
      if (seq_track_type[d] == 3)
      { //Arp
        if (arp_speed == 0 || (arp_speed == 1 && arp_counter == 0) ) {

        { if (arp_style == 0) { //arp up
              handleNoteOn(configuration.dexed[seq_chord_dexed_inst].midi_channel, arp_note + seq_arps[arp_chord][arp_step + seq_element_shift], seq_chord_velocity);
              arp_note_prev = arp_note + seq_arps[arp_chord][arp_step + seq_element_shift] ;
            }
            else if (arp_style == 1) { //arp down
              handleNoteOn(configuration.dexed[seq_chord_dexed_inst].midi_channel, arp_note + seq_arps[arp_chord][arp_lenght - arp_step + seq_element_shift], seq_chord_velocity);
              arp_note_prev = arp_note + seq_arps[arp_chord][arp_lenght - arp_step + seq_element_shift] ;
            }
            else if (arp_style == 2) { //arp up & down
              if (arp_step <= arp_lenght) {
                handleNoteOn(configuration.dexed[seq_chord_dexed_inst].midi_channel, arp_note + seq_arps[arp_chord][arp_step ], seq_chord_velocity);
                arp_note_prev = arp_note + seq_arps[arp_chord][arp_step ] ;
              }
              else {
                handleNoteOn(configuration.dexed[seq_chord_dexed_inst].midi_channel, arp_note + seq_arps[arp_chord][arp_lenght * 2 - arp_step ], seq_chord_velocity);
                arp_note_prev = arp_note + seq_arps[arp_chord][arp_lenght * 2 - arp_step ] ;
              }
            }
            else if (arp_style == 3) { //arp random
              uint8_t rnd1 = random(arp_lenght);
              handleNoteOn(configuration.dexed[seq_chord_dexed_inst].midi_channel, arp_note + seq_arps[arp_chord][rnd1 + seq_element_shift] + (seq_oct_shift * 12), seq_chord_velocity);
              arp_note_prev = arp_note + seq_arps[arp_chord][rnd1 + seq_element_shift] + (seq_oct_shift * 12);
            }
          }
        }
      }
    }
    seq_noteoffsent[d] = false;
  }
  arp_counter++;
  seq_step++;
  if (arp_speed == 0)  // Arp Speed 1/16
  {
    arp_step++;
  }
  else
  {
    // Arp Speed 1/8
    if (arp_counter > 1)  {
      arp_counter = 0;
      arp_step++;
    }
  }

  if (arp_style != 2) {

    if ( (arp_step > 1 && seq_arps[arp_chord][arp_step] == 0) || arp_step == arp_lenght)
    {
      arp_step = 0;
    }
  }
  if (arp_style == 1 || arp_style == 2  )
  {
    if (arp_lenght == 0)arp_lenght = 9;

  }
  if ( arp_style == 2  )  //only for up&down
  {
    if ( (arp_step > 1 && seq_arps[arp_chord][arp_step] == 0) || arp_step == arp_lenght * 2)
    {
      arp_step = 0;
    }

  }
  if (seq_step > 15)
  {
    seq_step = 0;
    if (seq_chain_lenght > 0) {
      seq_chain_active_step++;
      if (seq_chain_active_step > seq_chain_lenght)
      {
        seq_chain_active_step = 0;
      }
    }
  }
}

void sequencer_part2(void)
{
  for (uint8_t d = 0; d < 4; d++)
  {
    if (seq_noteoffsent[d] == false) {
      if ( seq_prev_note[d] > 0 && seq_track_type[d] > 0)
      {
        handleNoteOff(configuration.dexed[seq_inst_dexed[d]].midi_channel, seq_prev_note[d] , 0);
        if (seq_track_type[d] == 2) {  //Chords
          if ( seq_prev_vel[d] > 199)  {
            for (uint8_t x = seq_element_shift; x < seq_element_shift + seq_chord_key_ammount; x++) //play chord notes
            {
              handleNoteOff(configuration.dexed[seq_chord_dexed_inst].midi_channel, seq_prev_note[d] + seq_arps[seq_prev_vel[d] - 200][x], 0);
            }
          }
        }
        else if (seq_track_type[d] == 3)
        { //Arp
          handleNoteOff(configuration.dexed[seq_chord_dexed_inst].midi_channel, arp_note_prev, 0);
        }
      }
      seq_noteoffsent[d] = true;
    }
  }
}

void sequencer(void)
{ // Runs in Interrupt Timer. Switches between the Noteon and Noteoff Task, each cycle

  interrupt_swapper = !interrupt_swapper;

  if (interrupt_swapper) sequencer_part1();
  else sequencer_part2();
}
