/**************************************************************************E
 *   Copyright (C) 2007 by Vincenzo Forchi`,,,   *
 *   vincenzo.forchi@gmail.com   *
 *                                                                         *
 *   This program 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 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.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/


#include "preinjinterlock.h"
#include <qtango.h>
#include <event.h>
#include <QString>
#include <QTimer>

#include <QtDebug>

#define GUN	0
#define MOD1	1
#define MOD2	2
#define	PS	3

PreInjInterlock::PreInjInterlock(QWidget *parent) : QWidget(parent)
{
	ui.setupUi(this);
	
	QList<Ui::Mod*> modUis;
	modUis << (&uiMod1) << (&uiAlmMod1) << (&uiMod2) << (&uiAlmMod2);
	QList<QWidget*> widgets;
	widgets << ui.stackedMod1->widget(0);
	widgets << ui.stackedMod1->widget(1);
	widgets << ui.stackedMod2->widget(0);
	widgets << ui.stackedMod2->widget(1);

	for (int i = 0; i < widgets.size(); i++)
		modUis[i]->setupUi(widgets[i]);
	
//	ui.stackedMod1->setStyleSheet("background-image: url(:/src/mod.png)");
//	ui.stackedMod2->setStyleSheet("background-image: url(:/src/mod.png)");

//	foreach (QWidget *w, widgets)
//		w->setStyleSheet("QWidget { background-image: url(:/src/mod.png) }");

	QList<Ui::Gun*> gunUis;
	gunUis << (&uiGun) << (&uiAlmGun);

	uiGun.setupUi(ui.stackedGun->widget(0));
	uiAlmGun.setupUi(ui.stackedGun->widget(1));
//	ui.stackedGun->widget(0)->setStyleSheet("background-image: url(:/src/gun.png)");
//	ui.stackedGun->widget(1)->setStyleSheet("background-image: url(:/src/gun.png)");

	uiFoc.setupUi(ui.tabWidget->widget(PS));
//	ui.tabWidget->widget(PS)->setStyleSheet("background-image: url(:/src/foc.png)");

	ui.tabWidget->widget(GUN)->layout()->setMargin(0);
	ui.tabWidget->widget(MOD1)->layout()->setMargin(0);
	ui.tabWidget->widget(MOD2)->layout()->setMargin(0);
	//ui.tabWidget->widget(PS)->layout()->setMargin(0);

	QVector<ELabel*> cells;
	foreach (Ui::Mod *uiMod, modUis)
	{
		cells.clear();
		cells << uiMod->tHvEnable->cells[0] << uiMod->tHvEnable->cells[2] << uiMod->tHvEnable->cells[4] << uiMod->tKlystronEnable->cells[0];
		cells << uiMod->tThyratronTriggerEnable->cells[0] << uiMod->tThyratronTriggerEnable->cells[9] << uiMod->tPremagEnable->cells[0];
		cells << uiMod->tRfTrigger->cells[0] << uiMod->tLoad->cells[0] << uiMod->tSections->cells[0];
		foreach (ELabel *cell, cells)
		{
			cell->setFrameShape(QFrame::Panel);
			cell->setFrameShadow(QFrame::Plain);
			cell->setLineWidth(3);
		}
	}
	foreach (Ui::Gun *uiGun, gunUis)
	{
		cells.clear();
		cells << uiGun->tFilAbi->cells[0] << uiGun->tFilCurr->cells[0] << uiGun->tFilReady->cells[0] << uiGun->tGunHv->cells[0];
		foreach (ELabel *cell, cells)
		{
			cell->setFrameShape(QFrame::Panel);
			cell->setFrameShadow(QFrame::Plain);
			cell->setLineWidth(3);
		}
	}

	setMaximumSize(size());
	ui.tabWidget->setIconSize(QSize(16,16));

	QTimer::singleShot(100, this, SLOT(connectToField()));
}

PreInjInterlock::~PreInjInterlock()
{
//	client_leavefunc();
}

void PreInjInterlock::connectToField()
{
	connectGun();
	connectMod1();
	connectMod2();
	connectFoc();

	/*QTimer *timer = new QTimer(this);
	connect(timer, SIGNAL(timeout()), this, SLOT(updateAlarms()));
	timer->start(1000);*/
}

