void ReportDialog::GenerateRoutesReport()
{
    if(!m_bReportStale)
        return;
    m_bReportStale = false;

    /* sort configurations interate over each group of configurations
       with the same start and end to determine best and worst times,
       and cyclone crossings to determine cyclone times
    */

    std::map<wxString, std::list<RouteMapOverlay *> > routes;
    for(std::list<WeatherRoute*>::iterator it = m_WeatherRouting.m_WeatherRoutes.begin();
        it != m_WeatherRouting.m_WeatherRoutes.end(); it++) {
        if(!(*it)->routemapoverlay->ReachedDestination())
            continue;
        wxString route_string = (*it)->Start + _T(" - ") + (*it)->End;
        std::list<RouteMapOverlay *> overlays = routes[route_string];
        overlays.push_back((*it)->routemapoverlay);
        routes[route_string] = overlays;
    }

    if(routes.size() == 0) {
        m_htmlRoutesReport->SetPage(_("No routes to report yet."));
        return;
    }

    wxString page;
    for(std::map<wxString, std::list<RouteMapOverlay *> >::iterator it = routes.begin();
                          it != routes.end(); it++)
    {
        std::list<RouteMapOverlay *> overlays = it->second;
        if (overlays.begin() == overlays.end()) {
            // XXX not possible? but shut up compilers warnings
            continue;
        }
        RouteMapOverlay *first = *overlays.begin();

        RouteMapConfiguration c = first->GetConfiguration();
        page += _T("<p>");
        page += c.Start + _T(" ") + _("to") + _T(" ") + c.End + _T(" ") + wxString::Format
            (_T("(%ld ") + wxString(_("configurations")) + _T(")\n"), overlays.size());

        /* determine fastest time */
        wxTimeSpan fastest_time;
        RouteMapOverlay *fastest;

        std::multimap< wxDateTime, RouteMapOverlay * > sort_by_start;
        bool any_bad = false;
        bool any_good = false;

        for(std::list<RouteMapOverlay *>::iterator it2 = overlays.begin(); it2 != overlays.end(); it2++) {
            RouteMapOverlay *r = *it2;
            wxTimeSpan current_time = r->EndTime() - r->StartTime();
            sort_by_start.insert(std::pair< wxDateTime, RouteMapOverlay * >(r->StartTime() , r));
            if(r == first || current_time < fastest_time) {
                fastest_time = current_time;
                fastest = r;
            }
            if (r->RouteInfo(RouteMapOverlay::PERCENTAGE_UPWIND) > 50) {
                any_bad = true;
            } else {
                any_good = true;
            }
        }

        page += _("<dt>Fastest configuration ") + FormatTime(fastest->StartTime());
        page += wxString(_T(" ")) + _("avg speed") + wxString::Format
            (_T(": %.2f "), fastest->RouteInfo(RouteMapOverlay::AVGSPEED))
	    + _("knots");

        /* determine best times if upwind percentage is below 50 */
        page += _T("<dt>");
        page += _("Best Times (mostly downwind)") + wxString(_T(": "));
        if (any_good == false) {
            // no downwind route
            page += _("none");
        }
        else if (any_bad == false) {
            // all routes are downwind
            page += _("any");
        }
        else {
            bool first_print = true;
            std::multimap< wxDateTime, RouteMapOverlay * > reduce_by_start;
            // merge downwind routes in bigger interval
            // assume most routes with same start time are of same kind (downwind or upwind)
            std::multimap< wxDateTime, RouteMapOverlay * >::iterator it = sort_by_start.begin();
            while (it != sort_by_start.end() ) {
                // remove first upwind routes, from any_good test there's at least one downwind route
                for(; it != sort_by_start.end(); it++) {
                    RouteMapOverlay *r = it->second;
                    if (r->RouteInfo(RouteMapOverlay::PERCENTAGE_UPWIND) <= 50) {
                        break;
                     }
                }
                if (it == sort_by_start.end()) 
                    break;

                RouteMapOverlay *r = it->second;
                wxDateTime s = DisplayedTime(r->StartTime());
                wxDateTime e = DisplayedTime(r->EndTime());
                // merge downwind
                for(; it != sort_by_start.end(); it++) {
                    RouteMapOverlay *r = it->second;
                    if (r->RouteInfo(RouteMapOverlay::PERCENTAGE_UPWIND) > 50) {
                        break;
                    }
                    e = r->EndTime();
                }
                if(first_print)
                    first_print = false;
                else
                    page += _(" and ");
                page += s.Format(_T("%d %B ")) + _("to") + e.Format(_T(" %d %B"));
            }
        }
        
        // CUSTOMIZATION
        // Display the best option to travel in order
        // to get the most comfortable sailing
        page += _T("<dt>");
        page += _("Best Sailing Comfort") + wxString(_T(": "));
        wxDateTime best_comfort_date;
        int best_sailing_comfort = 6;
        for(std::multimap< wxDateTime, RouteMapOverlay * >::iterator it3 = sort_by_start.begin(); it3 != sort_by_start.end(); it3++) {
            RouteMapOverlay *r = it3->second;
            if (!best_comfort_date.IsValid() ||
                (best_comfort_date < r->StartTime() && best_sailing_comfort > r->RouteInfo(RouteMapOverlay::COMFORT)))
            {
                best_comfort_date = r->StartTime();
                best_sailing_comfort = r->RouteInfo(RouteMapOverlay::COMFORT);
            }
        }
        page += RouteMapOverlay::sailingConditionText(best_sailing_comfort);
        page += _T(" on ");
        page += FormatTime(best_comfort_date);
        
        page += _T("<dt>");
        page += _("Cyclones") + wxString(_T(": "));

        wxProgressDialog progressdialog(_("Weather Routing"), _("Calculating Cyclone Crossings"),
                                        overlays.size(), this, wxPD_ELAPSED_TIME);
        int pdi = 0;

        int cyclonemonths[12] = {0};
        std::list<RouteMapOverlay *> cyclone_safe_routes;
        bool allsafe = true, nonesafe = true;
        for(std::list<RouteMapOverlay *>::iterator it2 = overlays.begin(); it2 != overlays.end(); it2++) {
            switch((*it2)->Cyclones(cyclonemonths)) {
            case -1:
                page += _("Climatology data unavailable.");
                goto cyclonesfailed;
            case 0:
                cyclone_safe_routes.push_back(*it2);
                nonesafe = false;
            default:
                cyclone_safe_routes.push_back(NULL);
                allsafe = false;
            }

            progressdialog.Update(pdi++);
        }
        
        int i, j;
        for(i=0, j=11; i<12; j=i++)
            /* get first cyclone month */
            if(cyclonemonths[i] && !cyclonemonths[j]) {
                int lm = i;
                page += wxDateTime::GetMonthName((wxDateTime::Month)i);
                for(int k=i+1, l=i; ; l = k++) {
                    if(k==12) k = 0;
                    if(k == i)
                        break;
                    if(cyclonemonths[k] && !cyclonemonths[l]) {
                        page += _(" and ") + wxDateTime::GetMonthName((wxDateTime::Month)k);
                        lm = k;
                    } else if(!cyclonemonths[k] && cyclonemonths[l] && l!=lm)
                        page += _(" to ") + wxDateTime::GetMonthName((wxDateTime::Month)l);
                }
                goto had_some_cyclones;
            }
                    
        if(cyclonemonths[0])
            page += _("all months");
        else
            page += _("none");

    had_some_cyclones:;

        page += _T("<dt>");
        if(allsafe)
            page += _("All routes are safe from cyclones.");
        else if(nonesafe)
            page += _("No routes found to be safe from cyclones.");
        else {
            page += _("Start times for cyclone safe routes: ");
            /* note: does not merge beginning and end of linked list for safe times,
               this sometimes might be nice, but they will be in different years. */
            bool first = true;
            for(std::list<RouteMapOverlay *>::iterator it2 = cyclone_safe_routes.begin(); it2 != cyclone_safe_routes.end();
                        it2++)
            {
                if(!*it2) continue;
                if(!first)
                    page += _(" and ");
                first = false;
                page += DisplayedTime((*it2)->StartTime()).Format(_T("%x"));

                if(++it2 == cyclone_safe_routes.end())
                    break;

                if(!*it2)
                    continue;

                while(*it2 && ++it2 != cyclone_safe_routes.end());

                it2--;
                page += _(" to ") + DisplayedTime((*it2)->StartTime()).Format(_T("%x"));
            }
        }
    cyclonesfailed:;
    }

    m_htmlRoutesReport->SetPage(page);
}
void ReportDialog::GenerateRoutesReport()
{
    if(!m_bReportStale)
        return;
    m_bReportStale = false;

    /* sort configurations interate over each group of configurations
       with the same start and end to determine best and worst times,
       and cyclone crossings to determine cyclone times
    */

    std::map<wxString, std::list<RouteMapOverlay *> > routes;
    for(std::list<WeatherRoute*>::iterator it = m_WeatherRouting.m_WeatherRoutes.begin();
        it != m_WeatherRouting.m_WeatherRoutes.end(); it++) {
        if(!(*it)->routemapoverlay->ReachedDestination())
            continue;
        wxString route_string = (*it)->Start + _T(" - ") + (*it)->End;
        std::list<RouteMapOverlay *> overlays = routes[route_string];
        overlays.push_back((*it)->routemapoverlay);
        routes[route_string] = overlays;
    }

    if(routes.size() == 0) {
        m_htmlRoutesReport->SetPage(_("No routes to report yet."));
        return;
    }

    wxString page;
    for(std::map<wxString, std::list<RouteMapOverlay *> >::iterator it = routes.begin();
        it != routes.end(); it++) {
        std::list<RouteMapOverlay *> overlays = it->second;
        RouteMapOverlay *first = *overlays.begin();
        RouteMapConfiguration c = first->GetConfiguration();
        page += _T("<p>");
        page += c.Start + _T(" ") + _("to") + _T(" ") + c.End + _T(" ") + wxString::Format
            (_T("(%ld ") + wxString(_("configurations")) + _T(")\n"), overlays.size());

        /* determine fastest time */
        wxTimeSpan fastest_time;
        RouteMapOverlay *fastest = NULL;
        for(std::list<RouteMapOverlay *>::iterator it2 = overlays.begin(); it2 != overlays.end(); it2++) {
            wxTimeSpan current_time = ((*it2)->EndTime() - (*it2)->StartTime());
            if(*it2 == first || current_time < fastest_time) {
                fastest_time = current_time;
                fastest = *it2;
            }
        }
        page += _("<dt>Fastest configuration ") + fastest->StartTime().Format(_T("%x"));
        page += wxString(_T(" ")) + _("avg speed") + wxString::Format
            (_T(": %.2f knots"), fastest->RouteInfo(RouteMapOverlay::AVGSPEED));

        /* determine best times if upwind percentage is below 50 */
        page += _T("<dt>");
        page += _("Best Times (mostly downwind)") + wxString(_T(": "));

        bool last_bad, any_bad, any_good = false, first_print = true;

        wxDateTime best_time_start;

        std::list<RouteMapOverlay *>::iterator it2, it2end = overlays.begin(), prev;
        last_bad = overlays.back()->RouteInfo(RouteMapOverlay::PERCENTAGE_UPWIND) > 50;

        for(it2 = overlays.begin(); it2 != overlays.end(); it2++)
            if((*it2)->RouteInfo(RouteMapOverlay::PERCENTAGE_UPWIND) > 50) {
                any_bad = last_bad = true;
            } else {
                if(!best_time_start.IsValid() && last_bad) {
                    best_time_start = (*it2)->StartTime();
                    it2end = it2;
                    it2++;
                    break;
                }
                last_bad = false;
            }

        if(it2 == overlays.end())
            it2++;
        for(;;) {
            if(it2 == it2end)
                break;
            it2++;
            if(it2 == overlays.end())
                it2++;

            if((*it2)->RouteInfo(RouteMapOverlay::PERCENTAGE_UPWIND) > 50) {
                if(!last_bad) {
                    prev = it2;
                    prev--;
                    if(prev == overlays.end()) prev--;
                    if(first_print)
                        first_print = false;
                    else
                        page += _(" and ");
                    page += best_time_start.Format(_T("%d %B ")) +
                        _("to") + (*prev)->EndTime().Format(_T(" %d %B"));
                }
                last_bad = any_bad = true;
            } else {
                if(last_bad)
                    best_time_start = (*it2)->StartTime();
                last_bad = false;
                any_good = true;
            }
        }

        if(!any_bad)
            page += _("any");
        else if(!any_good)
            page += _("none");

        page += _T("<dt>");
        page += _("Cyclones") + wxString(_T(": "));

        wxProgressDialog progressdialog(_("Weather Routing"), _("Calculating Cyclone Crossings"),
                                        overlays.size(), this, wxPD_ELAPSED_TIME);
        int pdi = 0;

        int cyclonemonths[12] = {0};
        std::list<RouteMapOverlay *> cyclone_safe_routes;
        bool allsafe = true, nonesafe = true;
        for(it2 = overlays.begin(); it2 != overlays.end(); it2++) {
            switch((*it2)->Cyclones(cyclonemonths)) {
            case -1:
                page += _("Climatology data unavailable.");
                goto cyclonesfailed;
            case 0:
                cyclone_safe_routes.push_back(*it2);
                nonesafe = false;
            default:
                cyclone_safe_routes.push_back(NULL);
                allsafe = false;
            }

            progressdialog.Update(pdi++);
        }
        
        int i, j;
        for(i=0, j=11; i<12; j=i++)
            /* get first cyclone month */
            if(cyclonemonths[i] && !cyclonemonths[j]) {
                int lm = i;
                page += wxDateTime::GetMonthName((wxDateTime::Month)i);
                for(int k=i+1, l=i; ; l = k++) {
                    if(k==12) k = 0;
                    if(k == i)
                        break;
                    if(cyclonemonths[k] && !cyclonemonths[l]) {
                        page += _(" and ") + wxDateTime::GetMonthName((wxDateTime::Month)k);
                        lm = k;
                    } else if(!cyclonemonths[k] && cyclonemonths[l] && l!=lm)
                        page += _(" to ") + wxDateTime::GetMonthName((wxDateTime::Month)l);
                }
                goto had_some_cyclones;
            }
                    
        if(cyclonemonths[0])
            page += _("all months");
        else
            page += _("none");

    had_some_cyclones:;

        page += _T("<dt>");
        if(allsafe)
            page += _("All routes are safe from cyclones.");
        else if(nonesafe)
            page += _("No routes found to be safe from cyclones.");
        else {
            page += _("Start times for cyclone safe routes: ");
            /* note: does not merge beginning and end of linked list for safe times,
               this sometimes might be nice, but they will be in different years. */
            bool first = true;
            for(it2 = cyclone_safe_routes.begin(); it2 != cyclone_safe_routes.end(); it2++) {
                if(!*it2) continue;
                if(!first)
                    page += _(" and ");
                first = false;
                page += (*it2)->StartTime().Format(_T("%x"));

                if(++it2 == cyclone_safe_routes.end())
                    break;

                if(!*it2)
                    continue;

                while(*it2 && ++it2 != cyclone_safe_routes.end());

                it2--;
                page += _(" to ") + (*it2)->StartTime().Format(_T("%x"));
            }
        }
    cyclonesfailed:;
    }

    m_htmlRoutesReport->SetPage(page);
}