#include <qfiledialog.h>
#include "KrossWordPlayer.h"

static const char* fileopen_xpm[] = {
    "16 13 5 1",
    ". c #040404",
    "# c #808304",
    "a c None",
    "b c #f3f704",
    "c c #f3f7f3",
    "aaaaaaaaa...aaaa",
    "aaaaaaaa.aaa.a.a",
    "aaaaaaaaaaaaa..a",
    "a...aaaaaaaa...a",
    ".bcb.......aaaaa",
    ".cbcbcbcbc.aaaaa",
    ".bcbcbcbcb.aaaaa",
    ".cbcb...........",
    ".bcb.#########.a",
    ".cb.#########.aa",
    ".b.#########.aaa",
    "..#########.aaaa",
    "...........aaaaa"
};


static const char* solution_xpm[]={
    "16 16 4 1",
    ". c None",
    "# c #000000",
    "b c #0000ff",
    "a c #ffffff",
    "................",
    "................",
    "################",
    "#aaaa###########",
    "#aaaa###########",
    "#aaaa###########",
    "#aaaa###########",
    "b###############",
    "baaaa#aaaa#aaaa#",
    "bbaaa#aaaa#aaaa#",
    "bbbaa#aaaa#aaaa#",
    "bbbbb#aaaa#aaaa#",
    "bbbbbbbbb##aaaa#",
    "bbbbbbbbbbb#####",
    "bbbbbbbbbbbbbbbb",
    "bbbbbbbbbbbbbbbb"};


static const char* word_xpm[]={
    "16 16 3 1",
    ". c None",
    "# c #000000",
    "a c #0000ff",
    "................",
    "................",
    "................",
    "####..#########.",
    "####..#########.",
    ".##...##........",
    ".##...##........",
    ".##...########..",
    ".##...########..",
    "a##...##........",
    "aa#...##........",
    "aaaaaaaaa######.",
    "aaaaaaaaaaaaaa#.",
    "aaaaaaaaaaaaaaa.",
    "aaaaaaaaaaaaaaaa",
    "aaaaaaaaaaaaaaaa"};


static const char* letter_xpm[]={
    "16 16 3 1",
    ". c None",
    "# c #000000",
    "a c #0000ff",
    "................",
    "................",
    "................",
    "....#########...",
    "....#########...",
    "....##..........",
    "....##..........",
    "....########....",
    "....########....",
    "aa..##..........",
    "aaaa##..........",
    "aaaaaaa######...",
    "aaaaaaaaaaaa#...",
    "aaaaaaaaaaaaaa..",
    "aaaaaaaaaaaaaaaa",
    "aaaaaaaaaaaaaaaa"};

static const char *check_solution_xpm[]={
"16 16 5 1",
". c None",
"b c #000000",
"# c #00c000",
"a c #00ff00",
"c c #ffffff",
"............#a#.",
"...........#aaa#",
"bbbbbbbbbb#aaaaa",
"bccccbbbbbb#aaa#",
"bccccbbbbbbb#a#b",
"bccccbbbbbbbb#bb",
"bccccbbbbbbbbbbb",
"bbbbbbbbbbbbbbbb",
"bccccbccccbccccb",
"bccccbccccbccccb",
"bccccbccccbccccb",
"bccccbccccbccccb",
"bccccbccccbccccb",
"bbbbbbbbbbbbbbbb",
"bbbbbbbbbbbbbbbb",
"bbbbbbbbbbbbbbbb"};


static const char *check_word_xpm[]={
"16 16 4 1",
". c None",
"b c #000000",
"# c #00c000",
"a c #00ff00",
"............#a#.",
"...........#aaa#",
"..........#aaaaa",
"bbbb..bbbbb#aaa#",
"bbbb..bbbbbb#a#.",
".bb...bb.....#..",
".bb...bb........",
".bb...bbbbbbbb..",
".bb...bbbbbbbb..",
".bb...bb........",
".bb...bb........",
"bbbb..bbbbbbbbb.",
"bbbb..bbbbbbbbb.",
"................",
"................",
"................"};


static const char *check_letter_xpm[]={
"16 16 4 1",
". c None",
"b c #000000",
"# c #00c000",
"a c #00ff00",
"............#a#.",
"...........#aaa#",
"..........#aaaaa",
"....bbbbbbb#aaa#",
"....bbbbbbbb#a#.",
"....bb.......#..",
"....bb..........",
"....bbbbbbbb....",
"....bbbbbbbb....",
"....bb..........",
"....bb..........",
"....bbbbbbbbb...",
"....bbbbbbbbb...",
"................",
"................",
"................"};


