#include "menu.h"
#include "tinerxml.h"
#include "glex.h"
#include "glness.h"

#include <iostream>
using namespace std;

void menu::init()
{
    xmlfile menu;
    TiXmlElement *parent = menu.getxmlfirstelement("menu.xml");
        TiXmlElement *mnulist = menu.getelement(parent, "menulist");
        TiXmlElement *mnuitm = menu.getelement(mnulist, "mnuitem");
        int i = 0;

        string texname;
        while( mnuitm )
        {
            i++;
            tmenuitem.pos.x = 200;
            tmenuitem.pos.y = 200*i;
            tmenuitem.scale = 128;
            texname = "img/";
            texname += mnuitm->Attribute("img");
            tmenuitem.texid = LoadGLTexture(texname.c_str());
            tmenuitem.name = mnuitm->Attribute("name");

            menuitem.push_back(tmenuitem);
            //cout << texname << endl;
            mnuitm = mnuitm->NextSiblingElement();
        }


        TiXmlElement *xLogo = menu.getelement(parent, "logo");
        texname = "img/";
        texname += xLogo->Attribute("default");
        logo[0] = LoadGLTexture(texname.c_str());
        texname = "img/";
        texname += xLogo->Attribute("p1win");
        logo[1] = LoadGLTexture(texname.c_str());
        texname = "img/";
        texname += xLogo->Attribute("p2win");
        logo[2] = LoadGLTexture(texname.c_str());

        clogo = 0;


        //w = (int)atoi(xGraphics->Attribute("w"));
    menu.endxml();


    currentmenuitem = menuitem.begin();

}

void menu::nextmenuitem()
{
    currentmenuitem++;
    if(currentmenuitem == menuitem.end())
        currentmenuitem = menuitem.begin();
}
void menu::prevmenuitem()
{
    if(currentmenuitem == menuitem.begin())
        currentmenuitem = menuitem.end();
    currentmenuitem--;
}


string menu::cstring()
{
    return (*currentmenuitem).name;
}

void menu::draw()
{
    glDisable(GL_DEPTH_TEST);      // Enable Depth Testing
    glLoadIdentity();
    float drawscale = 0.05f;
    glColor3f(1.f,1.f,1.f);
    glScalef(drawscale, drawscale, drawscale);
    glTranslatef(-200,300,-1300);
    glScalef(-1.f,-1.f,1.f);

    //int counter = -1;
    list<menuitemT>::iterator i;
    for(i=menuitem.begin();i!=menuitem.end();i++)
    {
        //counter++;
        if(i == currentmenuitem)
        {
            glColor4f(1.f,1.f,1.f, 1.f);
        }
        else
        {
            glColor4f(1.f,1.f,1.f, 0.5f);
        }
        glBindTexture( GL_TEXTURE_2D, (*i).texid);
        drawquad(-(*i).pos.x, (*i).pos.y, 300, (*i).scale);
        //cout << (*i).name << " " << (*i).texid << endl;
    }
	glBindTexture( GL_TEXTURE_2D, logo[clogo]);
	glColor4f(1.f,1.f,1.f, 1.f);
	drawquad(200,0,0,128);
    glEnable(GL_DEPTH_TEST);      // Enable Depth Testing
}
