package org.strangeforest.tcb.stats.model;

import java.math.*;

import static java.lang.String.*;
import static java.math.RoundingMode.*;
import static org.strangeforest.tcb.stats.util.PercentageUtil.*;

public class SurfaceTimelineItem {

	private final int season;
	private final BigDecimal hardPct;
	private final BigDecimal clayPct;
	private final BigDecimal grassPct;
	private final BigDecimal carpetPct;
	private BigDecimal hardOutdoorPct;
	private BigDecimal hardIndoorPct;

	private static final BigDecimal HUNDRED = new BigDecimal(100);

	public SurfaceTimelineItem(int season, int matchCount, int hardMatchCount, int clayMatchCount, int grassMatchCount, int carpetMatchCount) {
		this.season = season;
		hardPct = scaledPct(hardMatchCount, matchCount);
		clayPct = scaledPct(clayMatchCount, matchCount);
		grassPct = scaledPct(grassMatchCount, matchCount);
		BigDecimal aCarpetPct = scaledPct(carpetMatchCount, matchCount);
		BigDecimal hardClayGrassPct = hardPct.add(clayPct).add(grassPct);
		carpetPct = hardClayGrassPct.add(aCarpetPct).compareTo(HUNDRED) <= 0 ? aCarpetPct : HUNDRED.subtract(hardClayGrassPct);
	}

	public SurfaceTimelineItem(int season, int matchCount, int hardMatchCount, int clayMatchCount, int grassMatchCount, int carpetMatchCount, int hardIndoorMatchCount) {
		this.season = season;
		hardPct = scaledPct(hardMatchCount, matchCount);
		hardOutdoorPct = scaledPct(hardMatchCount - hardIndoorMatchCount, matchCount);
		hardIndoorPct = scaledPct(hardIndoorMatchCount, matchCount);
		clayPct = scaledPct(clayMatchCount, matchCount);
		grassPct = scaledPct(grassMatchCount, matchCount);
		BigDecimal aCarpetPct = scaledPct(carpetMatchCount, matchCount);
		BigDecimal hardClayGrassPct = hardOutdoorPct.add(hardIndoorPct).add(clayPct).add(grassPct);
		carpetPct = hardClayGrassPct.add(aCarpetPct).compareTo(HUNDRED) <= 0 ? aCarpetPct : HUNDRED.subtract(hardClayGrassPct);
	}

	public int getSeason() {
		return season;
	}

	public String getHardPct() {
		return formatPct(hardPct);
	}

	public String getClayPct() {
		return formatPct(clayPct);
	}

	public String getGrassPct() {
		return formatPct(grassPct);
	}

	public String getCarpetPct() {
		return formatPct(carpetPct);
	}

	public String getHardOutdoorPct() {
		return formatPct(hardOutdoorPct);
	}

	public String getHardIndoorPct() {
		return formatPct(hardIndoorPct);
	}

	private static BigDecimal scaledPct(int value, int from) {
		return new BigDecimal(pct(value, from)).setScale(1, HALF_EVEN);
	}

	private static String formatPct(BigDecimal pct) {
		return format("%1$.1f%%", pct);
	}
}

@@ -1299,52 +1299,66 @@
 this
-.
+(
 season
- = season;%0A%09%09hardPct = scaledPct(hard
+, matchCount, hardMatchCount, clayMatchCount, grass
 Matc
@@ -1357,33 +1357,39 @@
 rassMatchCount, 
-m
+carpetM
 atchCount);%0A%09%09ha
@@ -1532,395 +1532,8 @@
 t);%0A
-%09%09clayPct = scaledPct(clayMatchCount, matchCount);%0A%09%09grassPct = scaledPct(grassMatchCount, matchCount);%0A%09%09BigDecimal aCarpetPct = scaledPct(carpetMatchCount, matchCount);%0A%09%09BigDecimal hardClayGrassPct = hardOutdoorPct.add(hardIndoorPct).add(clayPct).add(grassPct);%0A%09%09carpetPct = hardClayGrassPct.add(aCarpetPct).compareTo(HUNDRED) %3C= 0 ? aCarpetPct : HUNDRED.subtract(hardClayGrassPct);%0A
 %09%7D%0A%0A

Revert Fix