void PreInjInterlock::connectGun()
{
	uiAlmGun.label->setText("GUN - MEMO ALLARMI");
	uiAlmGun.tView->setText("View Real Time");
	uiAlmGun.tAcknowledge->setVisible(true);
	uiAlmGun.tAcknowledge->setTargets(devGun + "->Acknowledge_all");

	uiAlmGun.tGunSafety->setSource(devGun + "/almgunsafety", POLLED_REFRESH,1500);
	uiAlmGun.tFilAbi->setSource(devGun + "/almfilabi", POLLED_REFRESH,1500);
	uiAlmGun.tFilCurr->setSource(devGun + "/almfilcurr", POLLED_REFRESH,1500);
	uiAlmGun.tFilReady->setSource(devGun + "/almfilready", POLLED_REFRESH,1500);
	uiAlmGun.tGunHv->setSource(devGun + "/almgunhv", POLLED_REFRESH,1500);
	uiAlmGun.tAbiTrigGun->setSource(devGun + "/almabitriggun", POLLED_REFRESH,1500);
	uiAlmGun.tCurrFilVal->hide();
	uiAlmGun.tGridVal->hide();
	uiAlmGun.tTimer->hide();

	uiGun.label->setText("GUN - REAL TIME");
	uiGun.tView->setText("View Alarms");
	uiGun.tAcknowledge->setVisible(false);
	
	uiGun.tGunSafety->setSource(devGun + "/gunsafety", POLLED_REFRESH,1500);
	uiGun.tFilAbi->setSource(devGun + "/filabi", POLLED_REFRESH,1500);
	uiGun.tFilCurr->setSource(devGun + "/filcurr", POLLED_REFRESH,1500);
	uiGun.tFilReady->setSource(devGun + "/filready", POLLED_REFRESH,1500);
	uiGun.tGunHv->setSource(devGun + "/gunhv", POLLED_REFRESH,1500);
	uiGun.tAbiTrigGun->setSource(devGun + "/abitriggun", POLLED_REFRESH,1500);
	uiGun.tCurrFilVal->setNumMajorTicks(7);
	uiGun.tCurrFilVal->setSource(devGun + "/currfilval", POLLED_REFRESH,1500);
	uiGun.tGridVal->setSource(devGun + "/gridval", POLLED_REFRESH,1500);
	uiGun.tTimer->setSource(devGun + "/realfiltimer", POLLED_REFRESH,1500);

	connect(uiGun.tView, SIGNAL(clicked()), this, SLOT(showAlmGun()));
	connect(uiAlmGun.tView, SIGNAL(clicked()), this, SLOT(showGun()));
}

