/** Called when the stkgui file is read. It stores the pointer to various
 *  widgets and adds the right names for reverse mode.
 */
void GPInfoScreen::loadedFromFile()
{
    // The group spinner is filled in init every time the screen is shown
    // (since the groups can change if addons are added/deleted).
    m_group_spinner      = getWidget<SpinnerWidget>("group-spinner");
    m_reverse_spinner    = getWidget<SpinnerWidget>("reverse-spinner");
    m_reverse_spinner->addLabel(_("Default"));
    m_reverse_spinner->addLabel(_("None"));
    m_reverse_spinner->addLabel(_("All"));
    m_reverse_spinner->addLabel(_("Random"));
    m_reverse_spinner->setValue(0);

    m_num_tracks_spinner = getWidget<SpinnerWidget>("track-spinner");
    // Only init the number of tracks here, this way the previously selected
    // number of tracks will be the default.
    m_num_tracks_spinner->setValue(1);

    m_ai_kart_spinner = getWidget<SpinnerWidget>("ai-spinner");
    
    GUIEngine::IconButtonWidget* screenshot = getWidget<IconButtonWidget>("screenshot");
    screenshot->setFocusable(false);
    screenshot->m_tab_stop = false;
}   // loadedFromFile
/** Initialised the display. The previous screen has to setup m_track before
 *  pushing this screen using TrackInfoScreen::getInstance()->setTrack().
 */
