/*
 *  Copyright (C) 2007-2008 CMM Sigma Andrzej Chmielowiec <cmmsigma@cmmsigma.eu>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3
 *  as published by the Free Software Foundation.
 *
 *  This program 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.
 */

#include "KeyFolderWidget.h"
#include "MainWindow.h"

#include "crd_ut10xx.h"

KeyFolderOptionsWidget::KeyFolderOptionsWidget(int id, QWidget *parent): QGroupBox(parent)
{
	mId = id;
	mGenerated = false;
	
	setTitle(tr("Key #%1").arg(mId + 1));
	
	QGridLayout *layout = new QGridLayout;
	mNameLineEdit = new QLineEdit;
	mNameLineEdit->setMaxLength(22);
	mGenButton = new QPushButton(QIcon(QString(":img/random.png")), tr("Generate"));
	mDelButton = new QPushButton(QIcon(QString(":img/cancel.png")), tr("Delete"));
	layout->addWidget(new QLabel(tr("Name:")), 0, 0);
	layout->addWidget(mNameLineEdit, 0, 1);
	layout->addWidget(mGenButton, 0, 2);
	layout->addWidget(mDelButton, 1, 2);
	layout->setColumnStretch(1, 1);
	setLayout(layout);
	setEnabled(false);
	
	connect(mGenButton, SIGNAL(clicked()), this, SLOT(generateKeySlot()));
	connect(mDelButton, SIGNAL(clicked()), this, SLOT(deleteKeySlot()));
}

void
KeyFolderOptionsWidget::setInfo(CRD_UT10XX_key_info_t *key)
{
	if (key->info & 0x01)
	{
		mGenerated = true;
		mNameLineEdit->setText(trUtf8(key->name));
	}
	else
	{
		mGenerated = false;
		mNameLineEdit->setText(tr(""));
	}
	
	if (isEnabled())
		enable();
}

void
KeyFolderOptionsWidget::enable()
{
	setEnabled(true);
	
	mNameLineEdit->setEnabled(!mGenerated);
	mGenButton->setEnabled(!mGenerated);
	mDelButton->setEnabled(mGenerated);
}

void
KeyFolderOptionsWidget::disable()
{
	setEnabled(false);
}

void
KeyFolderOptionsWidget::generateKeySlot()
{
	mName = (mNameLineEdit->text()).toUtf8();
	emit generateKeySig(mName, mId);
}

void
KeyFolderOptionsWidget::deleteKeySlot()
{
	int button = QMessageBox::question(MainWindow::mainWnd, tr("Question"), tr("Do you want to delete key?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
	
	if (button == QMessageBox::Yes)
		emit deleteKeySig(mId);
}


KeyFolderWidget::KeyFolderWidget(QWidget *parent): QWidget(parent)
{
	mLayout = new QGridLayout;
	
	for (int i = 0; i < 4; i++)
	{
		mKeyWidget[i] = new KeyFolderOptionsWidget(i, this);
		mLayout->addWidget(mKeyWidget[i], i, 0);
		connect(mKeyWidget[i], SIGNAL(generateKeySig(const char*, int)), 
			this, SLOT(generateKeySlot(const char*, int)));
		connect(mKeyWidget[i], SIGNAL(deleteKeySig(int)), this, 
			SLOT(deleteKeySlot(int)));
	}
	
	mLayout->setRowStretch(5, 1);
	setLayout(mLayout);
}

void
KeyFolderWidget::setInfo(CRD_UT10XX_key_info_t *keyList)
{
	for (int i = 0; i < 4; i++)
		mKeyWidget[i]->setInfo(keyList + i);
}

void
KeyFolderWidget::enable()
{
	for (int i = 0; i < 4; i++)
		mKeyWidget[i]->enable();
}

void
KeyFolderWidget::disable()
{
	for (int i = 0; i < 4; i++)
		mKeyWidget[i]->disable();
}

void
KeyFolderWidget::generateKeySlot(const char *name, int id)
{
	emit generateKeySig(name, id);
}

void
KeyFolderWidget::deleteKeySlot(int id)
{
	emit deleteKeySig(id);
}
