#include "dialogtypeparser.h"
#include "yesnodialog.h"
#include "infodialog.h"
#include "msgdialog.h"
#include "gaugedialog.h"
#include "inputdialog.h"
#include "combodialog.h"
#include "rangedialog.h"
#include "spindialog.h"
#include "multiinputdialog.h"
#include "checklistdialog.h"
#include "menudialog.h"
#include "radiolistdialog.h"
#include "textdialog.h"

#include <QDialog>
#include <QStringList>
#include <QWidget>
#include <QDesktopWidget>

DialogTypeParser::DialogTypeParser(QObject *parent) : QObject( parent)
{
}
/*

This is the comment that was originally here for options. These
apparently are the options that the dialog program accepts
-------------------------------------------------------------

Transient options:
  --fixed-font
  --password (may be repeated 2 or 3 times before --2inputsbox or --3inputsbox)
  --password=1|2 (for --2inputsbox or --3inputsbox)
  --editable
  --time-stamp | --date-stamp
  --reverse
  --keep-colors
  --interval <timeout>
  --timeout <timeout> (in seconds)
  --no-tags
  --item-help (if used, the {...} parameters are needed in menus/lists widgets)
  --default-item <tag>
  --icon <xpm filename>
  --no-ok
  --no-cancel
  --no-buttons
  --default-no
  --wizard
  --help <help>
  --print <printer> (1)
  --check <label> [<status>]
  --ok-label <label>
  --cancel-label <label>
  --beep
  --beep-after
  --begin <Yorg> <Xorg>
  --ignore-eof
  --smooth
 Box options:

  --progress    <text> <height> <width> [<maxdots> [[-]<msglen>]]
  --2rangesbox  <text> <height> <width> <label1> <min1> <max1> <def1> <label2> <min2> <max2> <def2>
  --3rangesbox  <text> <height> <width> <label1> <min1> <max1> <def1> ... <label3> <min3> <max3> <def3>
  --2spinsbox   <text> <height> <width> <min1> <max1> <def1> <label1> <min2> <max2> <def2> <label2>
  --3spinsbox   <text> <height> <width> <min1> <max1> <def1> <label1> ... <min3> <max3> <def3> <label3>
  --buildlist   <text> <height> <width> <list height> <tag1> <item1> <status1> {<help1>}...
  --treeview    <text> <height> <width> <list height> <tag1> <item1> <status1> <item_depth1> {<help1>}...
  --fselect     <file> <height> <width>
  --dselect     <directory> <height> <width>
  --colorsel    <text> <height> <width> [<red> <green> <blue>]
  --fontsel     <font name> <height> <width>
  --calendar    <text> <height> <width> [<day> <month> <year>]
  --timebox     <text> <height> <width> [<hours> <minutes> <seconds>]

Special options:
  --version             (prints version number to stderr and exits).
  --print-version       (same as above in a cdialog-compatible way).
  --print-maxsize       (prints maximum menu size in characters and exits).
*/


