CustomGameStatsMenu::CustomGameStatsMenu() : MenuBase()
{
	// set up window
	SDL_Surface *surf;
	surf = pGFXManager->getUIGraphic(UI_MenuBackground);

	setBackground(surf,false);
	resize(surf->w,surf->h);

	setWindowWidget(&windowWidget);

	int localHouseColor = houseColor[pLocalHouse->getHouseID()];

	windowWidget.addWidget(&mainVBox, Point(24,23),	Point(screen->w - 48, screen->h - 32));

    captionLabel.setText(getBasename(currentGame->getGameInitSettings().getFilename(), true));
    captionLabel.setTextColor(localHouseColor + 3);
    captionLabel.setAlignment(Alignment_HCenter);
    mainVBox.addWidget(&captionLabel, 24);
    mainVBox.addWidget(VSpacer::create(24));

    mainVBox.addWidget(Spacer::create(), 0.05);

    mainVBox.addWidget(&mainHBox, 0.80);

    mainHBox.addWidget(Spacer::create(), 0.4);
    mainHBox.addWidget(&playerStatListVBox, 0.2);

    headerHBox.addWidget(&headerLabelDummy, 130);
    headerHBox.addWidget(Spacer::create(), 10);
    headerLabel1.setText(_("Built Object"));
    headerLabel1.setAlignment(Alignment_HCenter);
    headerLabel1.setTextColor(localHouseColor + 3);
    headerHBox.addWidget(&headerLabel1, 132);
    headerHBox.addWidget(Spacer::create(), 30);
    headerLabel2.setText(_("Destroyed"));
    headerLabel2.setAlignment(Alignment_HCenter);
    headerLabel2.setTextColor(localHouseColor + 3);
    headerHBox.addWidget(&headerLabel2, 132);
    headerLabel3.setText(_("Harvested Spice"));
    headerLabel3.setAlignment(Alignment_HCenter);
    headerLabel3.setTextColor(localHouseColor + 3);
    headerHBox.addWidget(Spacer::create(), 30);
    headerHBox.addWidget(&headerLabel3, 132);

    playerStatListVBox.addWidget(&headerHBox, 25);

    playerStatListVBox.addWidget(VSpacer::create(15));

    int maxBuiltValue = 0;
    int maxDestroyedValue = 0;
    float maxSpiceHarvested = 0.0;

    for(int i=0;i<NUM_HOUSES;i++) {
        House* pHouse = currentGame->getHouse(i);

        if(pHouse != NULL) {
            maxBuiltValue = std::max(maxBuiltValue, pHouse->getBuiltValue());
            maxDestroyedValue = std::max(maxDestroyedValue, pHouse->getDestroyedValue());
            maxSpiceHarvested = std::max(maxSpiceHarvested, pHouse->getHarvestedSpice());
        }
    }

    for(int i=0;i<NUM_HOUSES;i++) {
        HouseStat& curHouseStat = houseStat[i];
        House* pHouse = currentGame->getHouse(i);

        if(pHouse != NULL) {
            int color = houseColor[i];

            curHouseStat.houseName.setText(_("House") + " " + getHouseNameByNumber((HOUSETYPE) i));
            curHouseStat.houseName.setTextColor(color + 3);
            curHouseStat.houseHBox.addWidget(&curHouseStat.houseName, 140);
            curHouseStat.houseHBox.addWidget(Spacer::create(), 15);

            curHouseStat.value1.setText( stringify(pHouse->getBuiltValue()*100));
            curHouseStat.value1.setTextFont(FONT_STD10);
            curHouseStat.value1.setAlignment(Alignment_Right);
            curHouseStat.value1.setTextColor(color + 3);
            curHouseStat.houseHBox.addWidget(&curHouseStat.value1, 50);
            curHouseStat.houseHBox.addWidget(HSpacer::create(2));
            curHouseStat.progressBar1.setProgress( (maxBuiltValue == 0) ? 0.0 : (pHouse->getBuiltValue() * 100.0f / maxBuiltValue));
            curHouseStat.progressBar1.setDrawShadow(true);
            curHouseStat.progressBar1.setColor(color + 1);
            curHouseStat.vBox1.addWidget(Spacer::create(), 0.5);
            curHouseStat.vBox1.addWidget(&curHouseStat.progressBar1, 12);
            curHouseStat.vBox1.addWidget(Spacer::create(), 0.5);
            curHouseStat.houseHBox.addWidget(&curHouseStat.vBox1, 80);

            curHouseStat.houseHBox.addWidget(Spacer::create(), 30);

            curHouseStat.value2.setText( stringify(pHouse->getDestroyedValue()*100));
            curHouseStat.value2.setTextFont(FONT_STD10);
            curHouseStat.value2.setAlignment(Alignment_Right);
            curHouseStat.value2.setTextColor(color + 3);
            curHouseStat.houseHBox.addWidget(&curHouseStat.value2, 50);
            curHouseStat.houseHBox.addWidget(HSpacer::create(2));
            curHouseStat.progressBar2.setProgress( (maxDestroyedValue == 0) ? 0.0 : (pHouse->getDestroyedValue() * 100.0f / maxDestroyedValue));
            curHouseStat.progressBar2.setDrawShadow(true);
            curHouseStat.progressBar2.setColor(color + 1);
            curHouseStat.vBox2.addWidget(Spacer::create(), 0.5);
            curHouseStat.vBox2.addWidget(&curHouseStat.progressBar2, 12);
            curHouseStat.vBox2.addWidget(Spacer::create(), 0.5);
            curHouseStat.houseHBox.addWidget(&curHouseStat.vBox2, 80);

            curHouseStat.houseHBox.addWidget(Spacer::create(), 30);

            curHouseStat.value3.setText( stringify((int) pHouse->getHarvestedSpice()));
            curHouseStat.value3.setTextFont(FONT_STD10);
            curHouseStat.value3.setAlignment(Alignment_Right);
            curHouseStat.value3.setTextColor(color + 3);
            curHouseStat.houseHBox.addWidget(&curHouseStat.value3, 50);
            curHouseStat.houseHBox.addWidget(HSpacer::create(2));
            curHouseStat.progressBar3.setProgress( (maxSpiceHarvested == 0.0) ? 0.0 : (pHouse->getHarvestedSpice() * 100.0f / maxSpiceHarvested));
            curHouseStat.progressBar3.setDrawShadow(true);
            curHouseStat.progressBar3.setColor(color + 1);
            curHouseStat.vBox3.addWidget(Spacer::create(), 0.5);
            curHouseStat.vBox3.addWidget(&curHouseStat.progressBar3, 12);
            curHouseStat.vBox3.addWidget(Spacer::create(), 0.5);
            curHouseStat.houseHBox.addWidget(&curHouseStat.vBox3, 80);

            playerStatListVBox.addWidget(&curHouseStat.houseHBox, 20);

            playerStatListVBox.addWidget(VSpacer::create(15));
        }
    }

    mainHBox.addWidget(Spacer::create(), 0.4);

    mainVBox.addWidget(Spacer::create(), 0.05);

    mainVBox.addWidget(VSpacer::create(20));
	mainVBox.addWidget(&buttonHBox, 24);
	mainVBox.addWidget(VSpacer::create(14), 0.0);

    buttonHBox.addWidget(HSpacer::create(70));
    int totalTime = currentGame->getGameTime()/1000;
    timeLabel.setText(strprintf(_("@DUNE.ENG|22#Time: %d:%02d"), totalTime/3600, (totalTime%3600)/60));
    timeLabel.setTextColor(localHouseColor + 3);
	buttonHBox.addWidget(&timeLabel, 0.2);

	buttonHBox.addWidget(HSpacer::create(20));

	int nbdays = currentGame->getNumberOfDays();
	std::string  phase = currentGame->getDayPhaseString();
	std::string qualif,sepa, daycal, daytext;
	if (nbdays == 1) {
		qualif = (_("st"));
	} else if (nbdays == 2) {
		qualif = (_("nd"));
	} else {
		qualif = (_("th"));;
	}


	daycal= (_("on the"));

	daytext = phase+" "+daycal+" "+ std::to_string(nbdays)+ qualif+sepa;
    dayLabel.setText(daytext);
    dayLabel.setTextColor(localHouseColor + 3);
    if ( currentGame->getGameInitSettings().getGameOptions().daynight ) {
    	buttonHBox.addWidget(&dayLabel, 0.2);
    }

	buttonHBox.addWidget(Spacer::create(), 0.0625);
	buttonHBox.addWidget(Spacer::create(), 0.475);
	buttonHBox.addWidget(Spacer::create(), 0.0625);

    okButton.setText(_("OK"));
    okButton.setTextColor(localHouseColor + 3);
	okButton.setOnClick(std::bind(&CustomGameStatsMenu::onOK, this));
	buttonHBox.addWidget(&okButton, 0.2);
	buttonHBox.addWidget(HSpacer::create(90));
}
void GameStatsMenu::calculateScore(int level)
{
    Human_UnitsDestroyed = 0;
	AI_UnitsDestroyed = 0;

    Human_StructuresDestroyed = 0;
	AI_StructuresDestroyed = 0;

	Human_SpiceHarvested = 0.0;
	AI_SpiceHarvested = 0.0;

	totalTime = currentGame->GetGameTime()/1000;

    totalScore = level*45;

    double totalHumanCredits = 0.0;
	for(int i=0; i < MAX_PLAYERS; i++) {
        House* pHouse = currentGame->house[i];
        if(pHouse != NULL) {
            if(pHouse->isAI() == true) {
                AI_UnitsDestroyed += pHouse->getNumDestroyedUnits();
                AI_StructuresDestroyed += pHouse->getNumDestroyedStructures();
                AI_SpiceHarvested += pHouse->getHarvestedSpice();

                totalScore -= pHouse->getDestroyedValue();
            } else {
                Human_UnitsDestroyed += pHouse->getNumDestroyedUnits();
                Human_StructuresDestroyed += pHouse->getNumDestroyedStructures();
                Human_SpiceHarvested += pHouse->getHarvestedSpice();

                totalHumanCredits += pHouse->getCredits();

                totalScore += pHouse->getDestroyedValue();
            }
        }
	}

	totalScore += ((int) totalHumanCredits) / 100;

    for(RobustList<StructureClass*>::const_iterator iter = structureList.begin(); iter != structureList.end(); ++iter) {
        StructureClass* pStructure = *iter;
        if(pStructure->getOwner()->isAI() == false) {
            totalScore += currentGame->objectData.data[pStructure->getItemID()].price / 100;
        }
    }

    totalScore -= ((totalTime/60) + 1);

    for(RobustList<UnitClass*>::const_iterator iter = unitList.begin(); iter != unitList.end(); ++iter) {
        UnitClass* pUnit = *iter;
        if(pUnit->getItemID() == Unit_Harvester) {
            HarvesterClass* pHarvester = (HarvesterClass*) pUnit;
            if(pHarvester->getOwner()->isAI() == true) {
                AI_SpiceHarvested += pHarvester->getAmountOfSpice();
            } else {
                Human_SpiceHarvested += pHarvester->getAmountOfSpice();
            }
        }
    }

    if(currentGame->CheatsEnabled() == true) {
        rank = "Cheater";
    } else {
        int rankID = DuneText_SandFlea;

        if(totalScore >= 25)   rankID++;
        if(totalScore >= 50)   rankID++;
        if(totalScore >= 100)  rankID++;
        if(totalScore >= 150)  rankID++;
        if(totalScore >= 200)  rankID++;
        if(totalScore >= 300)  rankID++;
        if(totalScore >= 400)  rankID++;
        if(totalScore >= 500)  rankID++;
        if(totalScore >= 700)  rankID++;
        if(totalScore >= 1000) rankID++;
        if(totalScore >= 1400) rankID++;
    //    if(totalScore >= 1800) rankID++;

        rank = pTextManager->getDuneText(rankID);
    }
}