// 'battery', 14x7px
const unsigned char battery [] PROGMEM = {
  0xff, 0xf0, 0x80, 0x10, 0x80, 0x1c, 0x80, 0x1c, 0x80, 0x1c, 0x80, 0x10, 0xff, 0xf0
};

// 'antenna', 7x7px
const unsigned char antenna [] PROGMEM = {
  0xfe, 0x92, 0x54, 0x38, 0x10, 0x10, 0x10
};

void menu() {
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);

  display.drawBitmap(0, 0, battery, 14, 7, WHITE);
  display.setCursor(20, 0);
  display.print(bat_lvl);
  display.print(" %");

  if(sig <= -100) {
    display.drawBitmap(67, 0, antenna, 7, 7, WHITE);
    display.setCursor(80, 0);
    display.print(sig);
  }
  else {
    display.drawBitmap(73, 0, antenna, 7, 7, WHITE);
    display.setCursor(86, 0);
    display.print(sig);
  }
  display.print(" dBm");

  switch(menu_pos) {
    case 1:
      showFire();
      break;
    case 2:
      showTimer();
      break;
    case 3:
      showSettings();
      break;
    case 4:
      showCommands();
      break;
  }

  display.display();
}

void exitMenu() {
  if((exit_menu_code == 255) || (exit_menu_code == 12)) {
    resExitMenu();
  }
  
  // Fire -----------------------------------------------------------------------------------------
  else if(exit_menu_code == 11) {
    exit_menu_code = 12;
    if(fire_mode == 3) {
      ID_number = 0;
      resExitMenu();
    }
    else if(ID_number == 0) {
      ID_number = 1;
    }
    new_position = ID_number;
  }

  // Timer ----------------------------------------------------------------------------------------
  else if(exit_menu_code == 21) {
    exit_menu_code = 22;
    new_position = time_m;
  }
  else if(exit_menu_code == 22) {
    exit_menu_code = 23;
    new_position = time_s;
  }
  else if(exit_menu_code == 23) {
    resExitMenu();
    setTimer();
  }

  // Settings -------------------------------------------------------------------------------------
  else if(exit_menu_code == 30) {
    // Ignation Delay -----------------------------------------------------------------------------
    if(new_position == 0) {
      exit_menu_code = 31;
      new_position = set_ID_number;
      setIgnationDelay();
    }
    // LoRa Power ---------------------------------------------------------------------------------
    else if(new_position == -1) {
      exit_menu_code = 35;
      new_position = LoRa_power;
    }
    else if(new_position == -2) {
      exit_menu_code = 37;
      new_position = set_ID_number;
      setUnitLoRa();
    }
    // Alias --------------------------------------------------------------------------------------
    else if(new_position == -3) {
      exit_menu_code = 130;
      new_position = alias;
    }
  }
  else if(exit_menu_code == 31) {
    exit_menu_code = 32;
    new_position = set_time_m;
  }
  else if(exit_menu_code == 32) {
    exit_menu_code = 33;
    new_position = set_time_s;
  }
  else if(exit_menu_code == 33) {
    exit_menu_code = 34;
    new_position = set_time_ms;
  }
  else if(exit_menu_code == 34) {
    exit_menu_code = 30;
    new_position = 0;
    ignation_delay = set_time_m * 60;
    ignation_delay = ignation_delay + set_time_s;
    ignation_delay = ignation_delay * 1000;
    ignation_delay = ignation_delay + (set_time_ms * 100);
    sendDelay();
  }
  else if(exit_menu_code == 35) {
    exit_menu_code = 36;
    EEPROM.write(0, LoRa_power);
    LoRa.setTxPower(LoRa_power);
    new_position = LoRa_SF;
  }
  else if(exit_menu_code == 36) {
    exit_menu_code = 30;
    EEPROM.write(1, LoRa_SF);
    LoRa.setSpreadingFactor(LoRa_SF);
    new_position = -1;
  }
  else if(exit_menu_code == 37) {
    exit_menu_code = 38;
    new_position = LoRa_unit_power;
  }
  else if(exit_menu_code == 38) {
    exit_menu_code = 39;
    new_position = LoRa_unit_SF;
  }
  else if(exit_menu_code == 39) {
    exit_menu_code = 30;
    sendUnitSetting();
    new_position = -2;
  }
  else if(exit_menu_code == 130) {
    exit_menu_code = 131;
    new_position = set_ID_number;
  }
  else if(exit_menu_code == 131) {
    exit_menu_code = 30;
    sendAlias();
    new_position = -3;
  }

  else if(exit_menu_code == 41) {
    if(command_mode == 0) {
      exit_menu_code = 255;
    }
    else {
      exit_menu_code = 42;
      new_position = set_ID_number;
    }
  }
  else if(exit_menu_code == 42) {
    sendCommand();
    exit_menu_code = 255;
  }


  else if(exit_menu_code == 0) {
    switch(menu_pos) {

      // Firing
      case 1:
        exit_menu_code = 11;
        new_position = fire_mode;
        break;

      // Countdown
      case 2:
        exit_menu_code = 21;
        new_position = time_h;
        break;

      // Settings
      case 3:
        exit_menu_code = 30;
        new_position = 0;
        break;

      // Commands
      case 4:
        exit_menu_code = 41;
        new_position = command_mode;
        break;
    }
  }

  old_position = new_position;
}


void resExitMenu() {
  exit_menu_code = 0;
  new_position = menu_pos;
}
