#include "console.h"

CConsole::CConsole(void)
{
filemap[0]='*';
}

CConsole::~CConsole(void)
{
}
void CConsole::ListSphere(string dirPath)
{
_finddata_t file;
    
string subPath =dirPath; //declare subpath variable. This is just a string

//Find the first position of a dot which means a fileName.extension. Pass a reference to your _finddata structure (file) to it.
long currentPosition = _findfirst("sphere*.jpg", &file);
numsphere=0;

if (currentPosition == -1L)
{
   return ;
}
else // If a file is found
{
             // The following do...while loop, loops through all the items found in the current directory. Each directory has a '.' and '..' item which is
             // the root or parent of that directory. We want to ignore those. Unfortuntely this method can not be used for files residing on the
             // root. There are way around it though.
             do
              {
                   //ignore . and ..  
                   if(strcmp(file.name , ".") != 0 && strcmp(file.name , "..") !=0)
                   {
                        // A bitwise checking for the 'attrib' attribute of the _finddata structure. This means if the item is a subdirectory recursively
                        // call this method to find the files inside this subdirectory. ListDirect was the name of this current  method in my code. Replace
                        // it with the name of your own method , i.e. call this method recursively for your subdirectories. Depending on what you are
                        // doing you can take this part out.
                        if (file.attrib & _A_SUBDIR)
                        {
                             //if the directory has subdirectories, recursing the subdirectories
                             subPath = dirPath;
                             subPath.append ("\\");
                             subPath.append (file.name );
                             ListSphere(subPath);
                        }
                        else // if the item is not a subdirectory, then you can use other attributes of the _finddata structure to extract the information you
                               // want (e.g. size), there are a bunch of other attributes of this structure like the time stamp etc. which you can find on MSDN.
                        { 
                            fprintf(stdout," %s\n",file.name);
                            sphere[numsphere]=file.name;
                            if (numsphere==0) sphere[0].copy(filesphere,string::npos);
                            numsphere++;
                             //adding up the size of files
              /*               totalSize += file.size ;
                             if(file.size % 1024 == 0)
                                  roundTotalSize += (file.size / 1024); //rounding up the size of files
                             else
                                  roundTotalSize += ((file.size / 1024) + 1); */
                        }
                   }
                
              }while(_findnext(currentPosition, &file) == 0);
         }
         _findclose(currentPosition); //close search



}
void CConsole::ListMap(string dirPath)
{
    _finddata_t file;
    
string subPath =dirPath; //declare subpath variable. This is just a string

//Find the first position of a dot which means a fileName.extension. Pass a reference to your _finddata structure (file) to it.
long currentPosition = _findfirst("*.mnm", &file);
nummap=0;

if (currentPosition == -1L)
{
   return ;
}
else // If a file is found
{
             // The following do...while loop, loops through all the items found in the current directory. Each directory has a '.' and '..' item which is
             // the root or parent of that directory. We want to ignore those. Unfortuntely this method can not be used for files residing on the
             // root. There are way around it though.
             do
              {
                   //ignore . and ..  
                   if(strcmp(file.name , ".") != 0 && strcmp(file.name , "..") !=0)
                   {
                        // A bitwise checking for the 'attrib' attribute of the _finddata structure. This means if the item is a subdirectory recursively
                        // call this method to find the files inside this subdirectory. ListDirect was the name of this current  method in my code. Replace
                        // it with the name of your own method , i.e. call this method recursively for your subdirectories. Depending on what you are
                        // doing you can take this part out.
                        if (file.attrib & _A_SUBDIR)
                        {
                             //if the directory has subdirectories, recursing the subdirectories
                             subPath = dirPath;
                             subPath.append ("\\");
                             subPath.append (file.name );
                             ListMap(subPath);
                        }
                        else // if the item is not a subdirectory, then you can use other attributes of the _finddata structure to extract the information you
                               // want (e.g. size), there are a bunch of other attributes of this structure like the time stamp etc. which you can find on MSDN.
                        { 
                            fprintf(stdout," %s\n",file.name);
                            map[nummap]=file.name;
                            if (nummap==0) map[0].copy(filemap,string::npos);
                            nummap++;
                             //adding up the size of files
              /*               totalSize += file.size ;
                             if(file.size % 1024 == 0)
                                  roundTotalSize += (file.size / 1024); //rounding up the size of files
                             else
                                  roundTotalSize += ((file.size / 1024) + 1); */
                        }
                   }
                
              }while(_findnext(currentPosition, &file) == 0);
         }
         _findclose(currentPosition); //close search





}
void CConsole::item(int pos,char *str, bool sel)
{ // str[strlen(str)]='\0';
//  if (sel) {strcpy(selitem,str); }
  glPrintf(SizeX/2,SizeY-110-pos*20+5,1,12,sel,str,pos);
  DrawBox(0,SizeY-110-pos*20,SizeX,20,sel);
}
void CConsole::menu(int type,int selpos) 
{
  int i;
    bool a[100];
    for (i=0; i<=99;i++) a[i]=false;
    a[selpos]=true;
    switch(type) {
    case MENU_GAMEOVER:
        title("GAME OVER");
        item(ITEM_GAMEOVER,"Return Main Menu",a[ITEM_GAMEOVER]);
        numitem=ITEM_GAMEOVER;
        break;
    case MENU_MAIN:
        title("MARBLE GAME");
        item(ITEM_MAIN_START,"Start",a[ITEM_MAIN_START]);
        item(ITEM_MAIN_MAP,"Load Map",a[ITEM_MAIN_MAP]);
        item(ITEM_MAIN_DISPLAY,"Display",a[ITEM_MAIN_DISPLAY]);
        item(ITEM_MAIN_CAMERA,"Camera",a[ITEM_MAIN_CAMERA]);
        item(ITEM_MAIN_GRAPHICS,"Graphics",a[ITEM_MAIN_GRAPHICS]);
        item(ITEM_MAIN_BALL,"Ball",a[ITEM_MAIN_BALL]);
        item(ITEM_MAIN_CONTROL,"Control",a[ITEM_MAIN_CONTROL]);
        item(ITEM_MAIN_TIMER,"Timer",a[ITEM_MAIN_TIMER]);
        item(ITEM_EXIT,"Exit",a[ITEM_EXIT]);
        numitem=ITEM_EXIT;
        break;
    case MENU_DEAD:
        title("OH NOOO");
        item(ITEM_DEAD,"Continue Game", a[ITEM_DEAD]);
        numitem=ITEM_DEAD;
        
        break;
    case MENU_TIMEOUT:
        title("OH NOOO TIME OUT!");
        item(ITEM_DEAD,"Continue Game", a[ITEM_DEAD]);
        numitem=ITEM_DEAD;
        
        break;
    case MENU_GOAL:
        title("GOAL!");
        item(ITEM_GOAL,"Next Map", a[ITEM_GOAL]);
        numitem=ITEM_GOAL;
        break;
    case MENU_PAUSE:
        title("PAUSE");
        item(ITEM_MAIN_START,"Continue Game",a[ITEM_MAIN_START]);
        item(ITEM_MAIN_MAP,"Load Map",a[ITEM_MAIN_MAP]);
        item(ITEM_MAIN_DISPLAY,"Display",a[ITEM_MAIN_DISPLAY]);
        item(ITEM_MAIN_CAMERA,"Camera",a[ITEM_MAIN_CAMERA]);
        item(ITEM_MAIN_GRAPHICS,"Graphics",a[ITEM_MAIN_GRAPHICS]);
        item(ITEM_MAIN_BALL,"Ball",a[ITEM_MAIN_BALL]);
        item(ITEM_MAIN_CONTROL,"Control",a[ITEM_MAIN_CONTROL]);
        item(ITEM_MAIN_TIMER,"Timer",a[ITEM_MAIN_TIMER]);
        item(ITEM_EXIT,"Exit",a[ITEM_EXIT]);
        numitem=ITEM_EXIT;
        break;
    case MENU_MAP:
        title ("MAPS");

    
        for (i=0;i<nummap;i++) {

            map[i].copy(filemap,string::npos);
            char *s;
            s=strstr(filemap,".mnm");
            s[0]='\0';
            //filename[strlen(filename)]='\0';
                item(i+1,filemap,a[i+1]);
        }

        numitem=nummap;
        break;





    case MENU_GRAPHICS:
        title("Graphics");
        item(ITEM_GRAPHICS_TEXTURED,"Textured",a[ITEM_GRAPHICS_TEXTURED]);
        item(ITEM_GRAPHICS_NORMAL,"Textured+Normal Light",a[ITEM_GRAPHICS_NORMAL]);
        item(ITEM_GRAPHICS_WIREFRAME,"Wireframe",a[ITEM_GRAPHICS_WIREFRAME]);
        item(ITEM_GRAPHICS_SOLID,"Solid",a[ITEM_GRAPHICS_SOLID]);
        item(ITEM_GRAPHICS_ENV,"Enviroment",a[ITEM_GRAPHICS_ENV]);
        item(ITEM_GRAPHICS_ROBOT,"Enemy Robot",a[ITEM_GRAPHICS_ROBOT]);
        item(ITEM_GRAPHICS_NOROBOT,"Enemy NoRobot",a[ITEM_GRAPHICS_NOROBOT]);
        item(ITEM_GRAPHICS_BACK,"Back",a[ITEM_GRAPHICS_BACK]);
        numitem=ITEM_GRAPHICS_BACK;
        break;
    case MENU_DISPLAY:
        title("Display");
        item(ITEM_DISPLAY_W_640x480,"Window: 640x480",a[ITEM_DISPLAY_W_640x480]);
        item(ITEM_DISPLAY_W_800x600,"Window: 800x600",a[ITEM_DISPLAY_W_800x600]);
        item(ITEM_DISPLAY_W_1024x768,"Window:1024x768",a[ITEM_DISPLAY_W_1024x768]);
        item(ITEM_DISPLAY_FULLSCREEN,"FullScreen",a[ITEM_DISPLAY_FULLSCREEN]);
        item(ITEM_DISPLAY_BACK,"Back",a[ITEM_DISPLAY_BACK]);
        numitem=ITEM_DISPLAY_BACK;
        break;
    case MENU_CAMERA:
        title("Camera");
        item(ITEM_CAMERA_FOLLOW ,"Follow Ball",a[ITEM_CAMERA_FOLLOW ]);
        item(ITEM_CAMERA_NFOLLOW ,"Not Follow Ball",a[ITEM_CAMERA_NFOLLOW ]);
        item(ITEM_CAMERA_BACK ,"Back",a[ITEM_CAMERA_BACK ]);
        numitem=ITEM_CAMERA_BACK ;
        break;
    case MENU_CONTROL:
        title("Control");
        item(ITEM_CONTROL_ROLL,"Roll Control",a[ITEM_CONTROL_ROLL]);
        item(ITEM_CONTROL_LUKE,"Luke Control",a[ITEM_CONTROL_LUKE]);
        item(ITEM_CONTROL_SNS20,"Sensibility Mouse 20",a[ITEM_CONTROL_SNS20]);
        item(ITEM_CONTROL_SNS40,"Sensibility Mouse 40",a[ITEM_CONTROL_SNS40]);
        item(ITEM_CONTROL_SNS60,"Sensibility Mouse 60",a[ITEM_CONTROL_SNS60]);
        item(ITEM_CONTROL_SNS80,"Sensibility Mouse 80",a[ITEM_CONTROL_SNS80]);
        item(ITEM_CONTROL_BACK,"Back",a[ITEM_CONTROL_BACK]);

        numitem=ITEM_CONTROL_BACK;
        break;
    case MENU_ENV:
        title("Enviroment");
        item(ITEM_ENV_DEFAULT,"Default",a[ITEM_ENV_DEFAULT]);
        item(ITEM_ENV_METAL,"Metal",a[ITEM_ENV_METAL]);
        item(ITEM_ENV_GLASS,"Glass",a[ITEM_ENV_GLASS]);
        item(ITEM_ENV_PLASTIC,"Plastic",a[ITEM_ENV_PLASTIC]);
        item(ITEM_ENV_SHINING,"Shining",a[ITEM_ENV_SHINING]);
        item(ITEM_ENV_BACK,"Back",a[ITEM_ENV_BACK]);
        numitem=ITEM_ENV_BACK;
        break;
    case MENU_BALL:
            title ("Ball");

    
        for (i=0;i<numsphere;i++) {

            sphere[i].copy(filesphere,string::npos);
            char *s;
            s=strstr(filesphere,".jpg");
            s[0]='\0';
            //filename[strlen(filename)]='\0';
                item(i+1,filesphere,a[i+1]);
        }

        numitem=numsphere;

        break; 
    case MENU_TIMER:
        title("Timer");
        item(ITEM_TIMER_100,"100 sec",a[ITEM_TIMER_100]);   
        item(ITEM_TIMER_300,"300 sec",a[ITEM_TIMER_300]);
        item(ITEM_TIMER_600,"600 sec",a[ITEM_TIMER_600]);
        item(ITEM_TIMER_UNLIMITED,"unlimited",a[ITEM_TIMER_UNLIMITED]);
        item(ITEM_TIMER_BACK,"Back",a[ITEM_TIMER_BACK]);
        numitem=ITEM_TIMER_BACK;
        break;
    }   
}
void CConsole::setSize(int X, int Y)
{
SizeX=X;
SizeY=Y;
}
void CConsole::title( char *t)
{       
    glPrintf(SizeX/2,SizeY-50,1,40,false,t);
    DrawBox(0,SizeY-100,SizeX,100,false);

}
void CConsole::DrawBox(int x, int y , int sizex, int sizey, bool sel)
{   
    glPushAttrib(GL_ENABLE_BIT|GL_LIST_BIT|GL_CURRENT_BIT); // Pushes the display list bits 
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_1D);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_LINE_SMOOTH);
    if (!sel) {glColor3f(0.6,0.6,0.6);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    } else
    {glColor3f(0,0,0);
        glDisable(GL_BLEND);
    }
    