QDialog *DialogTypeParser::getDialog( const QStringList &args) const
{
    DialogBase *dlg = 0;
    QString width;
    QString height;
	Qt::WindowFlags flags = Qt::Dialog;

	if (args.size() == 1)
	{
		qWarning(
			"TYPES:\n"
			"  --yesno <text> <height> <width>\n"
			"  --msgbox <text> <height> <width>\n"
			"  --infobox <text> <height> <width> [<timeout>]\n"
			"  --gauge <text> <height> <width> [<percent>]\n"
			"  --combobox <text> <height> <width> <item1> ... <itemN>\n"
			"  --menubox <text> <height> <width> <menu height> <tag1> <item1> {<help1>}...\n"
			"  --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1> {<help1>}...\n"
			"  --checklist <text> <height> <width> <list height> <tag1> <item1> <status1> {<help1>}...\n"
			"  --editbox <file> <height> <width>\n"
			"  --textbox <file> <height> <width>\n"
			"  --tailbox <file> <height> <width>\n"
			"  --inputbox <text> <height> <width> [<init>]\n"
			"  --2inputsbox <text> <height> <width> <label1> <init1> <label2> <init2>\n"
			"  --3inputsbox <text> <height> <width> <label1> <init1> <label2> <init2> <label3> <init3>\n"
			"  --rangebox <text> <height> <width> <min value> <max value> [<default value>]\n"
			"  --spinbox <text> <height> <width> <min value> <max value> <default value> <label>\n"
			"\n"
			"\n"
			"OPTIONS:\n"
			"  (no-op) --wmclass <class>\n"
			"  --backtitle <title>\n"
			"  (no-op) --screen-center\n"
			"  (no-op) --under-mouse\n"
			"  --url-encode\n"
			"  --center\n"
			"  --right\n"
			"  --fill\n"
			"  --no-wrap\n"
			"  --separator\n"
			"  --icon <iconfile>\n"
			"  --show-ok\n"
			"  --show-cancel\n"
			"  --no-ok\n"
			"  --no-cancel\n"
			"  --no-buttons\n"
			"  (no-op) --default-no\n"
			"  --help <message>\n"
			"  --check <label> <on|checked|true>\n"
			"  --ok-label <label>\n"
			"  --cancel-label <label>\n"
			"  --stdout\n"
#if QT_VERSION >= 0x040500
			"  --no-close\n"
#endif
			"WINDOW OPTIONS:\n"
			"  --wmclass {window|dialog|sheet|drawer|popup|tool|tooltip|splashscreen}\n"
			"  --msfixedsize\n"
			"  --bypasswm\n"
			"  --frameless\n"
			"  --titlehint\n"
			"  --wmmenuhint\n"
			"  --minimizehint\n"
			"  --maximizehint\n"
#if QT_VERSION >= 0x040500
			"  --closebuttonhint\n"
#endif
			"  --helpbuttonhint\n"
			"  --shadehint\n"
			"  --staysontop\n"
			"  --customwindowhint\n"
	          );
		exit(0);
	}

    if( args.contains("--yesno") )
    {

        int index = args.indexOf("--yesno");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );

        dlg = YesNoDialog::createWidget(text);
        if ( dlg == 0 )
            qWarning("--yesno <text> <height> <width>");
    }
    else if( args.contains("--msgbox") )
    {
        int index = args.indexOf("--msgbox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );

        dlg = MsgDialog::createWidget(text);
        if ( dlg == 0 )
            qWarning("--msgbox <text> <height> <width>");
    }
    else if( args.contains("--infobox") )
    {
        int index = args.indexOf("--infobox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString timeout = args.value( index + 4, "10");

        dlg = InfoDialog::createWidget(text,timeout);
        if ( dlg == 0 )
            qWarning("--infobox <text> <height> <width> [<timeout>]");
    }
    else if ( args.contains("--gauge") )
    {
        int index = args.indexOf("--gauge");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString percent = args.value( index + 4, "0" );
        dlg = GaugeDialog::createWidget(text,percent);
        if ( dlg == 0 )
            qWarning("--gauge <text> <height> <width> [<percent>]");
    }
    else if ( args.contains("--combobox") )
    {
        int index = args.indexOf("--combobox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QStringList options;
        for( int idx = index + 4; idx < args.count(); ++idx )
            options << args.value(idx);
        bool editable = args.contains("--editable");

        dlg = ComboDialog::createWidget(text,options,editable);
        if ( dlg == 0 )
            qWarning("--combobox <text> <height> <width> <item1> ... <itemN>");
    }
    else if ( args.contains("--menubox") )
    {
        int index = args.indexOf("--menubox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString listHeight = args.value( index + 4 );
        QStringList tags;
        QStringList items;
        QStringList helps;
        int offset = 2;
        bool noTags = args.contains("--no-tags");
        bool showHelp = args.contains("--item-help");
        if( showHelp )
            offset = 3;
        for( int idx = index + 4; idx < args.count() - offset; idx += offset )
        {
            tags << args.value(index + idx);
            items << args.value(index + idx + 1);
            if( showHelp )
                helps << args.value(index + idx + 2 );
            else
                helps << "";
        }

        dlg = MenuDialog::createWidget(text, listHeight, tags, items, helps, noTags);
        if ( dlg == 0 )
            qWarning("--menubox <text> <height> <width> <menu height> <tag1> <item1> {<help1>}...");
    }
    else if ( args.contains("--radiolist") )
    {
        int index = args.indexOf("--radiolist");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString listHeight = args.value( index + 4 );
        QStringList tags;
        QStringList items;
        QList<bool> states;
        QStringList helps;
        int offset = 3;
        bool noTags = args.contains("--no-tags");
        bool showHelp = args.contains("--item-help");
        if( showHelp )
            offset = 4;
        for( int idx = index + 4; idx < args.count() - offset; idx += offset )
        {
            tags << args.value(index + idx);
            items << args.value(index + idx + 1);
            QString checkedValue = args.value( index + idx + 2 );
            states << ( checkedValue == "on" || checkedValue == "checked" || checkedValue == "true" );
            if( showHelp )
                helps << args.value(index + idx + 3 );
            else
                helps << "";
        }

        dlg = RadioListDialog::createWidget(text, listHeight, tags, items, states, helps, noTags);
        if ( dlg == 0 )
            qWarning("--radiolist <text> <height> <width> <list height> <tag1> <item1> <status1> {<help1>}...");
    }
    else if ( args.contains("--checklist") )
    {
        int index = args.indexOf("--checklist");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString listHeight = args.value( index + 4 );
        QStringList tags;
        QStringList items;
        QList<bool> states;
        QStringList helps;
        int offset = 3;
        bool noTags = args.contains("--no-tags");
        bool showHelp = args.contains("--item-help");
        if( showHelp )
            offset = 4;
        for( int idx = index + 4; idx < args.count() - offset; idx += offset )
        {
            tags << args.value(index + idx);
            items << args.value(index + idx + 1);
            QString checkedValue = args.value( index + idx + 2 );
            states << ( checkedValue == "on" || checkedValue == "checked" || checkedValue == "true" );
            if( showHelp )
                helps << args.value(index + idx + 3 );
            else
                helps << "";
        }

        dlg = CheckListDialog::createWidget(text, listHeight, tags, items, states, helps, noTags);
        if ( dlg == 0 )
            qWarning("--checklist <text> <height> <width> <list height> <tag1> <item1> <status1> {<help1>}...");
    }
    else if ( args.contains("--editbox") )
    {
        int index = args.indexOf("--editbox");
        QString file = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );

        dlg = TextDialog::createWidget(TextDialog::Edit,file);
        if ( dlg == 0 )
            qWarning("--editbox <file> <height> <width>");
    }
    else if ( args.contains("--textbox") )
    {
        int index = args.indexOf("--textbox");
        QString file = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );

        dlg = TextDialog::createWidget(TextDialog::View,file);
        if ( dlg == 0 )
            qWarning("--textbox <file> <height> <width>");
    }
    else if ( args.contains("--tailbox") )
    {
        int index = args.indexOf("--tailbox");
        QString file = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );

        dlg = TextDialog::createWidget(TextDialog::Tail,file);
        if ( dlg == 0 )
            qWarning("--tailbox <file> <height> <width>");
    }
    else if ( args.contains("--logbox") )
    {
        int index = args.indexOf("--logbox");
        QString file = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );

        dlg = TextDialog::createWidget(TextDialog::Log,file);
        if ( dlg == 0 )
            qWarning("--logbox <file> <height> <width>");
    }
    else if ( args.contains("--inputbox") )
    {
        int index = args.indexOf("--inputbox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString initialText = args.value( index + 4, "" );
	bool password = args.contains("--password");

        dlg = InputDialog::createWidget(text,initialText,password);
        if ( dlg == 0 )
            qWarning("--inputbox <text> <height> <width> [<init>]");
    }
    else if ( args.contains("--2inputsbox") )
    {
        int index = args.indexOf("--2inputsbox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QStringList labels;
        QStringList initValues;
        QList<bool> passwords;

        labels << args.value( index + 4);
        initValues << args.value( index + 5);
        passwords << (args.contains("--password"));
        labels << args.value( index + 6);
        initValues << args.value( index + 7);
        passwords << (args.contains("--password=2") || ( args.count("--password") == 2));


        dlg = MultiInputDialog::createWidget(text,labels,initValues,passwords);
        if ( dlg == 0 )
            qWarning("--2inputsbox <text> <height> <width> <label1> <init1> <label2> <init2>");
    }
    else if ( args.contains("--3inputsbox") )
    {
        int index = args.indexOf("--3inputsbox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QStringList labels;
        QStringList initValues;
        QList<bool> passwords;

        labels << args.value( index + 4);
        initValues << args.value( index + 5);
        passwords << (args.contains("--password"));
        labels << args.value( index + 6);
        initValues << args.value( index + 7);
        passwords << (args.contains("--password=2") || ( args.count("--password") == 2));
        labels << args.value( index + 8);
        initValues << args.value( index + 9);
        passwords << (args.contains("--password=3") || ( args.count("--password") == 3));

        dlg = MultiInputDialog::createWidget(text,labels,initValues,passwords);
        if ( dlg == 0 )
            qWarning("--3inputsbox <text> <height> <width> <label1> <init1> <label2> <init2> <label3> <init3>");
    }
    else if ( args.contains("--rangebox") )
    {
        int index = args.indexOf("--rangebox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString minValue = args.value( index + 4 );
		QString maxValue = args.value( index + 5 );
		QString defaultValue = args.value( index + 6, "0" );
        dlg = RangeDialog::createWidget(text,minValue,maxValue,defaultValue);
        if ( dlg == 0 )
            qWarning("--rangebox <text> <height> <width> <min value> <max value> [<default value>]");
    }
    else if ( args.contains("--spinbox") )
    {
        int index = args.indexOf("--spinbox");
        QString text = args.value( index + 1 );
        height = args.value( index + 2 );
        width = args.value( index + 3 );
        QString minValue = args.value( index + 4 );
		QString maxValue = args.value( index + 5 );
		QString defaultValue = args.value( index + 6 );
		QString label = args.value( index + 7 );
        dlg = SpinDialog::createWidget(text,minValue,maxValue,defaultValue,label);
        if ( dlg == 0 )
            qWarning("--spinbox <text> <height> <width> <min value> <max value> <default value> <label>");
    }
    if( dlg )
    {
		Qt::WindowFlags defaultFlags = dlg->windowFlags();
		if( args.contains("--wmclass") )
		{
			int index = args.indexOf("--wmclass");
			QString wmclass = args.value(index + 1);

			if (wmclass == "window")
				flags = Qt::Window;
			else if (wmclass == "dialog")
				flags = Qt::Dialog;
			else if (wmclass == "sheet")
				flags = Qt::Sheet;
			else if (wmclass == "drawer")
				flags = Qt::Drawer;
			else if (wmclass == "popup")
				flags = Qt::Popup;
			else if (wmclass == "tool")
				flags = Qt::Tool;
			else if (wmclass == "tooltip")
				flags = Qt::ToolTip;
			else if (wmclass == "splashscreen")
				flags = Qt::SplashScreen;
			else
				qWarning("bunk wmclass");
		}
		if (args.contains("--msfixedsize"))
			flags |= Qt::MSWindowsFixedSizeDialogHint;
		if (args.contains("--bypasswm"))
			flags |= Qt::X11BypassWindowManagerHint;
		if (args.contains("--frameless"))
			flags |= Qt::FramelessWindowHint;
		if (args.contains("--titlehint"))
			flags |= Qt::WindowTitleHint;
		if (args.contains("--wmmenuhint"))
			flags |= Qt::WindowSystemMenuHint;
		if (args.contains("--minimizehint"))
			flags |= Qt::WindowMinimizeButtonHint;
		if (args.contains("--maximizehint"))
			flags |= Qt::WindowMaximizeButtonHint;

#if QT_VERSION >= 0x040500
		if (args.contains("--closebuttonhint"))
			flags |= Qt::WindowCloseButtonHint;
#endif
		if (args.contains("--helpbuttonhint"))
			flags |= Qt::WindowContextHelpButtonHint;
		if (args.contains("--shadehint"))
			flags |= Qt::WindowShadeButtonHint;
		if (args.contains("--staysontop"))
			flags |= Qt::WindowStaysOnTopHint;
		if (args.contains("--customwindowhint"))
			flags |= Qt::CustomizeWindowHint;

		if (defaultFlags != flags)
			dlg->setWindowFlags(flags);

		if (flags & Qt::X11BypassWindowManagerHint)
			dlg->activateWindow();


		if ( args.contains("--url-encode") )
		{
			dlg->setUrlEncodeOutput(true);
		}

        if( args.contains("--backtitle") )
        {
            int index = args.indexOf("--backtitle");
            QString backtitle = args.value(index + 1);
            dlg->setBackTitle(backtitle);
        }
        if( args.contains("--title") )
        {
            int index = args.indexOf("--title");
            QString title = args.value(index + 1);
            dlg->setTitle( title );
        }
        if( args.contains("--no-close") )
        {
#if QT_VERSION >= 0x040500
            Qt::WindowFlags flgs = dlg->windowFlags();
            flgs |= Qt::CustomizeWindowHint;
            flgs ^= Qt::WindowCloseButtonHint;
            dlg->setWindowFlags ( flgs );
#endif
        }
        if( args.contains("--screen-center") )
        {

        }
        if( args.contains("--under-mouse") )
        {

        }
        if( args.contains("--center") )
        {
            dlg->setAlignment( Qt::AlignCenter );
        }
        if( args.contains("--right") )
        {
            dlg->setAlignment( Qt::AlignRight);
        }
        if( args.contains("--fill") )
        {
            dlg->setAlignment( Qt::AlignJustify);
        }
        if( args.contains("--no-wrap") )
        {
            dlg->setNoWrap();
        }
        if( args.contains("--separator") )
        {
            int index = args.indexOf("--separator");
            QString separator = args.value(index + 1);
            dlg->setDelimiter(separator.at(0));
        }

		if( args.contains("--icon") )
		{
			int index = args.indexOf("--icon");
			QString icon = args.value(index + 1 );
			dlg->setIcon( icon );
		}

        if( args.contains("--show-ok") )
        {
            dlg->showOk();
        }
		else if( args.contains("--no-ok") )
        {
            dlg->hideOk();
        }

        if( args.contains("--show-cancel") )
        {
            dlg->showCancel();
		}
		else if( args.contains("--no-cancel") )
        {
            dlg->hideCancel();
        }

        if( args.contains("--no-buttons") )
        {
            dlg->hideButtons();
        }

        if( args.contains("--default-no") )
        {

        }
        if( args.contains("--help") )
        {
            int index = args.indexOf("--help");
            QString helpText = args.value(index + 1);
            dlg->addHelp(helpText);
        }
        if( args.contains("--check") )
        {
            int index = args.indexOf("--check");
            QString label = args.value(index + 1);
            QString checked = args.value(index + 2);

            if( !checked.startsWith("--") )
            {
               if( checked == "on" ||
                   checked == "checked" ||
                   checked == "true" )
                   dlg->enableCheckArea( label, true );
               else
                   dlg->enableCheckArea( label, false );
            }
            else
                dlg->enableCheckArea( label, false );
        }
        if( args.contains("--ok-label") )
        {
            int index = args.indexOf("--ok-label");
            QString label = args.value( index + 1 );
            dlg->setOkLabel(label);
        }
        if( args.contains("--cancel-label") )
        {
            int index = args.indexOf("--cancel-label");
            QString label = args.value( index + 1 );
            dlg->setCancelLabel(label);
        }
		dlg->setStandardOutput(args.contains("--stdout"));

		if ( ! dlg->adjustSize( width, height ) )
		{
			delete dlg;
			return 0;
		}
    }
    else
    {
        // Print usage here...
    }
    return dlg;
}

