// NormalizeStats takes in a proto and normalizes it by converting
// any absolute value to value/TotalTime.
func NormalizeStats(p *bspb.BatteryStats) (*bspb.BatteryStats, error) {
	totalTimeHour := roundToTwoDecimal(float64((p.GetSystem().GetBattery().GetBatteryRealtimeMsec()) / (3600 * 1000)))

	if totalTimeHour == 0 {
		return nil, errors.New("battery real time cannot be 0")
	}
	normApp := &bspb.BatteryStats{
		ReportVersion:   p.ReportVersion,
		AggregationType: p.AggregationType,
	}

	// Normalize the app data
	for _, a1 := range p.GetApp() {
		a := normalizeApp(a1, totalTimeHour)
		normApp.App = append(normApp.App, a)
	}

	// Normalize system data
	s1 := p.GetSystem()
	s := &bspb.BatteryStats_System{}

	if norm := normalizeMessage(s1.GetBattery(), totalTimeHour); norm != nil {
		s.Battery = norm.(*bspb.BatteryStats_System_Battery)
	}
	if norm := normalizeMessage(s1.GetBatteryDischarge(), totalTimeHour); norm != nil {
		s.BatteryDischarge = norm.(*bspb.BatteryStats_System_BatteryDischarge)
	}
	s.BatteryLevel = s1.GetBatteryLevel()
	if norm := normalizeRepeatedMessage(s1.GetBluetoothState(), totalTimeHour); !norm.IsNil() {
		s.BluetoothState = norm.Interface().([]*bspb.BatteryStats_System_BluetoothState)
	}
	if norm := normalizeRepeatedMessage(s1.GetDataConnection(), totalTimeHour); !norm.IsNil() {
		s.DataConnection = norm.Interface().([]*bspb.BatteryStats_System_DataConnection)
	}
	if norm := normalizeMessage(s1.GetGlobalNetwork(), totalTimeHour); norm != nil {
		s.GlobalNetwork = norm.(*bspb.BatteryStats_System_GlobalNetwork)
	}
	if norm := normalizeRepeatedMessage(s1.GetKernelWakelock(), totalTimeHour); !norm.IsNil() {
		s.KernelWakelock = norm.Interface().([]*bspb.BatteryStats_System_KernelWakelock)
	}
	if norm := normalizeMessage(s1.GetMisc(), totalTimeHour); norm != nil {
		s.Misc = norm.(*bspb.BatteryStats_System_Misc)
	}
	if norm := normalizeRepeatedMessage(s1.GetPowerUseItem(), totalTimeHour); !norm.IsNil() {
		s.PowerUseItem = norm.Interface().([]*bspb.BatteryStats_System_PowerUseItem)
	}
	if norm := normalizeMessage(s1.GetPowerUseSummary(), totalTimeHour); norm != nil {
		s.PowerUseSummary = norm.(*bspb.BatteryStats_System_PowerUseSummary)
	}
	if norm := normalizeRepeatedMessage(s1.GetScreenBrightness(), totalTimeHour); !norm.IsNil() {
		s.ScreenBrightness = norm.Interface().([]*bspb.BatteryStats_System_ScreenBrightness)
	}
	if norm := normalizeMessage(s1.GetSignalScanningTime(), totalTimeHour); norm != nil {
		s.SignalScanningTime = norm.(*bspb.BatteryStats_System_SignalScanningTime)
	}
	if norm := normalizeRepeatedMessage(s1.GetSignalStrength(), totalTimeHour); !norm.IsNil() {
		s.SignalStrength = norm.Interface().([]*bspb.BatteryStats_System_SignalStrength)
	}
	if norm := normalizeRepeatedMessage(s1.GetWakeupReason(), totalTimeHour); !norm.IsNil() {
		s.WakeupReason = norm.Interface().([]*bspb.BatteryStats_System_WakeupReason)
	}
	if norm := normalizeRepeatedMessage(s1.GetWifiState(), totalTimeHour); !norm.IsNil() {
		s.WifiState = norm.Interface().([]*bspb.BatteryStats_System_WifiState)
	}
	if norm := normalizeRepeatedMessage(s1.GetWifiSupplicantState(), totalTimeHour); !norm.IsNil() {
		s.WifiSupplicantState = norm.Interface().([]*bspb.BatteryStats_System_WifiSupplicantState)
	}
	p.System = s
	p.App = normApp.App
	return p, nil
}