int vp[4];
    glGetIntegerv(GL_VIEWPORT,vp);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();


    glLoadIdentity();
    gluOrtho2D(vp[0], vp[0]+vp[2], vp[1], vp[1]+vp[3]);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();

    
    glLoadIdentity();
    //glRasterPos2f(2,3);
    glBegin(GL_POLYGON);
            glVertex2i(x,y);  
            glVertex2i(x+sizex,y);
            glVertex2i(x+sizex,y+sizey);
            glVertex2i(x,y+sizey);
    glEnd();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
    glPopAttrib();


}

void CConsole::printf(int x, int y, char* format)
{
    glPushAttrib(GL_ENABLE_BIT|GL_LIST_BIT|GL_CURRENT_BIT); // Pushes the display list bits 
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_1D);
    glDisable(GL_TEXTURE_2D);
    //glDisable(GL_TEXTURE_CUBE_MAP);

int vp[4];
    glGetIntegerv(GL_VIEWPORT,vp);
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();


    char strl[120];
    char *c;
    sprintf(strl,"marble test!");
    glLoadIdentity();
    gluOrtho2D(vp[0], vp[0]+vp[2], vp[1], vp[1]+vp[3]);
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();


    glLoadIdentity();
    //glRasterPos2f(2,3);
    glRasterPos2f(x,y);
 
    for (c=strl; *c != '\0'; c++)
       {
            glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *c);
        }
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
    glPopMatrix();
  glPopAttrib();

}
void CConsole::glPrintf(GLuint x, GLuint y, int align, GLfloat scale,bool sel, char* format, ...)
{
    va_list args;
    char buffer[255], *p;
        int len;
        float dx=0;
    GLfloat font_scale = 119.05f;// + 33.33f;

    va_start(args, format);
    vsprintf(buffer, format, args);
    va_end(args);

        if(align) 
        {
            len= (int) strlen(buffer);
            dx=((scale*len)/2)*104.76/119.05;
        }
    glMatrixMode(GL_PROJECTION);
    glPushMatrix();
    glLoadIdentity();
    gluOrtho2D(0, glutGet(GLUT_WINDOW_WIDTH), 0, glutGet(GLUT_WINDOW_HEIGHT));

    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    glLoadIdentity();

    glPushAttrib(GL_ENABLE_BIT);
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LINE_SMOOTH);
    if (sel) {
    glColor3f(1,1,1);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    }else
    {
    glColor3f(0,0,0);
        glDisable(GL_BLEND);
    }
    glTranslatef(x-dx, y, 0.0);

    glScalef(scale/font_scale, scale/font_scale, scale/font_scale);

    for(p = buffer; *p; p++)
            glutStrokeCharacter(GLUT_STROKE_MONO_ROMAN, *p);
  
    glPopAttrib();

    glPopMatrix();
    glMatrixMode(GL_PROJECTION);
    glPopMatrix();
    glMatrixMode(GL_MODELVIEW);
}