void PreInjInterlock::connectMod1()
{
	uiAlmMod1.label->setText("MODULATORE1 - MEMO ALLARMI");
	uiAlmMod1.tView->setText("View Real Time");
	uiAlmMod1.tAcknowledge->setVisible(true);
	uiAlmMod1.tAcknowledge->setTargets(devMod1 + "->Acknowledge_all");

	uiAlmMod1.tModsafety->setSource(devMod1 + "/AlmModSafety", POLLED_REFRESH,1500);
	uiAlmMod1.tThyratron->setSource(devMod1 + "/AlmThyratron", POLLED_REFRESH,1500);
	uiAlmMod1.tKlystron->setSource(devMod1 + "/AlmKlystron", POLLED_REFRESH,1500);
	uiAlmMod1.tHvEnable->setSource(devMod1 + "/AlmHvEnable", POLLED_REFRESH,1500);
	uiAlmMod1.tKlystronEnable->setSource(devMod1 + "/AlmKlystronEnable", POLLED_REFRESH,1500);
	uiAlmMod1.tThyratronTriggerEnable->setSource(devMod1 + "/AlmThyratronTriggerEnable", POLLED_REFRESH,1500);
	uiAlmMod1.tPremagEnable->setSource(devMod1 + "/AlmPremagEnable", POLLED_REFRESH,1500);
	uiAlmMod1.tRfTrigger->setSource(devMod1 + "/AlmRfTrigger", POLLED_REFRESH,1500);
	uiAlmMod1.tLoad->setSource(devMod1 + "/AlmLoad", POLLED_REFRESH,1500);
	uiAlmMod1.tSections->setSource(devMod1 + "/AlmSections", POLLED_REFRESH,1500);
	uiAlmMod1.tRfTriggerEnable->setSource(devMod1 + "/AlmRfTriggerEnable", POLLED_REFRESH,1500);
	
	uiMod1.label->setText("MODULATORE1 - REAL TIME");
	uiMod1.tView->setText("View Alarms");
	uiMod1.tAcknowledge->setVisible(false);

	uiMod1.tModsafety->setSource(devMod1 + "/ModSafety", POLLED_REFRESH,1500);
	uiMod1.tThyratron->setSource(devMod1 + "/Thyratron", POLLED_REFRESH,1500);
	uiMod1.tKlystron->setSource(devMod1 + "/Klystron", POLLED_REFRESH,1500);
	uiMod1.tHvEnable->setSource(devMod1 + "/HvEnable", POLLED_REFRESH,1500);
	uiMod1.tKlystronEnable->setSource(devMod1 + "/KlystronEnable", POLLED_REFRESH,1500);
	uiMod1.tThyratronTriggerEnable->setSource(devMod1 + "/ThyratronTriggerEnable", POLLED_REFRESH,1500);
	uiMod1.tPremagEnable->setSource(devMod1 + "/PremagEnable", POLLED_REFRESH,1500);
	uiMod1.tRfTrigger->setSource(devMod1 + "/RfTrigger", POLLED_REFRESH,1500);
	uiMod1.tLoad->setSource(devMod1 + "/Load", POLLED_REFRESH,1500);
	uiMod1.tSections->setSource(devMod1 + "/Sections", POLLED_REFRESH,1500);
	uiMod1.tRfTriggerEnable->setSource(devMod1 + "/RfTriggerEnable", POLLED_REFRESH,1500);
	
	connect(uiMod1.tView, SIGNAL(clicked()), this, SLOT(showAlmMod1()));
	connect(uiAlmMod1.tView, SIGNAL(clicked()), this, SLOT(showMod1()));
}