static const char* crossword_xpm[] = {
    "24 24 7 1",
    "  c #FC0404",
    ". c #FCFCFC",
    "X c #3C0404",
    "o c #DCDCDC",
    "O c #240404",
    "+ c #040404",
    "@ c None",
    "oooooooooooooooooooooooo",
    "oooooooooooooooooooooooo",
    "oo+++++++++++++++++ooooo",
    "oo+...+...+++++...+ooooo",
    "oo+.+++++++++++++++++ooo",
    "oo+.+...+...+++++...+ooo",
    "oo+++.OOOOXOOOO++++++++o",
    "oo+.+.O   X   O++++...+o",
    "oo+.++O   X   O++++...+o",
    "oo+.+.O   X   O++++...+o",
    "oo+++.OOOOXOOOO++++++++o",
    "oo+++.+...+...+...+++++o",
    "oo+++++...+...+...+++++o",
    "oo+++++...+...+...+++++o",
    "oo+++++++++++++++++++++o",
    "oo+.+++++++...+...+...+o",
    "oo+.+++++++...+...+...+o",
    "oo+.+.+++++...+...+...+o",
    "oo+++.+++++++++++++++++o",
    "oooo+.+...+++++...+...+o",
    "oooo+++...+++++...+...+o",
    "oooooo+...+++++...+...+o",
    "oooooo+++++++++++++++++o",
    "oooooooooooooooooooooooo"
};

/** 
  General Class description 

  \author   Ernie Pasveer 
 */
KrossWordPlayer::KrossWordPlayer () : KMainWindow () {
    _crossWord = new QzCrossWord(this);

    menuBar();
    statusBar();
    setCentralWidget (_crossWord);

    _fileMenu = new KPopupMenu(this);
    _fileMenu->insertItem(QPixmap(fileopen_xpm),       "&Open...",        this, SLOT(launchOpenDialog()) );
    _fileMenu->insertItem(QPixmap(fileopen_xpm),       "&Save...",        this, SLOT(launchSaveDialog()) );
    _fileMenu->insertItem(                             "&Exit",           qApp, SLOT(quit()) );

    _viewMenu = new KPopupMenu(this);
    _viewMenu->insertItem(QPixmap(solution_xpm),       "Reveal Puzzle",   this, SLOT(revealSolution()) );
    _viewMenu->insertItem(QPixmap(word_xpm),           "Reveal Word",     this, SLOT(revealWord())     );
    _viewMenu->insertItem(QPixmap(letter_xpm),         "Reveal Letter",   this, SLOT(revealLetter())   );
    _viewMenu->insertSeparator();
    _viewMenu->insertItem(QPixmap(check_solution_xpm), "Check Puzzle",    this, SLOT(checkSolution())  );
    _viewMenu->insertItem(QPixmap(check_word_xpm),     "Check Word",      this, SLOT(checkWord())      );
    _viewMenu->insertItem(QPixmap(check_letter_xpm),   "Check Letter",    this, SLOT(checkLetter())    );
    _viewMenu->insertSeparator();
    _viewMenu->insertItem(                             "Clear",           this, SLOT(clearSolution())  );

    _helpMenu = new KPopupMenu(this);
    _helpMenu->insertItem("About...",                                     this, SLOT(launchAboutDialog()) );

    menuBar()->insertItem("&File", _fileMenu);
    menuBar()->insertItem("&View", _viewMenu);
    menuBar()->insertItem("&Help", _helpMenu);

    _toolBar = toolBar();
    _toolBar->setLabel( "Puzzle Operations" );

    KToolBarButton* openButton           = new KToolBarButton( QPixmap(fileopen_xpm), 1, _toolBar, "open",           "Open Puzzle" );
    _toolBar->addSeparator();

    KToolBarButton* revealSolutionButton = new KToolBarButton( QPixmap(solution_xpm), 2, _toolBar, "revealsolution", "Reveal Puzzle" );
    KToolBarButton* revealWordButton     = new KToolBarButton( QPixmap(word_xpm),     3, _toolBar, "revealword",     "Reveal Word" );
    KToolBarButton* revealLetterButton   = new KToolBarButton( QPixmap(letter_xpm),   4, _toolBar, "revealletter",   "Reveal Letter" );
    _toolBar->addSeparator();

    KToolBarButton* checkSolutionButton  = new KToolBarButton( QPixmap(check_solution_xpm), 5, _toolBar, "checksolution",  "Check Puzzle" );
    KToolBarButton* checkWordButton      = new KToolBarButton( QPixmap(check_word_xpm),     6, _toolBar, "checkword",      "Check Word" );
    KToolBarButton* checkLetterButton    = new KToolBarButton( QPixmap(check_letter_xpm),   7, _toolBar, "checkletter",    "Check Letter" );

    QObject::connect (openButton,           SIGNAL(clicked(int)), this, SLOT(launchOpenDialog()));

    QObject::connect (revealSolutionButton, SIGNAL(clicked(int)), this, SLOT(revealSolution()));
    QObject::connect (revealWordButton,     SIGNAL(clicked(int)), this, SLOT(revealWord()));
    QObject::connect (revealLetterButton,   SIGNAL(clicked(int)), this, SLOT(revealLetter()));

    QObject::connect (checkSolutionButton,  SIGNAL(clicked(int)), this, SLOT(checkSolution()));
    QObject::connect (checkWordButton,      SIGNAL(clicked(int)), this, SLOT(checkWord()));
    QObject::connect (checkLetterButton,    SIGNAL(clicked(int)), this, SLOT(checkLetter()));

    statusBar()->message ("Enjoy!", 5000);
}

