/*
Adafruit Arduino - Lesson 3. RGB LED
*/

int redPin = 2;
int greenPin = 3;
int bluePin = 4;
int sensorValue;

int spreuken_index;
int spreuken_sub_index;

//spreuk index switch knop
const int button_A_Pin = A3;  // the number of the pushbutton pin
int button_A_State = 0;  // variable for reading the pushbutton status

//cast spell button
const int button_B_Pin = A1;
int button_B_State = 0;

//9mm met lezaah
int laser_pin = 11;
//uncomment this line if using a Common Anode LED
//#define COMMON_ANODE

void setup()
{
  Serial.begin(9600);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(button_A_Pin, INPUT);
  pinMode(laser_pin, OUTPUT);
}

void loop()
{
  Meter();
  set_spreuk_index();
  set_spreuk_sub_index();
  pre_cast_spell();
  //test_colors();
}

//activate lights
void setColor(int red, int green, int blue)
{
  #ifdef COMMON_ANODE
    red = 255 - red;
    green = 255 - green;
    blue = 255 - blue;
  #endif
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);  
}

//set spell
void set_spreuk_sub_index()
{
    if(sensorValue >=0 && sensorValue <=200)
      {
        spreuken_sub_index = 0;
      }

    if(sensorValue >= 201 && sensorValue <= 400)
      {
        spreuken_sub_index = 1;
      }
    if(sensorValue >= 401 && sensorValue <= 600)
      {
        spreuken_sub_index = 2;
      }
    if(sensorValue >= 601 && sensorValue <= 800)
      {
        spreuken_sub_index = 3;
      }
    if(sensorValue >= 801)
      {
        spreuken_sub_index = 4;
      }
}

void Meter()
{
  
  sensorValue = analogRead(A0);
  // print out the value you read:
  //sensorValue = 0;
  Serial.println(sensorValue);
  delay(1);  // delay in between reads for stability
  
}


void set_spreuk_index()
{
  // read the state of the pushbutton value:
  button_A_State = digitalRead(button_A_Pin);
  //Serial.println(button_A_State);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (button_A_State == HIGH) 
    {
      //setColor(255, 0, 0);
      //Serial.println("on");
      if (spreuken_index == 0)
      {
        spreuken_index = 1;
        //setColor(255, 0, 0);
        delay(500);
      }
      else
      {
        spreuken_index = 0;
        //setColor(0, 0, 255);
        delay(500);
      }
    }
}


//cast/uncast spell
void pre_cast_spell()
{
  // read the state of the pushbutton value:
  button_B_State = digitalRead(button_B_Pin);
  Serial.println(button_B_State);
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (button_B_State == HIGH) 
    {
      //setColor(255, 0, 0);
      //Serial.println("on");
        cast_spell();
    }
  else
  {
    digitalWrite(laser_pin,LOW);
  }
}

void cast_spell()
{
  digitalWrite(laser_pin,HIGH);
  if (spreuken_index == 0)
    {
      if (spreuken_sub_index == 0)
      {
        spell_aurus();
      }
      if (spreuken_sub_index == 1)
      {
        spell_cibus();
      }
    }
  if (spreuken_index == 1)
    {
      if (spreuken_sub_index == 0)
      {
        spell_shield();
      }
      if (spreuken_sub_index == 1)
      {
        spell_healing();
      }
    }
}

void uncast_spell()
{
  setColor(0, 0, 0);
  delay(500);
}


//all spell functions
//normal spells
void spell_aurus()
{
  setColor(255, 0, 0);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(0, 255, 0);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(255, 0, 0);
  delay(150);
  setColor(0, 0, 0);
}

void spell_cibus()
{
  setColor(0, 0, 255);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(0, 0, 255);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(0, 0, 255);
  delay(150);
  setColor(0, 0, 0);
}

//attack spells
void spell_shield()
{
  setColor(255, 0, 255);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(255, 0, 255);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(255, 0, 255);
  delay(150);
  setColor(0, 0, 0);
}

void spell_healing()
{
  setColor(255, 255, 255);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(255, 255, 255);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(255, 255, 255);
  delay(150);
  setColor(0, 0, 0);
}

//test's
void test_spell()
{
  setColor(255, 0, 0);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(255, 0, 0);
  delay(150);
  setColor(0, 0, 0);
  delay(150);
  setColor(0, 0, 255);
  delay(150);
  setColor(0, 0, 0);
}

void test_colors()
{
  setColor(255, 0, 0);  // red
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000);  
  setColor(150, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);  // aqua
  delay(1000);
  Serial.println("test_light");
}