
import * as Models from '../../lib/models/models';
import * as mongoose from 'mongoose';
import { CollectFeatureFiles } from '../feature-collector/feature-collector';
import { CollectJsons } from '../collect-jsons/collect-jsons';
import { GenerateHtml } from './helpers/GenerateHtml';
import { MongooseHelper } from '../mongoose-report-manager/mongoose-helper';
import { SuiteFactory } from './ReportObjectsFormatters/SuiteFormatter';
import { userPropertiesValidation } from './helpers/UserPropertiesValidation';

export class GenerateReport {

  public constructor () {
  }

  public async generate ( userProperties: Models.UserProperties | null ): Promise<string | mongoose.Types.ObjectId | null> {
    userProperties = await userPropertiesValidation.checkParameters( userProperties );
    const features = await new CollectFeatureFiles( userProperties ).collectFeatures();
    const jsonFiles = await new CollectJsons( userProperties ).createJoinedJson();
    const jsonReport = await new SuiteFactory( userProperties ).parseJsons( jsonFiles, features );
    let reportId: mongoose.Types.ObjectId | null = null;
    if ( userProperties.saveReportInMongoDb ){
      reportId = await new MongooseHelper( userProperties ).insertReport( jsonReport );
    }
    await new GenerateHtml( jsonReport, userProperties ).createHtmlPages();
    return reportId;
  }

  public async generateHtmlReport( userProperties: Models.UserProperties, jsonReport: Models.ExtendedReport ): Promise<void>{
    userProperties = await userPropertiesValidation.checkParameters( userProperties );
    await new GenerateHtml( jsonReport, userProperties ).createHtmlPages();
  }
}
