import * as CommonFunctions from '../../common-functions/common-functions';
import * as Models from '../../models/models';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import { Messages } from './ConsoleMessages';

class UserPropertiesValidation {

  public constructor () {
  }

  public async checkParameters ( userProperties: Models.UserProperties | null ): Promise<Models.UserProperties> {
    if ( !userProperties ) {
      throw new Error( Messages.optionsNotProvided );
    }

    if ( userProperties &&
      ( userProperties.jsonDir === null || typeof userProperties.jsonDir === 'undefined' || userProperties.jsonDir === '' ) ) {
      throw new Error( Messages.jsonPathNotProvided );
    }

    userProperties = this.initializeParameters( userProperties );

    const result = await CommonFunctions.fileSystemFunctions.checkFolder( userProperties.reportPath );

    if ( !result ) {
      const tzoffset = new Date().getTimezoneOffset() * 60000;
      userProperties.reportPath = path.join(
        os.tmpdir(),
        'multiple_cucumber_report',
        new Date( Date.now() - tzoffset ).toISOString().substr( 0, 19 ) ).replace( 'T', '___' ).replace( /:/g, '_' ).replace( /-/g, '_' );
      await fs.promises.mkdir( userProperties.reportPath, { recursive: true } );
    }
    return userProperties;
  }

  public initializeParameters ( userProperties: Models.UserProperties ): Models.UserProperties{
    userProperties.saveReportInMongoDb = typeof userProperties.saveReportInMongoDb === 'undefined'
      ? false
      : userProperties.saveReportInMongoDb;
    userProperties.isLinkToServer = typeof userProperties.isLinkToServer === 'undefined'
      ? false
      : userProperties.isLinkToServer;
    userProperties.saveCollectedJSON = typeof userProperties.saveCollectedJSON === 'undefined'
      ? false
      : userProperties.saveCollectedJSON;
    userProperties.projectName = typeof userProperties.projectName === 'undefined'
      ? ''
      : userProperties.projectName;
    userProperties.showExecutionTime = typeof userProperties.showExecutionTime === 'undefined'
      ? true
      : userProperties.showExecutionTime;
    userProperties.date = typeof userProperties.date === 'undefined'
      ? new Date()
      : userProperties.date;
    userProperties.expandStepsNotPassed = typeof userProperties.expandStepsNotPassed === 'undefined'
      ? true
      : userProperties.expandStepsNotPassed;
    userProperties.navigateToFeatureIfThereIsOnlyOne = typeof userProperties.navigateToFeatureIfThereIsOnlyOne === 'undefined'
      ? true
      : userProperties.navigateToFeatureIfThereIsOnlyOne;
    userProperties.openReportInBrowser = typeof userProperties.openReportInBrowser === 'undefined'
      ? false
      : userProperties.openReportInBrowser;
    userProperties.disableLog = typeof userProperties.disableLog === 'undefined'
      ? false
      : userProperties.disableLog;
    userProperties.server = typeof userProperties.server === 'undefined'
      ? {
        serverPort: 3100,
      }
      : userProperties.server;
    userProperties.mongoDb = typeof userProperties.mongoDb === 'undefined'
      ? {
        collections: {
          features: 'Features',
          outputs: 'Outputs',
          reports: 'Reports',
          scenarios: 'Scenarios',
          steps: 'Steps'
        },
        dbHost: 'localhost',
        dbName: 'multiple-cucumber-html-reports',
        dbPort: 27017,
      }
      : userProperties.mongoDb;
    userProperties.metadata = typeof userProperties.metadata === 'undefined' ? {} as Models.Metadata[] : userProperties.metadata;

    return userProperties;
  }
}

export const userPropertiesValidation = new UserPropertiesValidation();