void TrackInfoScreen::init()
{
    m_record_this_race = false;

    const int max_arena_players = m_track->getMaxArenaPlayers();
    const bool has_laps         = race_manager->modeHasLaps();
    const bool has_highscores   = race_manager->modeHasHighscores();

    getWidget<LabelWidget>("name")->setText(translations->fribidize(m_track->getName()), false);

    //I18N: when showing who is the author of track '%s'
    //I18N: (place %s where the name of the author should appear)
    getWidget<LabelWidget>("author")->setText( _("Track by %s", m_track->getDesigner()),
                                               false );

    LabelWidget* max_players = getWidget<LabelWidget>("max-arena-players");
    max_players->setVisible(m_track->isArena());
    if (m_track->isArena())
    {
        //I18N: the max players supported by an arena.
        max_players->setText( _("Max players supported: %d", max_arena_players), false );
    }

    // ---- Track screenshot
    GUIEngine::IconButtonWidget* screenshot = getWidget<IconButtonWidget>("screenshot");

    // images are saved squared, but must be stretched to 4:

    // temporary icon, will replace it just after (but it will be shown if the given icon is not found)
    screenshot->m_properties[PROP_ICON] = "gui/icons/main_help.png";

    ITexture* image = STKTexManager::getInstance()
        ->getTexture(m_track->getScreenshotFile(),
        "While loading screenshot for track '%s':", m_track->getFilename());
    if(!image)
    {
        image = STKTexManager::getInstance()->getTexture("main_help.png",
            "While loading screenshot for track '%s':", m_track->getFilename());
    }
    if (image != NULL)
        screenshot->setImage(image);

    m_target_value_spinner->setVisible(false);
    m_target_value_label->setVisible(false);

    // Soccer options
    // -------------
    if (m_is_soccer)
    {
        m_target_type_label->setText(_("Soccer game type"), false);

        m_target_value_spinner->setVisible(true);
        m_target_value_label->setVisible(true);

        if (UserConfigParams::m_num_goals <= 0)
            UserConfigParams::m_num_goals = UserConfigParams::m_num_goals.getDefaultValue();

        if (UserConfigParams::m_soccer_time_limit <= 0)
            UserConfigParams::m_soccer_time_limit = UserConfigParams::m_soccer_time_limit.getDefaultValue();

        m_target_type_spinner->clearLabels();
        m_target_type_spinner->addLabel(_("Time limit"));
        m_target_type_spinner->addLabel(_("Goals limit"));
        m_target_type_spinner->setValue(UserConfigParams::m_soccer_use_time_limit ? 0 : 1);

        if (UserConfigParams::m_soccer_use_time_limit)
        {
            m_target_value_label->setText(_("Maximum time (min.)"), false);
            m_target_value_spinner->setValue(UserConfigParams::m_soccer_time_limit);
        }
        else
        {
            m_target_value_label->setText(_("Number of goals to win"), false);
            m_target_value_spinner->setValue(UserConfigParams::m_num_goals);
        }
    }

    // options for free-for-all and three strikes battle
    // -------------
    if (m_show_ffa_spinner)
    {
        m_target_type_label->setText(_("Game mode"), false);
        m_target_type_spinner->clearLabels();
        m_target_type_spinner->addLabel(_("3 Strikes Battle"));
        m_target_type_spinner->addLabel(_("Free-For-All"));
        m_target_type_spinner->setValue(UserConfigParams::m_use_ffa_mode ? 1 : 0);

        m_target_value_label->setText(_("Maximum time (min.)"), false);
        m_target_value_spinner->setValue(UserConfigParams::m_ffa_time_limit);

        m_target_value_label->setVisible(UserConfigParams::m_use_ffa_mode);
        m_target_value_spinner->setVisible(UserConfigParams::m_use_ffa_mode);
    }

    // Lap count m_lap_spinner
    // -----------------------
    if (has_laps)
    {
        m_target_value_spinner->setVisible(true);
        m_target_value_label->setVisible(true);

        if (UserConfigParams::m_artist_debug_mode)
            m_target_value_spinner->setMin(0);
        else
            m_target_value_spinner->setMin(1);
        m_target_value_spinner->setValue(m_track->getActualNumberOfLap());
        race_manager->setNumLaps(m_target_value_spinner->getValue());

        m_target_value_label->setText(_("Number of laps"), false);
    }

    // Number of AIs
    // -------------
    const int local_players = race_manager->getNumLocalPlayers();
    const bool has_AI =
        (race_manager->getMinorMode() == RaceManager::MINOR_MODE_3_STRIKES ||
         race_manager->getMinorMode() == RaceManager::MINOR_MODE_FREE_FOR_ALL ||
         race_manager->getMinorMode() == RaceManager::MINOR_MODE_SOCCER ?
         m_track->hasNavMesh() && (max_arena_players - local_players) > 0 :
         race_manager->hasAI());
    m_ai_kart_spinner->setVisible(has_AI);
    m_ai_kart_label->setVisible(has_AI);

    if (has_AI)
    {
        m_ai_kart_spinner->setActive(true);

        int num_ai = int(UserConfigParams::m_num_karts_per_gamemode
            [race_manager->getMinorMode()]) - local_players;

        // Avoid negative numbers (which can happen if e.g. the number of karts
        // in a previous race was lower than the number of players now.

        if (num_ai < 0) num_ai = 0;
        m_ai_kart_spinner->setValue(num_ai);

        race_manager->setNumKarts(num_ai + local_players);
        // Set the max karts supported based on the battle arena selected
        if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
           race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER)
        {
            m_ai_kart_spinner->setMax(max_arena_players - local_players);
        }
        else
            m_ai_kart_spinner->setMax(stk_config->m_max_karts - local_players);
        // A ftl reace needs at least three karts to make any sense
        if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER)
        {
            m_ai_kart_spinner->setMin(std::max(0, 3 - local_players));
        }
        // Make sure in battle and soccer mode at least 1 ai for single player
        else if((race_manager->getMinorMode()==RaceManager::MINOR_MODE_3_STRIKES ||
            race_manager->getMinorMode()==RaceManager::MINOR_MODE_SOCCER) &&
            local_players == 1 &&
            !UserConfigParams::m_artist_debug_mode)
            m_ai_kart_spinner->setMin(1);
        else
            m_ai_kart_spinner->setMin(0);

    }   // has_AI
    else
        race_manager->setNumKarts(local_players);

    // Reverse track or random item in arena
    // -------------
    const bool reverse_available = m_track->reverseAvailable() &&
               race_manager->getMinorMode() != RaceManager::MINOR_MODE_EASTER_EGG;
    const bool random_item = m_track->hasNavMesh();

    m_option->setVisible(reverse_available || random_item);
    getWidget<LabelWidget>("option-text")->setVisible(reverse_available || random_item);
    if (reverse_available)
    {
        //I18N: In the track info screen
        getWidget<LabelWidget>("option-text")->setText(_("Drive in reverse"), false);
    }
    else if (random_item)
    {
        //I18N: In the track info screen
        getWidget<LabelWidget>("option-text")->setText(_("Random item location"), false);
    }

    if (reverse_available)
    {
        m_option->setState(race_manager->getReverseTrack());
    }
    else if (random_item)
    {
        m_option->setState(UserConfigParams::m_random_arena_item);
    }
    else
        m_option->setState(false);

    // Record race or not
    // -------------
    const bool record_available = race_manager->getMinorMode() == RaceManager::MINOR_MODE_TIME_TRIAL;
    m_record_race->setVisible(record_available);
    getWidget<LabelWidget>("record-race-text")->setVisible(record_available);
    if (race_manager->isRecordingRace())
    {
        // isRecordingRace() is true when it's pre-set by ghost replay selection
        // which force record this race
        m_record_this_race = true;
        m_record_race->setState(true);
        m_record_race->setActive(false);
        m_ai_kart_spinner->setValue(0);
        m_ai_kart_spinner->setActive(false);
        race_manager->setNumKarts(race_manager->getNumLocalPlayers());
        
        UserConfigParams::m_num_karts_per_gamemode[race_manager->getMinorMode()] = race_manager->getNumLocalPlayers();
    }
    else if (record_available)
    {
        m_record_race->setActive(true);
        m_record_race->setState(false);
    }

    // ---- High Scores
    m_highscore_label->setVisible(has_highscores);

    for (unsigned int i=0;i<HIGHSCORE_COUNT;i++)
    {
        m_kart_icons[i]->setVisible(has_highscores);
        m_highscore_entries[i]->setVisible(has_highscores);
    }

    RibbonWidget* bt_start = getWidget<GUIEngine::RibbonWidget>("buttons");
    bt_start->setFocusForPlayer(PLAYER_ID_GAME_MASTER);

    updateHighScores();
}   // init
/** Initialised the display. The previous screen has to setup m_track before
 *  pushing this screen using TrackInfoScreen::getInstance()->setTrack().
 */
