#include "cs148.h"

// Screen size
int screenWidth = 640;
int screenHeight = 480;

// Start time, current time.
double startT;

// Display function -- called to redraw window
void myDisplay(void) {

  const double TIME_RATE = 30.0;
  double curT = CS148::getTime() - startT;

  double days = curT * TIME_RATE;
  double months = days / 30.0;
  double years = days / 365.0;

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glPushMatrix();
    
    /*
    glColor3f(0.3, 0.3, 0.3);
    double PLANE_SIZE = 2.;
    glBegin(GL_QUADS);
    glVertex3d(PLANE_SIZE, PLANE_SIZE, 0.);
    glVertex3d(-PLANE_SIZE, PLANE_SIZE, 0.);
    glVertex3d(-PLANE_SIZE, -PLANE_SIZE, 0.);
    glVertex3d(PLANE_SIZE, -PLANE_SIZE, 0.);
    glEnd();
    */

    // draw sun
    glPushMatrix();
      glColor3f(1.0, 1.0, 0.0);
      glRotatef(90.0, 1.0, 0.0, 0.0);  /*  rotate it upright  */
      glutSolidSphere(1.0, 10, 10);
    glPopMatrix();

    // draw smaller planet
    glColor3f(1.0, 0.0, 0.0);
    glRotatef(years * 360., 0.0, 1.0, 0.0);
    glTranslatef(2.0, 0.0, 0.0);
    glPushMatrix();
      glRotatef(90.0, 1.0, 0.0, 0.0);  /*  rotate it upright  */
      glutSolidSphere(0.4, 10, 10);
    glPopMatrix();

    // draw moon
    glColor3f(0.0, 0.0, 1.0);
    glRotatef(months * 360., 0.0, 1.0, 0.0);
    glTranslatef(1.0, 0.0, 0.0);
    glutSolidSphere(0.2, 10, 10);

  glPopMatrix();

  glutSwapBuffers();
}


// Reshape function -- called whenever window size changes
//   w, h = new window size
void myReshape(int w, int h) {
  screenWidth = w;
  screenHeight = h;
  glViewport(0, 0, screenWidth, screenHeight);  
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(50.0, (GLdouble)screenWidth / (GLdouble)screenHeight, 1.0, 10.);
  glMatrixMode(GL_MODELVIEW);
} 


// Idle function
void myIdle(void) {
  glutPostRedisplay();
}


// Initialization code
void myInit(void) {
  glEnable(GL_DEPTH_TEST);
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(0., 3., 5., 0., 0., 0., 0., 1., 0.);
  startT = CS148::getTime();
}


// Main function
int main(int argc, char** argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);
  glutInitWindowSize(screenWidth, screenHeight);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("Planet Animation");
  glutDisplayFunc(myDisplay);
  glutReshapeFunc(myReshape);
  glutIdleFunc(myIdle);
  myInit();
  glutMainLoop();
  return 0;
}




