// cOsdBasicDisplay.cpp: implementation of the cOsdBasicDisplay class.
//
//////////////////////////////////////////////////////////////////////
#include <vector>
#include <string>

using namespace std;

#include "osd.h"

#include "keys.h"
#include "defines.h"
#include "display.h"
#include "screen.h"
#include "text.h"

#include "cItemAction.h"
#include "cOsdBasicDisplay.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cOsdBasicDisplay::cOsdBasicDisplay()
{
	Clear();
}

cOsdBasicDisplay::~cOsdBasicDisplay()
{

}

/*virtual*/ void cOsdBasicDisplay::Clear(void)
{
	m_iNbText = 0;
	ymax = 0;
	yoffset = 0;

	m_lstPositionX.clear();
	m_lstPositionY.clear();
	m_lstText.clear();
}

/*virtual*/ void cOsdBasicDisplay::Draw(void)
{
    int txtwidth;
	int x,y,i, col = COL_RED;
    Scr::clear();

        /* Draw screen frame */

            // Horizontl
	for (x=0; x<Scr::width(); x++)
    {
		for (i=0; i<BORDERWIDTH; i++)
            Scr::pixel(x, 0+i, col);                //  Top lign

		for (i=0; i<BORDERWIDTH; i++)
            Scr::pixel(x, TITLE_HEIGHT+i, col);     // lign under title

        for (i=0; i<BORDERWIDTH; i++)
            Scr::pixel(x, Scr::height()-BORDERWIDTH+i, col);  // Bottom lign
	}

            // Vertical
	for (y=0; y<Scr::height(); y++)
    {
		for (i=0; i<BORDERWIDTH; i++)
            Scr::pixel(0+i, y, col);                //  Left lign

		for (i=0; i<BORDERWIDTH; i++)
            Scr::pixel(Scr::width()-BORDERWIDTH+i, y, col);                //  Right lign
    }

			/* standard text */
	for(i=0;i<m_iNbText;i++)
	{
		Txt::print(m_lstPositionX[i],m_lstPositionY[i]-yoffset,m_lstText[i].c_str(),COL_WHT);
	}

    txtwidth = Txt::width(m_title.c_str());
    Txt::print((Scr::width()-txtwidth) / 2,BORDERWIDTH,m_title.c_str(),COL_WHT);
    Scr::update();

}

/*virtual*/ cItemAction *cOsdBasicDisplay::ProcessKey(int Key)
{
	cItemAction *nextAction = NULL;

	switch(Key)
	{

		case kUp:	if(yoffset > 0)   yoffset-=10;	Draw();	break;
		case kDown: if(ymax-yoffset > 500)	yoffset+=10; Draw();		break;
		case kOk:   nextAction = &m_BackAction;     break;
		case kBack: nextAction = &m_BackAction;		break;
	}

	return(nextAction);
}

void cOsdBasicDisplay::SetTitle(string title)
{
	m_title = title;
}

void cOsdBasicDisplay::SetBackAction(int action /*=NO_ACTION*/)
{
	m_BackAction.m_Type = action;
}

void cOsdBasicDisplay::AddText(int x,int y,string title)
{
	if(y > ymax)
		ymax = y;

	m_lstPositionX.push_back(x);
	m_lstPositionY.push_back(y);
	m_lstText.push_back(title);

	m_iNbText ++ ;
}
