/***************************************************************************
         dcusercommandedit.h  -  Valknut User Command Editor Dialog
                             -------------------
    begin                : Sun May 25 2008
    copyright            : (C) 2008 by Edward Sheldrake
    email                : ejs1920@yahoo.co.uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <QStyle>

#include "dcusercommandeditor.h"

#include "dcconfig.h"

#include <dclib/core/types.h>

/** */
DCUserCommandEditor::DCUserCommandEditor( QWidget * parent ) : QDialog ( parent )
{
	setupUi(this);
	
	PushButton_OK->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogOkButton,0,PushButton_OK));
	PushButton_CANCEL->setIcon(qApp->style()->standardIcon(QStyle::SP_DialogCancelButton,0,PushButton_CANCEL));
	
	connect( RadioButton_SEPARATOR, SIGNAL(toggled(bool)), this, SLOT(slotTypeChanged(bool)) );
	connect( RadioButton_RAW, SIGNAL(toggled(bool)), this, SLOT(slotTypeChanged(bool)) );
	connect( RadioButton_CHAT, SIGNAL(toggled(bool)), this, SLOT(slotTypeChanged(bool)) );
	connect( RadioButton_PM, SIGNAL(toggled(bool)), this, SLOT(slotTypeChanged(bool)) );
	
	connect( LineEdit_COMMAND, SIGNAL(textChanged(const QString &)), this, SLOT(slotCommandChanged(const QString &)) );
	connect( LineEdit_TO, SIGNAL(textChanged(const QString &)), this, SLOT(slotCommandChanged(const QString &)) );
	
	connect( PushButton_OK, SIGNAL(clicked()), this, SLOT(accept()) );
	connect( PushButton_CANCEL, SIGNAL(clicked()), this, SLOT(reject()) );
	
	LineEdit_PREVIEW->setEnabled(FALSE);
	
	/* one radio button must be on otherwise things will not work properly */
	RadioButton_CHAT->setChecked(TRUE);
	
	command = new DC_UserMenuCommand();
}

/** */
DCUserCommandEditor::~DCUserCommandEditor()
{

}

/** */
void DCUserCommandEditor::SetValues( DC_UserMenuCommand * umc )
{
	if ( umc == 0 )
	{
		return;
	}
	
	command->m_nType     = umc->m_nType;
	command->m_sName     = umc->m_sName;
	command->m_sCommand  = umc->m_sCommand;
	command->m_pAction   = umc->m_pAction;
	command->m_nContext  = umc->m_nContext;
	command->m_sHubIP    = umc->m_sHubIP;
	command->m_sTarget   = umc->m_sTarget;
	command->m_nPosition = umc->m_nPosition;
	
	/* Load values that don't need any processing */
	LineEdit_NAME->setText( umc->m_sName.Data() );
	LineEdit_HUBIP->setText( umc->m_sHubIP.Data() );
	LineEdit_TO->setText( umc->m_sTarget.Data() );
	
	if ( command->m_nType == euctRawOnce )
	{
		CheckBox_ONCE_PER_NICK->setChecked(TRUE);
	}
	
	/* Set context */
	if ( umc->m_nContext & euccHub )
	{
		CheckBox_CONTEXT_HUB->setChecked(TRUE);
	}
	if ( umc->m_nContext & euccChat )
	{
		CheckBox_CONTEXT_USER->setChecked(TRUE);
	}
	if ( umc->m_nContext & euccSearch )
	{
		CheckBox_CONTEXT_SEARCH->setChecked(TRUE);
	}
	if ( umc->m_nContext & euccFilelist )
	{
		CheckBox_CONTEXT_FILELIST->setChecked(TRUE);
	}
	
	/* Analyse type */
	if ( umc->m_nType == euctSeparator )
	{
		RadioButton_SEPARATOR->setChecked(TRUE);
	}
	
	QString s = umc->m_sCommand.Data();
	
	/** Check for common NMDC public chat or private message commands */
	if ( (umc->m_nType == euctRaw) || (umc->m_nType == euctRawOnce) )
	{
		if ( (s.startsWith("<%[mynick]> ")) || (s.startsWith("<%[myNI]> ")) )
		{
			RadioButton_CHAT->setChecked(TRUE);
			if ( s.startsWith("<%[mynick]> ") )
			{
				s = s.mid(12);
			}
			else
			{
				s = s.mid(10);
			}
			
			if ( s.right(1) == "|" )
			{
				s = s.left( s.length() - 1 );
			}
			
		}
		else if ( s.startsWith("$To: ") )
		{
			RadioButton_PM->setChecked(TRUE);
			
			s = s.mid(5);
			int pos = s.find(" $From: ");
			QString target = s.left( s.length() - pos );
			if ( (target == "%[usernick]") || (target == "%[userNI]") )
			{
				// nothing
			}
			else
			{
				LineEdit_TO->setText(target);
			}
			
			pos = s.find(">");
			s = s.mid(pos+2);
			
			if ( s.right(1) == "|" )
			{
				s = s.left( s.length() - 1 );
			}
		}
		else
		{
			RadioButton_RAW->setChecked(TRUE);
		}
		LineEdit_COMMAND->setText(s);
	}
	
	slotTypeChanged(FALSE);
	slotCommandChanged(s);
}

