// MidiVizControlBank
// Lew Hill II

#include "MidiVizControllerBank.h"

MidiVizControllerBank::MidiVizControllerBank(){
  init();
}

void MidiVizControllerBank::init(){

  for (int i = 0; i < NUM_CONTROLLERS; i++){
    controllerValues[i] = 0;
  }

  position[0] = 0;
  position[1] = 0;  
  position[2] = 0;

  rotation[0] = 0;
  rotation[1] = 0;  
  rotation[2] = 0;

  scale[0] = 0;
  scale[1] = 0;  
  scale[2] = 0;

}

void MidiVizControllerBank::processMidiEvent(MidiEvent event){
  
  
  //  int controllerNum = event.getControllerNumber(); 
  int controllerValue = event.getControllerValue();
  
  if (event.isRadiumSlider1()){
    controllerValues[0] = controllerValue;
    
  } else if (event.isRadiumSlider2()){
    controllerValues[1] = controllerValue;
    
  } else if (event.isRadiumSlider3()){
    controllerValues[2] = controllerValue;
    
  } else if (event.isRadiumSlider4()){
    controllerValues[3] = controllerValue;
    
  } else if (event.isRadiumSlider5()){
    controllerValues[4] = controllerValue;
    
  } else if (event.isRadiumSlider6()){
    controllerValues[5] = controllerValue;
    
  } else if (event.isRadiumSlider7()){
    controllerValues[6] = controllerValue;
    
  } else if (event.isRadiumSlider8()){
    controllerValues[7] = controllerValue;
    
  } else if (event.isRadiumKnob9()){
    controllerValues[8] = controllerValue;
    
  } else if (event.isRadiumKnob10()){
    controllerValues[9] = controllerValue;
    
  } else if (event.isRadiumKnob11()){
    controllerValues[10] = controllerValue;
    
  } else if (event.isRadiumKnob12()){
    controllerValues[11] = controllerValue;
    
  } else if (event.isRadiumKnob13()){
    controllerValues[12] = controllerValue;
    
  } else if (event.isRadiumKnob14()){
    controllerValues[13] = controllerValue;
    
  } else if (event.isRadiumKnob15()){
    controllerValues[14] = controllerValue;
    
  } else if (event.isRadiumKnob16()){
    controllerValues[15] = controllerValue;
    
  } else if (event.isRadiumKnobDataEntry()){
    controllerValues[16] = controllerValue;
    
  } 

}    

void MidiVizControllerBank::frame(){
  
  //
  //  for (int i = 0; i < 16; i++){
  //  }
  
}

void MidiVizControllerBank::draw(){

  // frame turn rate is the sum of the note spins.

  glPushMatrix();
  glTranslatef(position[0], position[1], position[2]);
  glRotatef(rotation[2], 0.0, 0.0, 1.0); 
  glRotatef(rotation[1], 0.0, 1.0, 0.0); 
  glRotatef(rotation[0], 1.0, 0.0, 0.0); 
  glScalef(scale[0], scale[1], scale[2]);

  for (int i = 0; i < NUM_CONTROLLERS; i++){
    
    glPushMatrix();
    
    float* colors = color_table.getValue(i+1);

    glColor3fv(colors);

    glTranslatef((i-6)*1, 0.0, 0.0);

    drawbox(-.30,.30, 0, controllerValues[i]*.05, -0.30, .30, GL_QUADS); 
   
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINE_LOOP);
    glVertex3f(-0.31, 0, -0.31);
    glVertex3f(0.31, 0, -0.31);
    glVertex3f(0.31, 0, 0.31);
    glVertex3f(-0.31, 0, 0.31);
    glEnd();

    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINE_LOOP);
    glVertex3f(-0.31, controllerValues[i] * .051, -0.31);
    glVertex3f(0.31,  controllerValues[i] * .051, -0.31);
    glVertex3f(0.31,  controllerValues[i] * .051, 0.31);
    glVertex3f(-0.31, controllerValues[i] * .051, 0.31);
    glEnd();


    glBegin(GL_LINES);
    glVertex3f(-0.31, controllerValues[i] * .051, -0.31);
    glVertex3f(-0.31, 0, -0.31);

    glVertex3f(0.31, controllerValues[i] * .051, -0.31);
    glVertex3f(0.31, 0, -0.31);

    glVertex3f(0.31, controllerValues[i] * .051, 0.31);
    glVertex3f(0.31, 0, 0.31);

    glVertex3f(-0.33, controllerValues[i] * .05, 0.33);
    glVertex3f(-0.33, 0, 0.33);
    
    glEnd();

    glPopMatrix();
  }

  glPopMatrix();
  
}

void MidiVizControllerBank::drawbox(GLdouble x0, GLdouble x1, 
				    GLdouble y0, GLdouble y1,
				    GLdouble z0, GLdouble z1, GLenum type)
{
   static GLdouble n[6][3] = {
      {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
      {0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0}
   };
   static GLint faces[6][4] = {
      { 0, 1, 2, 3}, { 3, 2, 6, 7}, { 7, 6, 5, 4},
      { 4, 5, 1, 0}, { 5, 6, 2, 1}, { 7, 4, 0, 3}
   };
   GLdouble v[8][3], tmp;
   GLint i;

   if (x0 > x1)
   {
      tmp = x0; x0 = x1; x1 = tmp;
   }
   if (y0 > y1)
   {
      tmp = y0; y0 = y1; y1 = tmp;
   }
   if (z0 > z1)
   {
      tmp = z0; z0 = z1; z1 = tmp;
   }
   v[0][0] = v[1][0] = v[2][0] = v[3][0] = x0;
   v[4][0] = v[5][0] = v[6][0] = v[7][0] = x1;
   v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
   v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
   v[0][2] = v[3][2] = v[4][2] = v[7][2] = z0;
   v[1][2] = v[2][2] = v[5][2] = v[6][2] = z1;

   for (i = 0; i < 6; i++)
   {
      glBegin(type);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][0]][0]);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][1]][0]);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][2]][0]);
         glNormal3dv(&n[i][0]);
         glVertex3dv(&v[faces[i][3]][0]);
      glEnd();
   }
}

