#include <cstdio>
#include <ogl2d.h>

#include "lumines.h"
#include "scanner.h"
#include "block.h"

void Scanner::init(Lumines *const _lumines, const ogl2d::Font *_font, int _pixwidth, int _pixheight)
{
    lumines = _lumines;
    font = _font;
    width = _pixwidth;
    height = _pixheight;
    scan_pos = 0;
    bScan = false;
    throughCount = 0;
    scan_speed = 1;
    last_pos = -1;
}
void Scanner::step()
{
    if (!bScan)
        return;

    scan_pos += scan_speed;
    if (scan_pos >= getScanLength())
    {
        scan_pos = 0;
        lumines->reportDeleted(throughCount);
        throughCount = 0;
        // bScan = false;
    }
    int pos = int(scan_pos)/(LUMINES_BLOCK_SIZE + 1);
    if (pos != last_pos)
    {
        last_pos = pos;
        assert(lumines);
        if (cDirection == LeftToRight)
            throughCount += lumines->scan_x(pos);
        else // if (cDirection == TopToBottom)
            throughCount += lumines->scan_y(pos);
    }
}
void Scanner::setScanSpeed(float speed)
{
    scan_speed = getScanLength() / (speed*60);
}
void Scanner::scan()
{
    bScan = true;
}
void Scanner::render()
{
    glEnable(GL_LINE_SMOOTH);
    glDisable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA);
    glPushMatrix();
    glColor3f(1,.8f,0);
    char str[32];
    sprintf(str,"%d",throughCount);
    if (cDirection == LeftToRight)
    {
        glTranslatef(float(scan_pos - 8 *strlen(str)), -5, 0);
        glScalef(20,-10,1);
        font->textOut(str);
        glPopMatrix();
        glColor4f(1,.5f,0,.4f);
        glLineWidth(3.f);
        glBegin(GL_LINES);
        glVertex3d(scan_pos,0,0.9f);
        glVertex3d(scan_pos,height+1,0.9f);
        glEnd();
        glColor4f(1,1,.9f,1);
        glLineWidth(.5f);
        glBegin(GL_LINES);
        glVertex3d(scan_pos,0,0.9f);
        glVertex3d(scan_pos,height+1,0.9f);
        glEnd();

        const int lightwidth = 50;
        float left = max(-1, scan_pos - lightwidth);
        float right = scan_pos - lightwidth + width;
        if (right >= width) right -= width;
        float scale = 1.f - (scan_pos - left)/(float)lightwidth;

        float color[4]={1,.8f,0,0.3f};
        glBegin(GL_QUADS);
        glColor4f(color[0],color[1],color[2],color[3]);
        glVertex2d(scan_pos, 0);
        glVertex2d(scan_pos, height+1);
        glColor4f(color[0]*scale,color[1]*scale,color[2]*scale,color[3]*scale);
        glVertex2d(left, height+1);
        glVertex2d(left, 0);
        if (right > left)
        {
            glColor4d(0,0,0,0);
            glVertex2d(right, 0);
            glVertex2d(right, height+1);
            glColor4f(color[0]*scale,color[1]*scale,color[2]*scale,color[3]*scale);
            glVertex2d(width, height+1);
            glVertex2d(width, 0);
        }
        glEnd();
    }
    else
    {
        glTranslatef(-5, float(scan_pos - 8 *strlen(str)), 0);
        glScalef(20,-10,1);
        font->textOut(str);
        glPopMatrix();
        glColor4f(1,.5f,0,.4f);
        glLineWidth(3.f);
        glBegin(GL_LINES);
        glVertex3d(0,scan_pos, 0.9f);
        glVertex3d(width+1,scan_pos, 0.9f);
        glEnd();
        glColor4f(1,1,.9f,1);
        glLineWidth(.5f);
        glBegin(GL_LINES);
        glVertex3d(0,scan_pos,0.9f);
        glVertex3d(width+1,scan_pos,0.9f);
        glEnd();

        const int lightheight = 50;
        float top = max(-1, scan_pos - lightheight);
        float bottom = scan_pos - lightheight + height;
        if (bottom >= height) bottom -= height;
        float scale = 1.f - (scan_pos - top)/(float)lightheight;

        float color[4]={1,.8f,0,0.3f};
        glBegin(GL_QUADS);
        glColor4f(color[0],color[1],color[2],color[3]);
        glVertex2d(0, scan_pos);
        glVertex2d(width+1, scan_pos);
        glColor4f(color[0]*scale,color[1]*scale,color[2]*scale,color[3]*scale);
        glVertex2d(width+1,top);
        glVertex2d(0, top);
        if (bottom > top)
        {
            glColor4d(0,0,0,0);
            glVertex2d(0, bottom);
            glVertex2d(width+1, bottom);
            glColor4f(color[0]*scale,color[1]*scale,color[2]*scale,color[3]*scale);
            glVertex2d(width+1, height);
            glVertex2d(0, height);
        }
        glEnd();
    }

    glDisable(GL_BLEND);
    glDisable(GL_LINE_SMOOTH);
}
