import { GetContentCommand } from '../commands/content/getContentCommand';
import { GetReportContentCommand } from '../commands/content/getReportContentCommand';
import { Content } from '../models/content/content';
import { Report } from '../models/content/report';
import { CompleteContentTaskInput } from '../models/tasks/completeContentTaskInput';
import { TaskType } from '../models/tasks/taskType';
import { BaseService } from './abstract/baseService';
import animationService from './animationService';
import connectService from './connectService';
import dataService from './dataService';
import userService from './userService';

class ContentService extends BaseService {
    public static sharedInstance = new ContentService();

    protected logTag: string = 'ContentService';

    public async getContent(contentId: string): Promise<Content | null> {
        const getContentCommand = new GetContentCommand(contentId.replace('/', '-'), connectService.currentUser && connectService.currentUser.externalId);
        const contentResult = await dataService.executeCommand(getContentCommand);
        return contentResult;
    }

    public async completeContent(contentId: string): Promise<Report | null> {
        const completeContentTask = new CompleteContentTaskInput(TaskType.CompleteContent);
        completeContentTask.contentId = contentId;
        await userService.userPerformedTask(completeContentTask);
        const reportResult = await this.getReportContent(contentId);
        if (reportResult) {
            reportResult.animationView = animationService.getAnimationConfiguration(reportResult.animationName, reportResult.pointsEarnedDisplay);
        }
        return reportResult;
    }

    public getChoiceCardTitle(content: Content): string {
        if (content.episodeNumber && content.seasonNumber) {
            return `S${content.seasonNumber} E${content.episodeNumber} ${content.episodeTitle}`;
        } else {
            return content.contentTitle;
        }
    }

    private async getReportContent(contentId: string): Promise<Report | null> {
        const getReportContentCommand = new GetReportContentCommand(contentId.replace('US/', 'US-'));
        return await dataService.executeCommand(getReportContentCommand);
    }
}

export default ContentService.sharedInstance as ContentService;