#include "cGrid.h"

#include "OGLPlatformSpecific.h"
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "cGame.h"

cGrid::cGrid()
{
    for(int y = 0; y < gridYSize_; ++y )
        for(int x = 0; x < gridXSize_; ++x )
        {
            blocks_[x][y].x_ = x;
            blocks_[x][y].y_ = y;
            blocks_[x][y].type_ = EMPTY_BLOCK;
            blocks_[x][y].grid_ = this;
        }
}

void cGrid::draw()
{
    for(int y = 0; y < gridYSize_; ++y )
        for(int x = 0; x < gridXSize_; ++x )
            blocks_[x][y].draw();
}

cGrid::cBlock::cBlock()
{
    size_ =   1;
}

bool cGrid::cBlock::canMove(int dx, int dy)
{
    int newX = x_ + dx;
    int newY = y_ + dy;
    
    if(newX < 0 || newX >= cGrid::gridXSize_) return false;
    if(newY < 0 || newY >= cGrid::gridYSize_) return false;
    
    if(grid_->blocks_[newX][newY].type_ != EMPTY_BLOCK) return false;
    
    return true;
}

bool cGrid::cBlock::move(int dx, int dy)
{
    if(canMove(dx, dy))
    {
        x_ += dx;
        y_ += dy;
        return true;
    }
    return false;
}

BLOCK_TYPE cGrid::cBlock::getRandomBlockType()
{
    switch(rand()%20)
    {
    case 0: case 1: case 2:  	return RED_BLOCK; 
    case 3: case 4: case 5: 	return BLUE_BLOCK;
    case 6: case 7: case 8:  	return GREEN_BLOCK;
    case 9: case 10: case 11:	return ORANGE_BLOCK;
    case 12: case 13: case 14:	return PURPLE_BLOCK;
    
    case 15: return RED_EXPLODER; 
    case 16: return BLUE_EXPLODER;
    case 17: return GREEN_EXPLODER;
    case 18: return ORANGE_EXPLODER;
    case 19: return PURPLE_EXPLODER;
    }
}

void cGrid::cBlock::draw()
{
    glPushMatrix();
    
    glScalef(.25,.25,.25);
    glTranslatef((x_ - cGrid::gridXSize_/2) * size_ * 2, (y_- cGrid::gridYSize_/2) * size_ * 2, 0);
    
    glColor3f(	.2 + .05 * cos((float)x_*.2 + (float)y_ * .2 + 643 + .3 * cGame::instance()->t_), 
                .2 + .05 * cos((float)x_*.2 + (float)y_ * .2 + 234 + .3 * cGame::instance()->t_), 
                .2 + .05 * cos((float)x_*.2 + (float)y_ * .2 + 432 + .3 * cGame::instance()->t_) ); 
    
    // alwaays draw an empty block first
    glBegin(GL_TRIANGLE_STRIP);						
        glVertex3f(size_, size_, 0); 	
        glVertex3f(-size_, size_, 0); 
        glVertex3f(size_, -size_, 0); 
        glVertex3f(-size_, -size_, 0); 	
    glEnd();
    
    switch(type_)
    {
    case RED_BLOCK:  		glColor3f(1, 0, 0); break;
    case BLUE_BLOCK:  		glColor3f(0, 0, 1); break;
    case GREEN_BLOCK:  		glColor3f(0, 1, 0); break;
    case ORANGE_BLOCK:		glColor3f(1, .5, 0); break;
    case PURPLE_BLOCK:		glColor3f(.5, 0, .5); break;
    
    case EMPTY_BLOCK: /* color is already set*/ break;
    
    case RED_EXPLODER:		glColor3f(1, 0, 0); break;
    case BLUE_EXPLODER:		glColor3f(0, 0, 1); break;
    case GREEN_EXPLODER:	glColor3f(0, 1, 0); break;
    case ORANGE_EXPLODER:	glColor3f(1, .5, 0); break;
    case PURPLE_EXPLODER:	glColor3f(.5, 0, .5); break;
    
    }
        
    switch(type_)
    {
    case EMPTY_BLOCK: /* already draw */ break;
    case RED_BLOCK: 
    case BLUE_BLOCK: 
    case GREEN_BLOCK:
    case ORANGE_BLOCK:
    case PURPLE_BLOCK:
        glBegin(GL_TRIANGLE_STRIP);						
        glVertex3f(size_, size_, 0); 	
        glVertex3f(-size_, size_, 0); 
        glVertex3f(size_, -size_, 0); 
        glVertex3f(-size_, -size_, 0); 	
        glEnd();
        break;
    case RED_EXPLODER:
    case BLUE_EXPLODER:
    case GREEN_EXPLODER:
    case ORANGE_EXPLODER:
    case PURPLE_EXPLODER:
        glEnable( GL_LIGHTING); 
        glutSolidSphere(size_, 6, 4);
        glDisable( GL_LIGHTING); 
        break;
    }
    
    //glDisable( GL_BLEND );
    
    glPopMatrix();
}