import { ZoneModel } from "../zone.model" ; import * as _ from "lodash" ; export class UserZonesModel { public static readonly TYPE_SPEED : string = "speed" ; public static readonly TYPE_PACE : string = "pace" ; public static readonly TYPE_GRADE_ADJUSTED_PACE : string = "gradeAdjustedPace" ; public static readonly TYPE_HEART_RATE : string = "heartRate" ; public static readonly TYPE_POWER : string = "power" ; public static readonly TYPE_RUNNING_POWER : string = "runningPower" ; public static readonly TYPE_CYCLING_CADENCE : string = "cyclingCadence" ; public static readonly TYPE_RUNNING_CADENCE : string = "runningCadence" ; public static readonly TYPE_GRADE : string = "grade" ; public static readonly TYPE_ELEVATION : string = "elevation" ; public static readonly TYPE_ASCENT : string = "ascent" ; public static asInstance ( userZonesModel ) { return new UserZonesModel ( userZonesModel . speed , userZonesModel . pace , userZonesModel . gradeAdjustedPace , userZonesModel . heartRate , userZonesModel . power , userZonesModel . runningPower , userZonesModel . cyclingCadence , userZonesModel . runningCadence , userZonesModel . grade , userZonesModel . elevation , userZonesModel . ascent ) ; } public static serialize ( zoneModels ) { const serialized = [ ] ; _ . forEach ( zoneModels , ( zoneModel , index ) => { if ( ! zoneModel . from && ! zoneModel . to ) { throw new Error ( "Cannot serialize zoneModels" ) ; } if ( zoneModels [ index - 1 ] ) { if ( zoneModels [ index - 1 ] . to !== zoneModel . from ) { serialized . push ( zoneModel . from ) ; } serialized . push ( zoneModel . to ) ; } else { serialized . push ( zoneModel . from ) ; serialized . push ( zoneModel . to ) ; } } ) ; return serialized ; } public static deserialize ( zones ) { const zoneModels = [ ] ; _ . forEach ( zones , ( zone , index ) => { if ( ! _ . isNumber ( zone ) ) { throw new Error ( "Cannot deserialize zones because of corrupted zones. Try to reset your settings from advanced menu (Go to 'Elevate App' => 'Contextual menu in top right' => 'Advanced')" ) ; } if ( _ . isNumber ( zones [ index + 1 ] ) ) { zoneModels . push ( { from : zone , to : zones [ index + 1 ] } ) ; } } ) ; return zoneModels ; } constructor ( public speed , public pace , public gradeAdjustedPace , public heartRate , public power , public runningPower , public cyclingCadence , public runningCadence , public grade , public elevation , public ascent ) { } public get ? ( type ) { const zones = _ . propertyOf ( this ) ( type ) ; if ( ! zones ) { throw new Error ( "Cannot retrieve zones for type: " + type ) ; } return UserZonesModel . deserialize ( zones ) ; } }