/** 
  General Method description 

  \param    X    Description of X 
  \return        Description of return value 
 */
KrossWordPlayer::~KrossWordPlayer () {
}

/** 
  General Method description 

  \param    X    Description of X 
  \return        Description of return value 
 */
void KrossWordPlayer::launchOpenDialog() {
    QString filename = QFileDialog::getOpenFileName(QString::null, "*.puz *.txt");

    setPuzzle (filename);
}

void KrossWordPlayer::launchSaveDialog() {
    QString filename = QFileDialog::getSaveFileName(QString::null, "*.puz");

    savePuzzle (filename);
}

void KrossWordPlayer::setPuzzle (const QString& filename) {
    if (filename.isNull() == false) {
        _crossWord->setPuzzle(filename);
    }
}

void KrossWordPlayer::savePuzzle (const QString& filename) {
    if (filename.isNull() == false) {
        _crossWord->savePuzzle(filename);
    }
}

void KrossWordPlayer::revealSolution () {
    _crossWord->revealSolution(true);
}

void KrossWordPlayer::revealWord () {
    _crossWord->revealWord(true);
}

void KrossWordPlayer::revealLetter () {
    _crossWord->revealLetter(true);
}

void KrossWordPlayer::checkSolution () {
    _crossWord->checkSolution();
}

void KrossWordPlayer::checkWord () {
    _crossWord->checkWord();
}

void KrossWordPlayer::checkLetter () {
    _crossWord->checkLetter();
}

void KrossWordPlayer::clearSolution () {
    _crossWord->clearSolution();
}

/** 
  General Method description 

  \param    X    Description of X 
  \return        Description of return value 
 */
void KrossWordPlayer::launchAboutDialog() {
    KAboutDialog dlg (KAboutDialog::AbtAppStandard, "KrossWordPlayer", KDialogBase::Ok, KDialogBase::Ok, this, "", true);
    dlg.setProduct ("KrossWordPlayer", "1.4", "Ernie Pasveer", "2003");
    dlg.setAuthor  ("Ernie Pasveer", "EPasveer@houston.rr.com", "http://home.houston.rr.com/epasveer", "");
    dlg.setLogo    (QPixmap(crossword_xpm));
    dlg.setVersion ("1.4");

    dlg.addTextPage    ("About Me", "Mail me at Epasveer@houston.rr.com or \nvisit my web site at \nhttp://home.houston.rr.com/epasveer", true);
    dlg.addLicensePage ("License",  "Released under the GPL");

    dlg.exec();
}

int main (int argc, char* argv[]) {

    KApplication app (argc, argv, "KrossWordPlayer");

    KrossWordPlayer* w = new KrossWordPlayer ();
    if (argc == 2) {
        w->setPuzzle(QString(argv[1]));
    }

    w->show();

    app.setMainWidget (w);

    return app.exec ();
}

