// ----
// ---- file   : Montage_ElementZoneForm.tks
// ---- author : Bastian Spiegel <bs@tkscript.de>
// ---- legal  : (c) 2022 by Bastian Spiegel.
// ----          Distributed under terms of the GNU LESSER GENERAL PUBLIC LICENSE (LGPL). See
// ----          http://www.gnu.org/licenses/licenses.html#LGPL or COPYING for further information.
// ----
// ---- info   : This is part of the "syntracker" midi sequencer.
// ----
// ---- created: 27Oct2022
// ---- changed:
// ----
// ----
// ----

module MMontage_ElementZoneForm;

use namespace ui;
use namespace st2;


// <class.png>
class Montage_ElementZoneForm extends Montage_ParamForm {
   // MontagePartNormalElementOscAmpPitch

   int element_idx;

   protected FloatParam *fp_note_limit_low;
   protected FloatParam *fp_note_limit_high;

   protected FloatParam *fp_levelscl_bp_1;
   protected FloatParam *fp_levelscl_bp_2;
   protected FloatParam *fp_levelscl_bp_3;
   protected FloatParam *fp_levelscl_bp_4;

   protected StringArray note_options;


   // <method.png>
   static StringArray octave_notes = ["C x", "C#x", "D x", "D#x", "E x", "F x", "F#x", "G x", "G#x", "A x", "A#x", "B x"];
   protected method genNoteOptions() {
      if(note_options.isEmpty())
      {
         int noteIdx = 0;
         loop(128)
         {
            note_options.add(octave_notes[noteIdx%12].replace("x", String( (noteIdx/12) -2)));
            noteIdx++;
         }
      }
   }

   // <init.png>
   public method initElementZoneForm(EditMIDISynthFormMontage _parentSynthForm, int _elementIdx) : boolean {

      initParamForm(_parentSynthForm);

      element_idx = _elementIdx;

      local String buf;
      MIDISynthProfile msp <= parent_synth_form.profile;

      String groupCaption <= "Element Zone";

      createWidgetsFromMSPGroup(buf, msp, groupCaption);

      local String bufXFM;
      bufXFM.load("Montage_ElementZoneForm.xfm", true/*bRemoveCR*/);
      bufXFM.replace("$(WIDGETS)", buf);

      if(!parseXMLForm(bufXFM))
      {
         trace "[---] failed to parse \"Montage_ElementZoneForm.xfm\"";
         return false;
      }

      if(!autoResolveIds(this))
      {
         return false;
      }

      if(!resolveWidgetsFromMSPGroup(msp, groupCaption, param_widgets))
      {
         trace "[---] Montage_ElementZoneForm: resolveWidgetsFromMSPGroup("+groupCaption+") failed.";
         return false;
      }

      genNoteOptions();
      fp_note_limit_low.setUserTable(note_options);
      fp_note_limit_low.setDisplayType(FloatParam.DISPLAY_USER);
      fp_note_limit_high.setUserTable(note_options);
      fp_note_limit_high.setDisplayType(FloatParam.DISPLAY_USER);

      fp_levelscl_bp_1.setUserTable(note_options);
      fp_levelscl_bp_1.setDisplayType(FloatParam.DISPLAY_USER);

      fp_levelscl_bp_2.setUserTable(note_options);
      fp_levelscl_bp_2.setDisplayType(FloatParam.DISPLAY_USER);

      fp_levelscl_bp_3.setUserTable(note_options);
      fp_levelscl_bp_3.setDisplayType(FloatParam.DISPLAY_USER);

      fp_levelscl_bp_4.setUserTable(note_options);
      fp_levelscl_bp_4.setDisplayType(FloatParam.DISPLAY_USER);

      recursiveBuildTabCycleLists();

      return true;
   }

   // <method.png>
   public method enter() {
   }

   // <method.png>
   public method leave() {
   }

   // <ui_kbd.png>
   public virtual addTabCycleOverrides(PointerArray layers) {
      layers.joinRR(layers, param_widgets);
   }

   // <method_get.png>
   public method getElementOsc() : MontagePartNormalElementOscAmpPitch {
      MontagePartNormal pNormal <= parent_synth_form.getEditPartNormal();
      if(null != pNormal)
      {
         MontagePartNormalElement el <= pNormal.getElementByIdx(element_idx);
         if(null != el)
         {
            return el.osc_amp_pitch;
         }
      }
      return null;
   }

   // <ui_show.png>
   public method showElementZone() {
      MontagePartNormalElementOscAmpPitch c <= getElementOsc();
      updateParamWidgets(c);
   }

   // <ui_consume.png>
   public virtual consumeAction(Action _action) : boolean {
      Layer ap <= _action.getActionProvider();
      String acName <= _action.getActionName();

      int paramIdx = param_widgets.indexOfPointer(ap, 0);
      if(paramIdx >= 0)
      {
         handleParamChanged(getElementOsc(), paramIdx, ap);

         return true;
      }

      switch(@(ap))
      {
         default:
            break;
      }
   }

}