/** */
DC_UserMenuCommand * DCUserCommandEditor::GetObject()
{
	command->m_sName   = LineEdit_NAME->text().toAscii().data();
	command->m_sHubIP  = LineEdit_HUBIP->text().toAscii().data();
	command->m_sTarget = LineEdit_TO->text().toAscii().data();
	
	if ( RadioButton_SEPARATOR->isChecked() )
	{
		command->m_nType = euctSeparator;
	}
	else if ( CheckBox_ONCE_PER_NICK->isChecked() )
	{
		command->m_nType = euctRawOnce;
	}
	else
	{
		command->m_nType = euctRaw;
	}
	
	command->m_nContext = 0;
	if ( CheckBox_CONTEXT_HUB->isChecked() )
	{
		command->m_nContext = command->m_nContext | euccHub;
	}
	if ( CheckBox_CONTEXT_USER->isChecked() )
	{
		command->m_nContext = command->m_nContext | euccChat;
	}
	if ( CheckBox_CONTEXT_SEARCH->isChecked() )
	{
		command->m_nContext = command->m_nContext | euccSearch;
	}
	if ( CheckBox_CONTEXT_FILELIST->isChecked() )
	{
		command->m_nContext = command->m_nContext | euccFilelist;
	}
	
	slotCommandChanged(LineEdit_COMMAND->text());
	command->m_sCommand = LineEdit_PREVIEW->text().toAscii().data();
	
	return command;
}

/** */
QString DCUserCommandEditor::getCurrentPrefix()
{
	if ( RadioButton_CHAT->isChecked() )
	{
		return QString("<%[myNI]> ");
	}
	else if ( RadioButton_PM->isChecked() )
	{
		if ( LineEdit_TO->text() != "" )
		{
			return QString("$To: ") + LineEdit_TO->text() + QString(" From: %[myNI] $<%[myNI]> ");
		}
		else
		{
			return QString("$To: %[userNI] From: %[myNI] $<%[myNI]> ");
		}
	}
	else
	{
		return QString("");
	}
}

/** */
QString DCUserCommandEditor::getCurrentSuffix()
{
	if ( RadioButton_CHAT->isChecked() || RadioButton_PM->isChecked() )
	{
		return QString("|");
	}
	else
	{
		return QString("");
	}
}

/** */
void DCUserCommandEditor::slotTypeChanged( bool /* checked */ )
{
	if ( RadioButton_SEPARATOR->isChecked() )
	{
		LineEdit_NAME->setEnabled(FALSE);
		LineEdit_COMMAND->setEnabled(FALSE);
		LineEdit_HUBIP->setEnabled(FALSE);
		LineEdit_TO->setEnabled(FALSE);
		CheckBox_ONCE_PER_NICK->setEnabled(FALSE);
	}
	else if ( RadioButton_RAW->isChecked() )
	{
		LineEdit_NAME->setEnabled(TRUE);
		LineEdit_COMMAND->setEnabled(TRUE);
		LineEdit_HUBIP->setEnabled(TRUE);
		LineEdit_TO->setEnabled(FALSE);
		CheckBox_ONCE_PER_NICK->setEnabled(TRUE);
	}
	else if ( RadioButton_CHAT->isChecked() )
	{
		LineEdit_NAME->setEnabled(TRUE);
		LineEdit_COMMAND->setEnabled(TRUE);
		LineEdit_HUBIP->setEnabled(FALSE);
		LineEdit_TO->setEnabled(FALSE);
		CheckBox_ONCE_PER_NICK->setEnabled(TRUE);
	}
	else if ( RadioButton_PM->isChecked() )
	{
		LineEdit_NAME->setEnabled(TRUE);
		LineEdit_COMMAND->setEnabled(TRUE);
		LineEdit_HUBIP->setEnabled(TRUE);
		LineEdit_TO->setEnabled(TRUE);
		CheckBox_ONCE_PER_NICK->setEnabled(TRUE);
	}
	
	slotCommandChanged(LineEdit_COMMAND->text());
}

/** */
void DCUserCommandEditor::slotCommandChanged( const QString & /* text */ )
{
	QString s = LineEdit_COMMAND->text();
	
	if ( (RadioButton_CHAT->isChecked()) || (RadioButton_PM->isChecked()) )
	{
		s.replace("$","&#36;");
		s.replace("|","&#124;");
	}
	
	LineEdit_PREVIEW->setText( getCurrentPrefix() + s + getCurrentSuffix() );
}
