#include <ctype.h>
#include <QPainter>
#include <QFile>
#include <QTimer>
#include <QKeyEvent>
#include <mediaplayer.h>


MediaPlayer::MediaPlayer(QWidget *parent) : QWidget(parent)
{
    bg = new QLabel(this);
    bg->setPixmap(QPixmap("./res/down_bg.lbm.png"));
    bg->move(0, 0);

    QLabel *softKey = new QLabel(this);
    softKey->setPixmap(QPixmap("./res/softykey.lbm.png"));
    softKey->move(0, 272-28);
    
    title = new QLabel(this);
    title->setText("MEDIA PLAYER in C++");
    title->move(10, 10);
    QPalette pala = title->palette();
    pala.setColor(QPalette::WindowText, Qt::green);
    title->setPalette(pala);
    
    selector = new QLabel(this);
    selector->setPixmap(QPixmap("./res/select_01.lbm.png"));
    selector->move(10, 50);

    usbMp3 = new QLabel(this);
    usbMp3->setPixmap(QPixmap("./res/mp3_small.lbm.png"));
    usbMp3->move(10, 50);

    usbMovie = new QLabel(this);
    usbMovie->setPixmap(QPixmap("./res/picture_small.lbm.png"));
    usbMovie->move(105, 50);

    sdMp3 = new QLabel(this);
    sdMp3->setPixmap(QPixmap("./res/mp3_small.lbm.png"));
    sdMp3->move(200, 50);

    sdMovie = new QLabel(this);
    sdMovie->setPixmap(QPixmap("./res/picture_small.lbm.png"));
    sdMovie->move(10, 140);

    bigIcon = new QLabel(this);
    bigIcon->setPixmap(QPixmap("./res/mp3_big.lbm.png"));
    bigIcon->move(320, 95);
    bigIconName = new QLabel(this);
    bigIconName->setText("USB MP3 BOX");
    bigIconName->setGeometry(320, 60, 128, 20);
    bigIconName->setAlignment(Qt::AlignCenter);
    QPalette pal = bigIconName->palette();
    pal.setColor(QPalette::WindowText, Qt::white);
    bigIconName->setPalette(pal);
    bigIconCount = new QLabel(this);
    bigIconCount->setText("0");
    bigIconCount->setAlignment(Qt::AlignCenter);
    bigIconCount->setGeometry(320, 210, 128, 20);
    
    selIndex = 0;
    mode = 0;
}


MediaPlayer::~MediaPlayer()
{
}


static void showPopupDialog(QString message)
{
    QLabel *popupImg = new QLabel(0); 
    QFontMetrics fm(popupImg->font());
    QImage img(480, 272, QImage::Format_RGB32);
    QPainter p(&img);
    p.drawPixmap(0, 0, QPixmap("images/depth_bg.jpg"));
    p.drawPixmap(0, 244, QPixmap("images/softykey.png"));
    p.drawImage((480-329)/2, (272-176)/2-12, QImage("images/big_popup_01.png"));
    p.drawText((480-fm.width(message))/2, 140, message);
    p.drawText(10, 10, "Popup Example in C++");
    p.end();
    popupImg->setPixmap(QPixmap::fromImage(img));
    popupImg->setGeometry(0,0,480,272);
    popupImg->show();
    QTimer::singleShot(2000, popupImg, SLOT(deleteLater()));
}


struct {
    int x;
    int y;
    const char *lab;
    const char *icon;
    int count;
} offsets[] = {
    { 10, 50, "USB MP3 BOX", "mp3", 0 },
    { 105, 50, "USB VIDEO BOX", "picture", 0 },
    { 200, 50, "SD MP3 BOX", "mp3", 0 },
    { 10, 140, "SD VIDEO BOX", "picture", 0 }
};


void MediaPlayer::keyPressEvent(QKeyEvent *ke)
{
    // Handle any other keys
    if ( ke->key() == Qt::Key_Context1 ) {         // PREV MENU
        return;
    } else if ( ke->key() == Qt::Key_Context2 || ke->key() == Qt::Key_Return ) {  // SELECT
        if ( mode == 0 ) {
            mode = 1;
            bg->setPixmap(QPixmap("./res/depth_bg_all.lbm.png"));
            title->setText(offsets[selIndex].lab);
            selector->hide();
            bigIcon->hide();
            bigIconName->hide();
            bigIconCount->hide();
            usbMp3->hide();
            usbMovie->hide();
            sdMp3->hide();
            sdMovie->hide();
        }
        return;
    } else if ( ke->key() == Qt::Key_Context3 ) {  // 
        return;
    } else if ( ke->key() == Qt::Key_Context4 ) {  // HOME 
        return;
    }

    switch (ke->key()) {
	    case Qt::Key_Left:
            selIndex--;
            if ( selIndex < 0 )
                selIndex = 0;
            break; // Left
	    case Qt::Key_Right:
            selIndex++;
            if ( selIndex > 3 )
                selIndex = 3;
            break; // Right
	    case Qt::Key_Up:
            if ( selIndex == 3 )
                selIndex = 0;
            break; // Up
	    case Qt::Key_Down:
            if ( selIndex == 0 )
                selIndex = 3;
            break; // Down
    }

    selector->move(offsets[selIndex].x, offsets[selIndex].y);
    bigIcon->setPixmap(QPixmap("./res/" + QString(offsets[selIndex].icon) + "_big.lbm.png"));
    bigIconName->setText(offsets[selIndex].lab);
    bigIconCount->setText(QString::number(offsets[selIndex].count));
}


void MediaPlayer::keyReleaseEvent(QKeyEvent *ke)
{
}