void TrackInfoScreen::init()
{
    const bool has_laps       = race_manager->modeHasLaps();
    const bool has_highscores = race_manager->modeHasHighscores();

    getWidget<LabelWidget>("name")->setText(translations->fribidize(m_track->getName()), false);

    //I18N: when showing who is the author of track '%s'
    //I18N: (place %s where the name of the author should appear)
    getWidget<LabelWidget>("author")->setText( _("Track by %s", m_track->getDesigner()),
                                               false );

    // ---- Track screenshot
    GUIEngine::IconButtonWidget* screenshot = getWidget<IconButtonWidget>("screenshot");

    // images are saved squared, but must be stretched to 4:

    // temporary icon, will replace it just after (but it will be shown if the given icon is not found)
    screenshot->m_properties[PROP_ICON] = "gui/main_help.png";

    ITexture* image = irr_driver->getTexture(m_track->getScreenshotFile(),
                                    "While loading screenshot for track '%s':",
                                           m_track->getFilename()            );
    if(!image)
    {
        image = irr_driver->getTexture("main_help.png",
                                    "While loading screenshot for track '%s':",
                                    m_track->getFilename());
    }
    if (image != NULL)
        screenshot->setImage(image);

    // Lap count m_lap_spinner
    // -----------------------
    m_lap_spinner->setVisible(has_laps);
    getWidget<LabelWidget>("lap-text")->setVisible(has_laps);
    if (has_laps)
    {
        if (UserConfigParams::m_artist_debug_mode)
            m_lap_spinner->setMin(0);
        else
            m_lap_spinner->setMin(1);
        m_lap_spinner->setValue(m_track->getActualNumberOfLap());
        race_manager->setNumLaps(m_lap_spinner->getValue());
    }

    // Number of AIs
    // -------------
    const bool has_AI = race_manager->hasAI();
    m_ai_kart_spinner->setVisible(has_AI);
    getWidget<LabelWidget>("ai-text")->setVisible(has_AI);
    if (has_AI)
    {
        m_ai_kart_spinner->setActive(true);

        // Avoid negative numbers (which can happen if e.g. the number of karts
        // in a previous race was lower than the number of players now.
        int num_ai = UserConfigParams::m_num_karts - race_manager->getNumLocalPlayers();
        if (num_ai < 0) num_ai = 0;
        m_ai_kart_spinner->setValue(num_ai);
        race_manager->setNumKarts(num_ai + race_manager->getNumLocalPlayers());
        m_ai_kart_spinner->setMax(stk_config->m_max_karts - race_manager->getNumLocalPlayers());
        // A ftl reace needs at least three karts to make any sense
        if(race_manager->getMinorMode()==RaceManager::MINOR_MODE_FOLLOW_LEADER)
        {
            m_ai_kart_spinner->setMin(3-race_manager->getNumLocalPlayers());
        }
        else
            m_ai_kart_spinner->setMin(0);

    }   // has_AI

    // Reverse track
    // -------------
    const bool reverse_available = m_track->reverseAvailable() &&
               race_manager->getMinorMode() != RaceManager::MINOR_MODE_EASTER_EGG;
    m_reverse->setVisible(reverse_available);
    getWidget<LabelWidget>("reverse-text")->setVisible(reverse_available);
    if (reverse_available)
    {
        m_reverse->setState(race_manager->getReverseTrack());
    }
    else
        m_reverse->setState(false);

    // ---- High Scores
    m_highscore_label->setVisible(has_highscores);

    m_kart_icons[0]->setVisible(has_highscores);
    m_kart_icons[1]->setVisible(has_highscores);
    m_kart_icons[2]->setVisible(has_highscores);

    m_highscore_entries[0]->setVisible(has_highscores);
    m_highscore_entries[1]->setVisible(has_highscores);
    m_highscore_entries[2]->setVisible(has_highscores);

    RibbonWidget* bt_start = getWidget<GUIEngine::RibbonWidget>("buttons");
    bt_start->setFocusForPlayer(PLAYER_ID_GAME_MASTER);

    updateHighScores();
}   // init