// Project 8: Array of Objest; Sorted by properties which displays in a table. //
// Sumaiya Haider //

// Title & Author //
String title = "Array of Objects: Sorted by properties \n Sumaiya Haider";

// Strings of Arrays //
String names[]= { "John", "Eric", "Nate", "Luke", "Tom", "Nick", "Dan", "Blair", "Chuck", "Serena" };
String columns[]= { "Name", "Age", "Weight", "Height", "BMI" };
int age[]= { 10, 20, 22, 66, 34, 50, 85, 32, 77, 18 };
int wt[]= { 90, 110, 250, 122, 225, 101, 168, 235, 300, 150 };
int ht[]= { 40, 45, 50, 55, 60, 65, 70, 75, 80, 44 };
//float[] bmis= new float[many];
//float bmis=25.0;

int many = names.length;

int[] rcolors= new int[many], gcolors= new int[many], bcolors= new int[many];

void setup() {
  size (800,700);
  reset();
  next();
}

void reset() {
  //// Re-initialize all members.
  for (int i=0; i<many; i++) {
    int r=int(random(200)), g=int(random(200)), b=int(random(200));
    int rr=(r+128)%256, gg=(g+128)%256, bb=(b+128)%256;
    age= int(random(10,100));
    wt= int(random(50,300));
    ht= int(random(40,80));
  }
}


/*void colors() {
  // Initialize colors //
  for (int i=0; i<many; i++ ) {
   rcolors[i]=  int( random(200) );
   gcolors[i]=  int( random(200) );
   bcolors[i]=  int( random(200) );
  }
}*/

void draw() {
  // Next frame //
  background(255,255,255);
  fill(0);
  drawTable();
  show();
  textSize (15);
  text (title, 150, 35);
  text( "Press keys a, w, h, or n to sort by age, weight, height, or name", width/4, height-30);
  text( "Press q key to quit.", width/4, height-10);
}

void drawTable() {
  // Table of information //
  fill(255,255,255);
  noStroke();
  rect(50, 50, 500, 700);
  // Fill in BMI Array //
//  for (int j=0; j<many; j++) {
//   bmis[j]=  bmicalc( ht[j], wt[j] );
  
  
  // Verticle lines: Arrays of names //
 noStroke();
  
  int n=0;
  for (int y=120; y<600; y= y+50) {
    line(100, y, 600, y);
    fill(0);
    textSize(15);
    text(""+names[n], 95, y+20);
    n= n+1;
    
  }
  
  // Horizontal lines: Arrays of columns //
  noStroke();
  
  int c=0;
  for (int x=50; x<500; x= x+100) {
    line(x, 100, x, 600);
    fill(0);
    textSize(15);
    text (""+columns[c], x+50, 100);
    c= c+1;
  }
  
  // Age of the members //
  int a=0;
  for (int y=120; y<600; y= y+50) {
   fill(0);
   textSize(12);
   text(""+age[a], 200, y+20);
  a= a+1;
  } 
  
  // Weight of members //  
  int w=0;
  for ( int y=120; y<600; y= y+50 ) {
    fill(0);
    textSize(12);
    text(""+wt[w], 300, y+20);
    w= w+1;
  }
  
  // Height of the members //
  int h=0;
  for ( int y=120; y<600; y= y+50 ) {
    fill(0);
    textSize(12);
    text(""+ht[h], 400, y+20);
    h= h+1;
  }
}

void keyPressed() {
  // Respond to keys //
  if ( key=='a' ) {
    sortby (age, many);
  }
  if ( key=='w') {
    sortby (wt,many);
  }
  if ( key=='h') {
    sortby (ht,many);
  }
  if ( key=='n' ) {
    sortby (names,many);
  }
  if ( key=='r' ) {
    reset();
  }
//  if ( key=='b' ) {
//    sortby (bmis,many);
//  }
  if ( key=='q' ) {
    exit();
  }
}

void sortby( int[] a, int many ) {
  // Rearrange ALL arrays, using a[] as "key" //
  for( int m=many; m>1; m-- ) {
    // Shrinking array (until only one element). //
    int w=  wherebig( a, m );    // Find biggest. //
    swapall( w, m-1 );           // Move to end //
  }
}
void sortby( float[] a, int many ) {  // See above //
  for( int m=many; m>1; m-- ) {
    swapall( wherebig(a,m), m-1 );  // Move biggest to end //
  }
}
void sortby( String[] a, int many ) {  // See above //
  for( int m=many; m>1; m-- ) {
    swapall( wherebig(a,m), m-1 );  // Move biggest to end //
  }
}
void swapall( int j, int k ) {
  // Swap all fields of two records // 
  swap( names, j, k );
  swap( age, j, k );
  swap( ht, j, k );
  swap( wt, j, k );
//  swap( bmis, j, k );
}
void swap( int[] a, int j, int k) { int t=a[j]; a[j]=a[k]; a[k]=t; }
void swap( float[] a, int j, int k) { float  t=a[j]; a[j]=a[k]; a[k]=t; }
void swap( String[] a, int j, int k) { String t=a[j]; a[j]=a[k]; a[k]=t; }

// Return index of biggest. //
int wherebig( int[] a, int m ) {
  // Return index of biggest. //
  int w=0;
  for (int j=1; j<m; j++ ) { 
    if ( a[j] > a[w] ) w=j;      // Index of biggest, so far //    
  }
  return w;
}
int wherebig( float[] a, int m ) { 
  int w=0;  
  for (int j=1; j<m; j++) { w=  a[j]>a[w] ? j : w;  }
  return w;
}
int wherebig( String[] a, int m ) {
  // Return index of biggest. //
  int w=0;
  for (int j=1; j<m; j++ ) { 
    if ( a[j].compareTo(a[w]) > 0 ) w=j;      // biggest, so far //
  }
  return w;
}

void next() {
  // Increase age by one, adjust the ht and wt as follows
}
/*void show( float x, float y ) {
    // Display an object at (x,y) //
    fill( rcolors.[i] );
    rectMode( CENTER );
    rect( x, y, wt/10, ht/2 );
    rectMode( CORNER );
    fill( gcolors.[i] );
    ellipse( x, y-ht/4-bmi/10, bmi/5, 5+bmi/5 );
  textSize(20);
    text( name.charAt(0), x-10, y+10 );    // Large initial //
  textSize(12);
    fill( bcolors.[i] );
    text( name, x-20, y+12+ht/2 );
  }

// Calculate BMI //
void bmitable() {
  //// Compute a BMI values.
  for (int j=0; j<many; j++) {
    bmis[j]=  bmicalc( ht[j], wt[j] );
  }
}
float bmicalc( int h, int mass ) {
  //// Return bmi index
  return 703.0 * h / (mass*mass);
}*/