void PreInjInterlock::connectMod2()
{
	uiAlmMod2.label->setText("MODULATORE2 - MEMO ALLARMI");
	uiAlmMod2.tView->setText("View Real Time");
	uiAlmMod2.tAcknowledge->setVisible(true);
	uiAlmMod2.tAcknowledge->setTargets(devMod2 + "->Acknowledge_all");

	uiAlmMod2.tModsafety->setSource(devMod2 + "/AlmModSafety", POLLED_REFRESH,1500);
	uiAlmMod2.tThyratron->setSource(devMod2 + "/AlmThyratron", POLLED_REFRESH,1500);
	uiAlmMod2.tKlystron->setSource(devMod2 + "/AlmKlystron", POLLED_REFRESH,1500);
	uiAlmMod2.tHvEnable->setSource(devMod2 + "/AlmHvEnable", POLLED_REFRESH,1500);
	uiAlmMod2.tKlystronEnable->setSource(devMod2 + "/AlmKlystronEnable", POLLED_REFRESH,1500);
	uiAlmMod2.tThyratronTriggerEnable->setSource(devMod2 + "/AlmThyratronTriggerEnable", POLLED_REFRESH,1500);
	uiAlmMod2.tPremagEnable->setSource(devMod2 + "/AlmPremagEnable", POLLED_REFRESH,1500);
	uiAlmMod2.tRfTrigger->setSource(devMod2 + "/AlmRfTrigger", POLLED_REFRESH,1500);
	uiAlmMod2.tLoad->setSource(devMod2 + "/AlmLoad", POLLED_REFRESH,1500);
	uiAlmMod2.tSections->setSource(devMod2 + "/AlmSections", POLLED_REFRESH,1500);
	uiAlmMod2.tRfTriggerEnable->setSource(devMod2 + "/AlmRfTriggerEnable", POLLED_REFRESH,1500);
	
	uiMod2.label->setText("MODULATORE2 - REAL TIME");
	uiMod2.tView->setText("View Alarms");
	uiMod2.tAcknowledge->setVisible(false);

	uiMod2.tModsafety->setSource(devMod2 + "/ModSafety", POLLED_REFRESH,1500);
	uiMod2.tThyratron->setSource(devMod2 + "/Thyratron", POLLED_REFRESH,1500);
	uiMod2.tKlystron->setSource(devMod2 + "/Klystron", POLLED_REFRESH,1500);
	uiMod2.tHvEnable->setSource(devMod2 + "/HvEnable", POLLED_REFRESH,1500);
	uiMod2.tKlystronEnable->setSource(devMod2 + "/KlystronEnable", POLLED_REFRESH,1500);
	uiMod2.tThyratronTriggerEnable->setSource(devMod2 + "/ThyratronTriggerEnable", POLLED_REFRESH,1500);
	uiMod2.tPremagEnable->setSource(devMod2 + "/PremagEnable", POLLED_REFRESH,1500);
	uiMod2.tRfTrigger->setSource(devMod2 + "/RfTrigger", POLLED_REFRESH,1500);
	uiMod2.tLoad->setSource(devMod2 + "/Load", POLLED_REFRESH,1500);
	uiMod2.tSections->setSource(devMod2 + "/Sections", POLLED_REFRESH,1500);
	uiMod2.tRfTriggerEnable->setSource(devMod2 + "/RfTriggerEnable", POLLED_REFRESH,1500);
	
	connect(uiMod2.tView, SIGNAL(clicked()), this, SLOT(showAlmMod2()));
	connect(uiAlmMod2.tView, SIGNAL(clicked()), this, SLOT(showMod2()));
}

void PreInjInterlock::connectFoc()
{
	uiFoc.tFocKlystron->setSource(devFoc + "/KlyFoc", POLLED_REFRESH,1500);
	uiFoc.tAbiFocKlystron->setSource(devFoc + "/AbiKlyFoc", POLLED_REFRESH,1500);
	uiFoc.tBuncherFoc->setSource(devFoc + "/BuncherFoc", POLLED_REFRESH,1500);
	uiFoc.tAbiBuncherFoc->setSource(devFoc + "/AbiBuncherFoc", POLLED_REFRESH,1500);
	uiFoc.tLensQuad->setSource(devFoc + "/LensQuad", POLLED_REFRESH,1500);
	uiFoc.tAbiLensQuad->setSource(devFoc + "/AbiLensQuad", POLLED_REFRESH,1500);
	uiFoc.tFocKlystron->setFalseColors("4293342999;4293342999;4293342999;4293342999;4293342999");
	uiFoc.tFocKlystron->setTrueColors("4284344910;4284344910;4284344910;4284344910;4284344910");
}

void PreInjInterlock::showAlmGun()
{
	ui.stackedGun->setCurrentIndex(1);
}

void PreInjInterlock::showGun()
{
	ui.stackedGun->setCurrentIndex(0);
}

void PreInjInterlock::showAlmMod1()
{
	ui.stackedMod1->setCurrentIndex(1);
}

void PreInjInterlock::showMod1()
{
	ui.stackedMod1->setCurrentIndex(0);
}

void PreInjInterlock::showAlmMod2()
{
	ui.stackedMod2->setCurrentIndex(1);
}

void PreInjInterlock::showMod2()
{
	ui.stackedMod2->setCurrentIndex(0);
}

void PreInjInterlock::updateAlarms()
{
	bool ok = true;
	foreach(QVariant v, uiMod2.tRfTriggerEnable->readValue())
		ok = ok && v.toBool();
	if (ok)
	{
		ui.tabWidget->setTabIcon(2, QIcon());
	}
	else
	{
		ui.tabWidget->setTabIcon(2, QIcon("/runtime/elettra/share/icons/warning.png"));
	}	
}