/*****************************************************************************
 *
 *  advancedmonitorwidgetcpp -- Show and control advanced monitoring data.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Gui4 module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *****************************************************************************/

#define	ADVANCED_MONITOR_WIDTH	480

#include <QString>

#include "advancedmonitorwidget.h"
#include "macros.h"
#include <QVBoxLayout>
#include <QLayoutItem>
#include <QDesktopWidget>
#include <QDebug>

AdvancedMonitorWidget::AdvancedMonitorWidget(QWidget *console, QWidget *parent =0)
	: QWidget(parent)
{
	setWindowTitle(tr("Advanced Monitor"));

	//setWindowFlags(Qt::CustomizeWindowHint);  // Turns off the default window title hints.
	//setWindowFlags(Qt::WindowTitleHint);      // Gives the window a title bar.

	resetAppearance(console);

	hide();  // to not be displayed before the main window
}

void AdvancedMonitorWidget::resetAppearance(QWidget *console)
{
#ifdef Q_OS_MAC
	if (console)
	{
		QDesktopWidget desktop;
		QRect dkrect = desktop.availableGeometry(desktop.screenNumber(console));
		resize(ADVANCED_MONITOR_WIDTH, qMin(int((double)dkrect.height() *0.8), 800));
	}
#else
	if (console)
	{
		QDesktopWidget desktop;
		QRect dkrect = desktop.availableGeometry(desktop.screenNumber(console));
		resize(ADVANCED_MONITOR_WIDTH, console->height());

		qDebug() << "Advanced Monitor: console& =" << console; 
		qDebug() << "Advanced Monitor: console size =" << console->size() \
			<< "console position =" << console->pos();
		qDebug() << "Console screen number :" << desktop.screenNumber(console);
		qDebug() << "Console screen available geometry:" << dkrect;

		int x1 = console->pos().x() + console->size().width() + 20;               // preferred: console right side
		int x2 = qMax(console->pos().x() - this->width() - 20, dkrect.x() + 10);  // or left side
		qDebug() << "x1:" << x1;
		qDebug() << "x2:" << x2;
		
		int x;
		if (console->pos().x() >= 0)
			x = x1 < dkrect.width() - this->width() ? x1 : x2;
		else
			x = x1 < - this->width() - 20 ? x1 : x2;
		move(x, console->pos().y());
	}
	else
	{
		resize(ADVANCED_MONITOR_WIDTH, 480);
	}
#endif

	qDebug() << "Advanced Monitor: size =" << this->size() << "position =" << this->pos();
}

void AdvancedMonitorWidget::addLog(const QString title, QTextEdit *attachedLog)
{
	attachedLog_t *newlog = new attachedLog_t;

	newlog->attached = attachedLog;

	newlog->log = new QTextEdit;
	newlog->log->setReadOnly(true);

	newlog->label = new QLabel(title);

	logs.append( *newlog );

	if ( this->layout() )
	{
		QLayout *layout = this->layout();
		layout->addWidget(newlog->label);
		layout->addWidget(newlog->log);
	}
	else
	{
		QVBoxLayout *layout = new QVBoxLayout;
		layout->addWidget(newlog->label);
		layout->addWidget(newlog->log);
		setLayout(layout);
	}

	connect(attachedLog, SIGNAL(textChanged()), this, SLOT(updateLog()));
}

void AdvancedMonitorWidget::updateLog()
{
	for (int i = 0; i < logs.count(); i++)
	{
		logs[i].log->setText( logs[i].attached->toPlainText() );
	}
#ifdef WIN32
#pragma message (ToBeDone "if we can know what log changed, just update it - " __FUNCTION__)
#pragma message (ToBeDone "scrolling down the logs would be great - " __FUNCTION__)
#endif
}

void AdvancedMonitorWidget::showLog(QTextEdit *log)
{
	for (int i = 0; i < logs.count(); i++)
	{
		if ( logs[i].attached == log )
		{
			logs[i].log->show();
			logs[i].label->show();
			break;
		}
	}

	// Show advanced monitor if at least one log is visible
	bool allHidden = true; // assuming
	for (int i = 0; i < logs.count(); i++)
	{
		if ( logs[i].log->isVisible() )
		{
			allHidden = false;
			break;
		}
	}
	if ( !allHidden )
	{
		this->show();
	}
}

void AdvancedMonitorWidget::hideLog(QTextEdit *log)
{
	for (int i = 0; i < logs.count(); i++)
	{
		if ( logs[i].attached == log )
		{
			logs[i].log->hide();
			logs[i].label->hide();
			break;
		}
	}

	// Hide advanced monitor if visible log yet
	bool allHidden = true; // assuming
	for (int i = 0; i < logs.count(); i++)
	{
		if ( logs[i].log->isVisible() )
		{
			allHidden = false;
			break;
		}
	}
	if ( allHidden )
	{
		this->hide();
	}
}

void AdvancedMonitorWidget::setLineWrapMode(QTextEdit *log, QTextEdit::LineWrapMode mode)
{
	for (int i = 0; i < logs.count(); i++)
	{
		if ( logs[i].attached == log )
		{
			logs[i].log->setLineWrapMode(mode);
			break;
		}
	}
}

void AdvancedMonitorWidget::reset()
{
	for (int i = 0; i < logs.count(); i++)
	{
		if (logs[i].label) delete (logs[i].label );
		if (logs[i].log) delete (logs[i].log );
	}

	logs.clear();
}

void AdvancedMonitorWidget::clearChannels()
{
	for (int i = 0; i < logs.count(); i++)
	{
		logs[i].log->clear();
		logs[i].attached->clear();
	}
}

void AdvancedMonitorWidget::hideEvent(QHideEvent *ev)
{
	QWidget::hideEvent(ev);

	emit hidden();
}

void AdvancedMonitorWidget::showEvent(QShowEvent *ev)
{
	QWidget::showEvent(ev);

	emit visible();
}
