/*
 *  The Mana World
 *  Copyright (C) 2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  Andrei Karas
 *
 *  This file is part of The Mana World.
 *
 *  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
 *  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 "gui/setup_chat.h"
#include "gui/editdialog.h"
#include "gui/chat.h"

#include "gui/widgets/button.h"
#include "gui/widgets/checkbox.h"
#include "gui/widgets/label.h"
#include "gui/widgets/layouthelper.h"
#include "gui/widgets/inttextfield.h"
#include "gui/widgets/chattab.h"

#include "configuration.h"
#include "localplayer.h"
#include "log.h"

#include "utils/gettext.h"

#define ACTION_REMOVE_COLORS "remove colors"
#define ACTION_MAGIC_IN_DEBUG "magic in debug"
#define ACTION_ALLOW_COMMANDS_IN_CHATTABS "allow commands"
#define ACTION_SERVER_MSG_IN_DEBUG "server in debug"
#define ACTION_SHOW_CHAT_COLORS "show chat colors"
#define ACTION_MAX_CHAR_LIMIT "char limit"
#define ACTION_EDIT_CHAR_LIMIT "edit char limit"
#define ACTION_EDIT_CHAR_OK "edit char ok"
#define ACTION_MAX_LINES_LIMIT "lines limit"
#define ACTION_EDIT_LINES_LIMIT "edit lines limit"
#define ACTION_EDIT_LINES_OK "edit lines ok"
#define ACTION_CHAT_LOGGER "chat logger"
#define ACTION_TRADE_TAB "trade tab"
#define ACTION_HIDE_SHOP_MESSAGES "hide shop messages"
#define ACTION_SHOW_CHAT_HISTORY "show chat history"

Setup_Chat::Setup_Chat()
{
    setName(_("Chat"));

    mRemoveColors = config.getValueBool("removeColors", false);
    mRemoveColorsCheckBox = new CheckBox(_("Remove colors from received chat messages"),
                                         mRemoveColors,
                                         this, ACTION_REMOVE_COLORS);

    mMagicInDebug = config.getValueBool("showMagicInDebug", false);
    mMagicInDebugCheckBox = new CheckBox(_("Log magic messages in debug tab"),
                                         mMagicInDebug,
                                         this, ACTION_MAGIC_IN_DEBUG);

    mAllowCommandsInChatTabs = config.getValueBool("allowCommandsInChatTabs", true);
    mAllowCommandsInChatTabsCheckBox = new CheckBox(_("Allow magic and GM commands in all chat tabs"),
                                                    mAllowCommandsInChatTabs,
                                                    this, ACTION_ALLOW_COMMANDS_IN_CHATTABS);

    mServerMsgInDebug = config.getValueBool("serverMsgInDebug", false);
    mServerMsgInDebugCheckBox = new CheckBox(_("Show server messages in debug tab"),
                                             mServerMsgInDebug,
                                             this, ACTION_SERVER_MSG_IN_DEBUG);

    mEnableChatLogger = config.getValueBool("enableChatLog", false);
    mEnableChatLoggerCheckBox = new CheckBox(_("Enable chat Log"),
                                             mEnableChatLogger,
                                             this, ACTION_CHAT_LOGGER);

    mEnableTradeTab = config.getValueBool("enableTradeTab", true);
    mEnableTradeTabCheckBox = new CheckBox(_("Enable trade tab"),
                                             mEnableTradeTab,
                                             this, ACTION_TRADE_TAB);

    mHideShopMessages = config.getValueBool("hideShopMessages", true);
    mHideShopMessagesCheckBox = new CheckBox(_("Hide shop messages"),
                                             mHideShopMessages,
                                             this, ACTION_HIDE_SHOP_MESSAGES);

    mShowChatHistory = config.getValueBool("showChatHistory", true);
    mShowChatHistoryCheckBox = new CheckBox(_("Show chat history"),
                                             mShowChatHistory,
                                             this, ACTION_SHOW_CHAT_HISTORY);

    mShowChatColors = config.getValueBool("showChatColorsList", true);
    mShowChatColorsCheckBox = new CheckBox(_("Show chat colors list"),
                                           mShowChatColors,
                                           this, ACTION_SHOW_CHAT_COLORS);

    mMaxCharButton = new Button(_("Edit"), ACTION_EDIT_CHAR_LIMIT, this);
    int maxCharLimit = config.getValue("chatMaxCharLimit", 300);
    mMaxChar = (maxCharLimit != 0);
    mMaxCharCheckBox = new CheckBox(_("Limit max chars in chat line"),
                                    mMaxChar,
                                    this, ACTION_MAX_CHAR_LIMIT);

    mMaxCharField = new IntTextField(maxCharLimit, 0, 500, mMaxChar, 20);

    mMaxLinesButton = new Button(_("Edit"), ACTION_EDIT_LINES_LIMIT, this);
    int maxLinesLimit = config.getValue("chatMaxLinesLimit", 20);
    mMaxLines = (maxLinesLimit != 0);

    mMaxLinesCheckBox = new CheckBox(_("Limit max lines in chat"),
                                     mMaxLines,
                                     this, ACTION_MAX_LINES_LIMIT);

    mMaxLinesField = new IntTextField(maxLinesLimit, 0, 100, mMaxLines, 20);

    // Do the layout
    LayoutHelper h(this);
    ContainerPlacer place = h.getPlacer(0, 0);

    place(0, 0, mRemoveColorsCheckBox, 10);
    place(0, 1, mMagicInDebugCheckBox, 10);
    place(0, 2, mAllowCommandsInChatTabsCheckBox, 10);
    place(0, 3, mServerMsgInDebugCheckBox, 10);
    place(0, 4, mShowChatColorsCheckBox, 10);
    place(0, 5, mMaxCharCheckBox, 6);
    place(6, 5, mMaxCharField, 2);
    place(8, 5, mMaxCharButton, 2);
    place(0, 6, mEnableChatLoggerCheckBox, 10);
    place(0, 7, mMaxLinesCheckBox, 6);
    place(6, 7, mMaxLinesField, 2);
    place(8, 7, mMaxLinesButton, 2);
    place(0, 8, mEnableTradeTabCheckBox, 10);
    place(0, 9, mHideShopMessagesCheckBox, 10);
    place(0, 10, mShowChatHistoryCheckBox, 10);

    place.getCell().matchColWidth(0, 0);
    place = h.getPlacer(0, 1);

    setDimension(gcn::Rectangle(0, 0, 500, 500));
}

void Setup_Chat::action(const gcn::ActionEvent &event)
{
    if (event.getId() == ACTION_REMOVE_COLORS)
    {
        mRemoveColors = mRemoveColorsCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_MAGIC_IN_DEBUG)
    {
        mMagicInDebug = mMagicInDebugCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_ALLOW_COMMANDS_IN_CHATTABS)
    {
        mAllowCommandsInChatTabs = mAllowCommandsInChatTabsCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_SERVER_MSG_IN_DEBUG)
    {
        mServerMsgInDebug = mServerMsgInDebugCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_SHOW_CHAT_COLORS)
    {
        mShowChatColors = mShowChatColorsCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_MAX_CHAR_LIMIT)
    {
        mMaxChar = mMaxCharCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_EDIT_CHAR_LIMIT)
    {
        mEditDialog =  new EditDialog("Limit max chars in chat line",
                                      toString(mMaxCharField->getValue()),
                                    ACTION_EDIT_CHAR_OK);
        mEditDialog->addActionListener(this);
    }
    else if (event.getId() == ACTION_EDIT_CHAR_OK)
    {
        mMaxCharField->setValue(atoi(mEditDialog->getMsg().c_str()));
    }
    else if (event.getId() == ACTION_MAX_LINES_LIMIT)
    {
        mMaxLines = mMaxLinesCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_EDIT_LINES_LIMIT)
    {
        mEditDialog =  new EditDialog("Limit max lines in chat",
                                      toString(mMaxLinesField->getValue()),
                                    ACTION_EDIT_LINES_OK);
        mEditDialog->addActionListener(this);
    }
    else if (event.getId() == ACTION_EDIT_LINES_OK)
    {
        mMaxLinesField->setValue(atoi(mEditDialog->getMsg().c_str()));
    }
    else if (event.getId() == ACTION_CHAT_LOGGER)
    {
        mEnableChatLogger = mEnableChatLoggerCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_TRADE_TAB)
    {
        mEnableTradeTab = mEnableTradeTabCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_HIDE_SHOP_MESSAGES)
    {
        mHideShopMessages = mHideShopMessagesCheckBox->isSelected();
    }
    else if (event.getId() == ACTION_SHOW_CHAT_HISTORY)
    {
        mShowChatHistory = mShowChatHistoryCheckBox->isSelected();
    }
}

void Setup_Chat::cancel()
{
    mRemoveColors = config.getValueBool("removeColors", false);
    mRemoveColorsCheckBox->setSelected(mRemoveColors);

    mMagicInDebug = config.getValueBool("showMagicInDebug", false);
    mMagicInDebugCheckBox->setSelected(mMagicInDebug);

    mAllowCommandsInChatTabs = config.getValueBool("allowCommandsInChatTabs", true);
    mAllowCommandsInChatTabsCheckBox->setSelected(mAllowCommandsInChatTabs);

    mServerMsgInDebug = config.getValueBool("serverMsgInDebug", false);
    mServerMsgInDebugCheckBox->setSelected(mServerMsgInDebug);

    mShowChatColors = config.getValueBool("showChatColorsList", true);
    mShowChatColorsCheckBox->setSelected(mShowChatColors);

    int maxCharLimit = config.getValue("chatMaxCharLimit", 300);
    mMaxChar = (maxCharLimit != 0);
    mMaxCharCheckBox->setSelected(mMaxChar);
    mMaxCharField->setValue(maxCharLimit);
    mMaxCharField->setEnabled(mMaxChar);

    int maxLinesLimit = config.getValue("chatMaxLinesLimit", 20);
    mMaxLines = (maxLinesLimit != 0);
    mMaxLinesCheckBox->setSelected(mMaxLines);
    mMaxLinesField->setValue(maxLinesLimit);
    mMaxLinesField->setEnabled(mMaxLines);

    mEnableChatLogger = config.getValueBool("enableChatLog", false);
    mEnableChatLoggerCheckBox->setSelected(mEnableChatLogger);

    mEnableTradeTab = config.getValueBool("enableTradeTab", true);
    mEnableTradeTabCheckBox->setSelected(mEnableTradeTab);

    mHideShopMessages = config.getValueBool("hideShopMessages", true);
    mHideShopMessagesCheckBox->setSelected(mHideShopMessages);

    mShowChatHistory = config.getValueBool("showChatHistory", true);
    mShowChatHistoryCheckBox->setSelected(mShowChatHistory);
}

void Setup_Chat::apply()
{
    config.setValue("removeColors", mRemoveColors);
    config.setValue("showMagicInDebug", mMagicInDebug);
    config.setValue("allowCommandsInChatTabs", mAllowCommandsInChatTabs);
    config.setValue("serverMsgInDebug", mServerMsgInDebug);
    config.setValue("showChatColorsList", mShowChatColors);
    if (mMaxChar)
        config.setValue("chatMaxCharLimit", mMaxCharField->getValue());
    else
        config.setValue("chatMaxCharLimit", 0);
    if (mMaxLines)
        config.setValue("chatMaxLinesLimit", mMaxLinesField->getValue());
    else
        config.setValue("chatMaxLinesLimit", 0);
    config.setValue("enableChatLog", mEnableChatLogger);
    config.setValue("enableTradeTab", mEnableTradeTab);
    config.setValue("hideShopMessages", mHideShopMessages);
    config.setValue("showChatHistory", mShowChatHistory);
}
