/*
 *  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 "FileFolderWidget.h"

#include "crd_ut10xx.h"
#include "FileDialogWPR.h"
#include "MainWindow.h"

FileFolderWidget::FileFolderWidget(QWidget *parent): QWidget(parent)
{
	QGridLayout *mainLayout = new QGridLayout;
	QGridLayout *layout;
	QGroupBox *gBox;
	
	// General file system informations.
	gBox = new QGroupBox(tr("Filesystem information"));
	layout = new QGridLayout;
	mFileSystemInfo = new QLabel;
	layout->addWidget(mFileSystemInfo, 0, 0);
	gBox->setLayout(layout);
	
	mainLayout->addWidget(gBox, 0, 0);
	
	// File list and options.
	gBox = new QGroupBox(tr("Files"));
	layout = new QGridLayout;
	
	QStringList fileTreeHeaderLabels;
	
	fileTreeHeaderLabels << tr("Filename");
	fileTreeHeaderLabels << tr("Size [bytes]");
	fileTreeHeaderLabels << tr("");
	
	mFileTree = new QTreeWidget;
	mFileTree->setHeaderLabels(fileTreeHeaderLabels);
	mFileTree->setColumnWidth(0, 250);
	mFileTree->setColumnWidth(1, 150);
	
	layout->addWidget(mFileTree, 0, 0, 1, 2);
	
	mAddFile = new QPushButton(QIcon(QString(":img/add.png")), tr("Add file"));
	mDelFile = new QPushButton(QIcon(QString(":img/cancel.png")), tr("Delete file"));
	mSaveFile = new QPushButton(QIcon(QString(":img/filecmd_saveas.png")), tr("Save as"));
	
	connect(mAddFile, SIGNAL(clicked()), this, SLOT(addFileSlot()));
	connect(mDelFile, SIGNAL(clicked()), this, SLOT(deleteFileSlot()));
	connect(mSaveFile, SIGNAL(clicked()), this, SLOT(saveFileSlot()));
	
	QGridLayout *buttonsLayout = new QGridLayout;
	buttonsLayout->addWidget(mAddFile, 0, 0);
	buttonsLayout->addWidget(mDelFile, 0, 1);
	buttonsLayout->addWidget(mSaveFile, 0, 3);
	buttonsLayout->setColumnStretch(2, 1);

	layout->addLayout(buttonsLayout, 2, 0);

	layout->setRowStretch(0, 1);
	
	gBox->setLayout(layout);
	
	mainLayout->addWidget(gBox, 1, 0);
	mainLayout->setRowStretch(1, 1);
	
	setLayout(mainLayout);
	
	// Disable options which need login.
	disable();
}

void
FileFolderWidget::insertFileWidgetItem(QTreeWidgetItem *item)
{
	mFileTree->insertTopLevelItem(0, item);
}

void
FileFolderWidget::removeFileWidgetItem(QTreeWidgetItem *item)
{
	mFileTree->takeTopLevelItem(mFileTree->indexOfTopLevelItem(item));
}

void
FileFolderWidget::sortFilesByName()
{
	mFileTree->sortItems(0, Qt::AscendingOrder);
}

void
FileFolderWidget::setInfo(CRD_UT10XX_t *ut)
{
	int noFiles = 0;
	int maxNoFiles = ut->files_no;
	int freeMemory = 0;
	int totalMemory = ut->info.public_mem;
	
	for (int i = 0; i < maxNoFiles; i++)
	{
		if (ut->file_types[i] != CRD_UT10XX_FT_EMPTY)
			noFiles++;
	}
	
	for (int i = 0; i < (int)sizeof(ut->free_sectors) / (int)sizeof(ut->free_sectors[0]); i++)
	{
		if (ut->free_sectors[i])
			freeMemory += 128;
	}
	
	mFileSystemInfo->setText(tr(
		"Number of files saved: <b>%1</b><br>"
		"Max number of files: <b>%2</b><br>"
		"Free memory (bytes): <b>%3</b><br>"
		"Total memory (bytes): <b>%4</b><br>"
		"Max file size (bytes): <b>%5</b>").
		arg(noFiles).
		arg(maxNoFiles).
		arg(freeMemory).
		arg(totalMemory).
		arg(4096)
	);
}

void
FileFolderWidget::enable()
{
	mAddFile->setEnabled(true);
	mDelFile->setEnabled(true);
}

void
FileFolderWidget::disable()
{
	mAddFile->setEnabled(false);
	mDelFile->setEnabled(false);
}

void
FileFolderWidget::addFileSlot()
{
	QString srcFileName;
	QString dstFileName;
	
	srcFileName = FileDialogWPR::getOpenFileName(this, tr("Import"));
	dstFileName = srcFileName.section('/', -1);
	
	if (srcFileName.isEmpty())
		return;
	
	emit addFileSig(srcFileName, dstFileName);
}

void
FileFolderWidget::deleteFileSlot()
{
	QString fileName;
	
	if (mFileTree->currentItem() == 0)
	{
		QMessageBox::information(MainWindow::mainWnd, tr("Information"), tr("Select file to delete."));
		return;
	}
	
	fileName = mFileTree->currentItem()->text(0);
	
	if (fileName.isEmpty())
		return;
	
	emit deleteFileSig(fileName);
}

void
FileFolderWidget::saveFileSlot()
{
	QString srcFileName;
	QString dstFileName;
	
	if (mFileTree->currentItem() == 0)
	{
		QMessageBox::information(MainWindow::mainWnd, tr("Information"), tr("Select file to save."));
		return;
	}
	
	srcFileName = QDir::homePath() + QDir::separator() + mFileTree->currentItem()->text(0);
	dstFileName = FileDialogWPR::getSaveFileName(this, tr("Save as"), srcFileName);
	
	if (dstFileName.isEmpty())
		return;
	
	emit saveFileSig(srcFileName, dstFileName);
}
