/*
 *  The Mana World
 *  Copyright (C) 2009  The Mana World Development Team
 *  Copyright (C) 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/editdialog.h"
#include "gui/okdialog.h"
#include "gui/setup_theme.h"
#include "gui/theme.h"

#include "gui/widgets/button.h"
#include "gui/widgets/checkbox.h"
#include "gui/widgets/label.h"
#include "gui/widgets/layouthelper.h"
#include "gui/widgets/textfield.h"
#include "gui/widgets/dropdown.h"

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

#include "utils/gettext.h"

#include "resources/resourcemanager.h"

#define ACTION_THEME "theme"

class ThemesModel : public gcn::ListModel
{
public:
    ThemesModel()
    {
        mNames.push_back("(default)");
        Theme::fillSkinsList(mNames);
    }

    virtual ~ThemesModel() { }

    virtual int getNumberOfElements()
    {
        return mNames.size();
    }

    virtual std::string getElementAt(int i)
    {
        if (i >= getNumberOfElements())
            return _("???");

        return mNames[i];
    }

protected:
    std::vector<std::string> mNames;
};

Setup_Theme::Setup_Theme():
    mTheme(config.getValue("theme", config.getValue("selectedSkin", "")))
{
    setName(_("Theme"));

    mThemeLabel = new Label(_("Gui theme"));

    mThemeDropDown = new DropDown(new ThemesModel());
    mThemeDropDown->setActionEventId(ACTION_THEME);
    mThemeDropDown->addActionListener(this);


    std::string skin = Theme::getThemeName();
//    logger->log("skin: " + skin);
    if (!skin.empty())
        mThemeDropDown->setSelectedString(skin);
    else
        mThemeDropDown->setSelected(0);

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

    place(0, 0, mThemeLabel, 10);
    place(0, 1, mThemeDropDown, 6);

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

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

void Setup_Theme::action(const gcn::ActionEvent &event)
{
    if (event.getId() == ACTION_THEME)
    {
        if (mThemeDropDown->getSelected() == 0)
            mTheme = "";
        else
            mTheme = mThemeDropDown->getSelectedString();
    }
}

void Setup_Theme::cancel()
{
    mTheme = config.getValue("theme", config.getValue("selectedSkin", ""));
//    mThemeField->setText(mTheme);

}

void Setup_Theme::apply()
{
    if (config.getValue("theme", config.getValue("selectedSkin", "")) != mTheme)
    {
        new OkDialog(_("Theme Changed"),
                     _("Restart your client for the change to take effect."));
    }
    config.setValue("selectedSkin", "");
    config.setValue("theme", mTheme);
}
