// cOsdListReportMenu.cpp: implementation of the cOsdListReportMenu class.
//
//////////////////////////////////////////////////////////////////////

#include <vector>
#include <string>

using namespace std;

#include "osd.h"

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

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


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

cOsdListReportMenu::cOsdListReportMenu()
{
	m_ColumnHead = new string[NBMAXCOL];
	m_Colstyle = new int[NBMAXCOL];
	m_ColWitdh = new int[NBMAXCOL];


	m_ListReport = new string[NBMAXCOL*NBMAXLIG];
	m_Action  = new cItemAction[NBMAXLIG];

	m_NbRaw = m_NbColumn = 0;
	m_current = 0;
}

cOsdListReportMenu::~cOsdListReportMenu()
{
	delete [] m_ColumnHead;
	delete m_Colstyle;
	delete m_ColWitdh;
	delete [] m_ListReport;
    delete [] m_Action;
}

void cOsdListReportMenu::InsertColumn(int nCol,string ColumnHeading,int align /*=ALIGN_LEFT*/,int nWidth /*=-1*/)
{

}

void cOsdListReportMenu::AddColumn(string ColumnHeading,int align /*=ALIGN_LEFT*/,int nWidth /*=-1*/)
{
	m_ColumnHead[m_NbColumn] = ColumnHeading;
	m_Colstyle[m_NbColumn] = align;
	m_ColWitdh[m_NbColumn] = nWidth;

	m_NbColumn++;
}


int cOsdListReportMenu::AddRaw(string Rawdata,int action /*=NO_ACTION*/,int *param /*=0*/)
{
	char txt[10];

	sprintf(txt,"%d ",m_NbRaw+1);
	m_ListReport[ m_NbRaw * NBMAXCOL ] = txt;
	m_ListReport[ m_NbRaw * NBMAXCOL ] += Rawdata;

	m_Action[ m_NbRaw ].m_Type = action;
	m_Action[ m_NbRaw ].pParam = param;


	m_NbRaw ++ ;
	if(m_NbRaw >= NBMAXLIG)
		m_NbRaw = NBMAXLIG-1;

	return(m_NbRaw-1);
}


//cOsdListReportMenu::InsertItem(int nCol,int nRaw,string &name)
//{
//	m_ListReport[ (m_NbRaw * NBMAXCOL) + nCol ] = name;
//}


cItemAction * cOsdListReportMenu::ProcessKey(int Key)
{
	cItemAction *nextAction = NULL;

	switch(Key)
	{
        case k0:    KeyNum(0);  break;
        case k1:    KeyNum(1);  break;
        case k2:    KeyNum(2);  break;
        case k3:    KeyNum(3);  break;
        case k4:    KeyNum(4);  break;
        case k5:    KeyNum(5);  break;
        case k6:    KeyNum(6);  break;
        case k7:    KeyNum(7);  break;
        case k8:    KeyNum(8);  break;
        case k9:    KeyNum(9);  break;

		case kUp:
			if(m_current > 0) m_current--;
            Draw();
			break;
		case kDown:
			if(m_current+1 < m_NbRaw) m_current++;
            Draw();
			break;

		case kOk:   nextAction = &m_Action[m_current];   break;

		case kBack: nextAction = &m_BackAction;			break;
	}

	return(nextAction);
}

void cOsdListReportMenu::Draw(void)
{
	int i,j, col = COL_RED;
    int bkcol,fgcol;
    int txtwidth,x,y;

    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, TITLE_HEIGHT+BORDERWIDTH+COLUMTITLE_HEIGHT+i, col);// lign under colum header

        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
    }

    x = BORDERWIDTH;
    for(j=0;j<m_NbColumn;j++)
    {
        x += m_ColWitdh[j];
	    for (int y=TITLE_HEIGHT+BORDERWIDTH; y<Scr::height()-BORDERWIDTH; y++)
        {
		    for (i=0; i<BORDERWIDTH; i++)
                Scr::pixel(x+i, y, col);                //  lign
        }
    }

        /* Draw colum head */
    x = BORDERWIDTH;
    for(j=0;j<m_NbColumn;j++)
    {
        Txt::print(x,TITLE_HEIGHT+BORDERWIDTH,m_ColumnHead[j].c_str(),COL_WHT);
        x += m_ColWitdh[j];
		x += BORDERWIDTH;
    }

        /* Draw item */
    y = BORDERWIDTH+TITLE_HEIGHT+BORDERWIDTH+COLUMTITLE_HEIGHT+BORDERWIDTH;
    for(i=0;i<m_NbRaw;i++)
    {
        if(i == m_current )
        {
            fgcol = COL_WHT;bkcol = COL_BLU;
        }
        else
        {
            fgcol = COL_CYN;bkcol = COL_BGR;
        }

        x = BORDERWIDTH;
        for(j=0;j<m_NbColumn;j++)        // m_NbColumn
        {
            Txt::printWidth(x,m_ColWitdh[j],y,m_ListReport[ ( i * NBMAXCOL ) + j  ].c_str(),fgcol,bkcol);
            x += m_ColWitdh[j];
			x += BORDERWIDTH;
        }
        y += RAW_HEIGHT;
    }

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

/*
void cOsdListReportMenu:frame_draw(void)
{
	int i, x1, x2, y1, y2, y3, col = COL_RED;

	x1 = y1 = 0;
	x2 = W + BORDERWIDTH;
	y2 = BORDERWIDTH + STATUSHEIGHT;
	y3 = H + 2*BORDERWIDTH + STATUSHEIGHT;

	for (int x=0; x<Scr::width(); x++)
    {
		for (i=0; i<BORDERWIDTH; i++) Scr::pixel(x, y1+i, col);
		for (i=0; i<BORDERWIDTH; i++) Scr::pixel(x, y2+i, col);
		for (i=0; i<BORDERWIDTH; i++) Scr::pixel(x, y3+i, col);
	}

	for (int y=0; y<Scr::height(); y++)
    {
		for (i=0; i<BORDERWIDTH; i++) Scr::pixel(x1+i, y, col);
		for (i=0; i<BORDERWIDTH; i++) Scr::pixel(x2+i, y, col);
	}
}
*/

void cOsdListReportMenu::Clear()
{
	m_NbRaw = m_NbColumn = 0;
	m_current = 0;

	cOsdBasicDisplay::Clear();
}

int cOsdListReportMenu::KeyNum(int k)
{
	m_current = k-1;
    Draw();

    return(0);
}

void cOsdListReportMenu::InsertItem(int nRaw,int nCol,string data)
{
	m_ListReport[ nCol + ( nRaw * NBMAXCOL) ] = data;
}

