/*
 * scbus utils to switch card settings.
 *
 * Copyright (C) 2011 Otvos Attila oattila@onebithq.com
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <stdio.h>

#include "devicedialog.h"
#include "ledstatus.h"

DeviceTab::DeviceTab(const int deviceid, QList<int>* devices, QWidget *parent) : QWidget(parent) {
    char sdeviceid[16];
    char tmp[16];

    snprintf(sdeviceid,sizeof(sdeviceid),"%02X",deviceid);
    QLabel *deviceLabel = new QLabel(tr("Device:"));
    if(deviceid)
        deviceValueLabel = new QLabel(QString(sdeviceid));
    else
        deviceValueLabel = new QLabel(QString("Not selected"));
    deviceValueLabel->setFrameStyle(QFrame::Panel | QFrame::Sunken);

    QLabel *newDeviceLabel = new QLabel(tr("New device id:"));

    newDeviceID = new QComboBox();

    this->deviceid=deviceid;
    this->devices=devices;
    if(deviceid) {
        for(int i=1;i<0x3f;i++) {
            if(devices->contains(i) && i!=deviceid)
                continue;
            snprintf(tmp,sizeof(tmp),"%02X",i);
            newDeviceID->addItem(tmp,i);
        }
        newDeviceID->addItem("FF",255);
        newDeviceID->setEnabled(true);
    } else {
        newDeviceID->setEnabled(false);
    }


    QVBoxLayout *mainLayout = new QVBoxLayout;

    mainLayout->addWidget(deviceLabel);
    mainLayout->addWidget(deviceValueLabel);
    mainLayout->addWidget(newDeviceLabel);
    mainLayout->addWidget(newDeviceID);

    QLabel *dummyLabel = new QLabel("");
    dummyLabel->setAlignment(Qt::AlignBottom);
    dummyLabel->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    mainLayout->addWidget(dummyLabel);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    acceptButton = new QPushButton("Accept");
    resetButton = new QPushButton("Reset");
    buttonLayout->addWidget(resetButton);
    buttonLayout->addWidget(acceptButton);
    QWidget * buttonWidget = new QWidget();
    buttonWidget->setLayout(buttonLayout);

    mainLayout->addWidget(buttonWidget);

//    mainLayout->addStretch(1);

    connect(newDeviceID,SIGNAL(currentIndexChanged(int)),this,SLOT(indexChanged(int)));
    connect(resetButton,SIGNAL(pressed()),this,SLOT(pressReset()));
    connect(acceptButton,SIGNAL(pressed()),this,SLOT(pressAccept()));
    setLayout(mainLayout);
}

void DeviceTab::indexChanged(int idx) {
    int d;

    d = newDeviceID->itemText(idx).toInt();
    if(d==deviceid) {
        acceptButton->setEnabled(false);
        resetButton->setEnabled(false);
        emit setModify(false);
    } else {
        acceptButton->setEnabled(true);
        resetButton->setEnabled(true);
        emit setModify(true);
    }
}

void DeviceTab::updateDevices(void) {
    char tmp[16];

    newDeviceID->clear();
    if(!deviceid) {
        acceptButton->setEnabled(false);
        resetButton->setEnabled(false);
        newDeviceID->setEnabled(false);
        emit setModify(false);
        return;
    }
    newDeviceID->setEnabled(true);
    for(int i=1;i<0x3f;i++) {
        if(devices->contains(i) && i!=deviceid)
            continue;
        snprintf(tmp,sizeof(tmp),"%02X",i);
        newDeviceID->addItem(tmp,i);
    }
    newDeviceID->addItem("FF",255);
}

void DeviceTab::pressReset(void) {
    int i;

    for(i=0;i<newDeviceID->count();i++) {
        if(newDeviceID->itemData(i).toInt()==deviceid) {
            newDeviceID->setCurrentIndex(i);
            return;
        }
    }
}

void DeviceTab::pressAccept(void) {
    int d;

    d = newDeviceID->itemData(newDeviceID->currentIndex()).toInt();
    if(d!=deviceid)
        emit accept(d);
}

void DeviceTab::setDevice(int deviceid) {
    char sdeviceid[16];

    this->deviceid=deviceid;
    snprintf(sdeviceid,sizeof(sdeviceid),"%02X",deviceid);
    if(deviceid) {
        deviceValueLabel->setText(QString(sdeviceid));
        newDeviceID->setEnabled(true);
    } else {
        deviceValueLabel->setText(QString("Not selected"));
        newDeviceID->clear();
        newDeviceID->setEnabled(false);
        return;
    }
    updateDevices();
    for(int i=0;i<newDeviceID->count();i++) {
        if(newDeviceID->itemData(i).toInt()==deviceid) {
            newDeviceID->setCurrentIndex(i);
            return;
        }
    }
}

int DeviceTab::getDevice(void) {
    return deviceid;
}


