import {Injectable} from '@angular/core'; import {first} from 'rxjs/operators'; import {CommunicationOxService} from 'ox-core'; import {MicroLessonLevelConfiguration, MicroLessonInfoOxDefault} from 'ox-types'; @Injectable({ providedIn: 'root' }) export class AppInfoOxService { public microLessonInfo: MicroLessonInfoOxDefault; constructor(private communicationService: CommunicationOxService) { this.communicationService.receiveMicroLessonInfo.pipe(first()).subscribe(microLessonInfo => { this.microLessonInfo = microLessonInfo; }); } // // getInitialLevel(): number { // return this.microLessonInfo.initialLevel; // // return this.getFirstMicroLessonInfo().initialLevel; // } // TODO if something like this is used, should be manage with extraData, cause the structure does not allow // more than micro lesson info per microlesosn. // getFirstMicroLessonInfo(): MicroLessonInfoOx { // return this.microLessonInfo[0] ? (this.microLessonInfo as MicroLessonInfoOx[])[0] // : (this.microLessonInfo as MicroLessonInfoOx); // } // getMicroLessonInfoById(microLessonId: number) { // const aux: MicroLessonInfoOx[] = this.microLessonInfo[0] ? this.microLessonInfo as MicroLessonInfoOx[] // : [this.microLessonInfo as MicroLessonInfoOx]; // return aux.find(x => x.microLessonId === microLessonId); // } // public getMetricsTable(microLessonId: number, level: number = 1): MetricsTable { // console.warn('Calling get metrics. This is deprecated.'); // console.log('Calling get metrics. This is deprecated.'); // throw new Error('Calling metrics table, this is deprecated.'); // // const aux = this.getMicroLessonInfoById(microLessonId); // // return aux.metricTables.length > 1 ? aux.metricTables[level - 1] : aux.metricTables[0]; // } // TODO re enable this one. public getTotalChallengeCount(currentLevel?: number) { if (this.microLessonInfo.creatorInfo) { return this.microLessonInfo.creatorInfo.exerciseCount; } const challengesType = this.getMicroLessonLevelConfiguration(currentLevel) .types.find(x => x.mode === 'challenges'); if (!challengesType) { console.warn('You ask for total challenge count and there is not type added with id challenges.'); return; } return challengesType.value; } public getMicroLessonLevelConfiguration(level: number): MicroLessonLevelConfiguration { return this.microLessonInfo.microLessonLevelConfigurations[level ? level - 1 : 0]; } getMaxSublevel(newLevel: number): number { return this.microLessonInfo.creatorInfo ? 1 : this.getMicroLessonLevelConfiguration(newLevel).exercisesToUpSubLevel.length; } getExercisesToUpSubLevel(newLevel: number) { return this.microLessonInfo.creatorInfo ? [999] : this.getMicroLessonLevelConfiguration(newLevel).exercisesToUpSubLevel; } getGameTypes(value: number): string[] { return this.microLessonInfo.creatorInfo ? [this.microLessonInfo.creatorInfo.type] : this.getMicroLessonLevelConfiguration(value).types.map(x => x.mode); } getChallengesModeAndValue(value: number): { mode: 'totalTime' | 'challengeTime' | 'totalLives' | 'challengeLives' | 'challenges'; value: number; } { return this.microLessonInfo.creatorInfo ? { mode: this.microLessonInfo.creatorInfo.type as any, value: this.microLessonInfo.creatorInfo.exerciseCount } : this.getMicroLessonLevelConfiguration(value).types.find(x => x.mode === 'challenges'); } }