/**
 * Copyright 2024 Phenix Real Time Solutions, Inc. Confidential and Proprietary. All Rights Reserved.
 */
// import TelemetryUrl from '/telemetry/TelemetryUrl';
import PlatformDetectionService from '../PlatformDetection.service';
import TelemetryConfiguration from '../telemetry/TelemetryConfiguration';
import TelemetryAppender from '../telemetry/TelemetryApender';
// import hostService from 'services/host-url.service';
// import userStore from 'services/user-store';
import ILogger from './LoggerInterface';
import Logger, {LoggingLevel} from './Logger';
import Appenders from './Appenders';
import LoggingThreshold from './LoggingThreshold';
import ConsoleAppender from './ConsoleAppender';
import LoggerDefaults from './LoggerDefaults';

export default class LoggerFactory {
  private static _loggers: {[category: string]: ILogger} = {};
  private static _appenders: Appenders = new Appenders();
  private static _threshold: LoggingThreshold = new LoggingThreshold();
  private static _telemetryConfiguration: TelemetryConfiguration = new TelemetryConfiguration();

  static get telemetryConfiguration(): TelemetryConfiguration {
    return this._telemetryConfiguration;
  }

  static applyLoggerConfig(): void {
    LoggerFactory.applyConsoleLogger(LoggingLevel['All']);
    LoggerFactory.applyLoggingLevel();
    LoggerFactory.applyTelemetryLogger();
  }

  static getLogger(category: string): ILogger {
    if (typeof category !== 'string') {
      category = 'portal';
    }

    const logger = LoggerFactory._loggers[category];

    if (logger) {
      return logger;
    }

    return (LoggerFactory._loggers[category] = new Logger(category, this._appenders, this._threshold));
  }

  static applyLoggingLevel(): void {
    this._threshold.setThreshold(LoggingLevel['All']);
  }

  static applyConsoleLogger(level: LoggingLevel): void {
    this._appenders.add(new ConsoleAppender(level || LoggerDefaults.defaultConsoleLoggingLevel));
  }

  static async applyTelemetryConfiguration(level: LoggingLevel): Promise<void> {
    const browser = PlatformDetectionService.browser;
    const applicationId = 'phenixrts.com-alex.zinn'; // TEMPORARY --> FOR DEVELOPMENT ONLY
    this._telemetryConfiguration.threshold = level || LoggerDefaults.defaultTelemetryLoggingLevel;
    this._telemetryConfiguration.url = 'https://pcast-stg.phenixrts.com/telemetry'; //TelemetryUrl.getTelemetryUrl();
    this._telemetryConfiguration.environment = 'https://pcast-stg.phenixrts.com'; // TODO(AZ): hostService.getHostUrl();
    this._telemetryConfiguration.tenancy = applicationId; // TODO(AZ): await userStore.get('applicationId');
    this._telemetryConfiguration.userId = applicationId;
    this._telemetryConfiguration.sessionId = 'some-session-id'; // TODOD(AZ):  await userStore.get('sessionId');
    this._telemetryConfiguration.browser = browser ? `${browser}/${PlatformDetectionService.version}` : 'unknown';
  }

  private static applyTelemetryLogger(): void {
    LoggerFactory.applyTelemetryConfiguration(LoggingLevel['Info']);

    this._appenders.add(new TelemetryAppender(this._telemetryConfiguration));
  }

  private constructor() {
    throw new Error('LoggerFactory is a static class that may not be instantiated');
  }
}

LoggerFactory.applyLoggerConfig();
