import type {
  AccessibilityAuditConfig,
} from "../models/AccessibilityAudit";
import type {
  VideoRecordingConfigInput,
  DeviceSnapshotConfigInput,
  AppearanceConfigInput,
} from "../models";

export type PlanExecutionLockScope = "session" | "global";

/**
 * Global server configuration state
 * Set once during server initialization
 */
class ServerConfig {
  private static instance: ServerConfig;
  private _uiPerfModeEnabled: boolean = true;
  private _accessibilityAuditConfig: AccessibilityAuditConfig | null = null;
  private _memPerfAuditEnabled: boolean = false;
  private _predictiveUiEnabled: boolean = false;
  private _rawElementSearchEnabled: boolean = false;
  private _planExecutionLockScope: PlanExecutionLockScope = "session";
  private _videoRecordingDefaults: VideoRecordingConfigInput = {};
  private _deviceSnapshotDefaults: DeviceSnapshotConfigInput = {};
  private _appearanceDefaults: AppearanceConfigInput = {};
  private _skipCtrlProxyDownload: boolean = false;
  private _networkMockableEnabled: boolean = false;
  private _mcpRecordingEnabled: boolean = false;
  private _dismissKeyboardAfterInputEnabled: boolean = false;
  private _navigationScreenshotsEnabled: boolean = true;
  private _waitForPollingOverheadEnabled: boolean = true;
  private _a11yIncludeNotImportantViews: boolean = true;
  private _a11yReportViewIds: boolean = true;
  private _a11yRetrieveInteractiveWindows: boolean = true;
  private _planExecutionActive: boolean = false;

  private constructor() {}

  static getInstance(): ServerConfig {
    if (!ServerConfig.instance) {
      ServerConfig.instance = new ServerConfig();
    }
    return ServerConfig.instance;
  }

  setUiPerfMode(enabled: boolean): void {
    this._uiPerfModeEnabled = enabled;
  }

  isUiPerfModeEnabled(): boolean {
    return this._uiPerfModeEnabled;
  }

  setAccessibilityAuditConfig(config: AccessibilityAuditConfig | null): void {
    this._accessibilityAuditConfig = config;
  }

  getAccessibilityAuditConfig(): AccessibilityAuditConfig | null {
    return this._accessibilityAuditConfig;
  }

  isAccessibilityAuditEnabled(): boolean {
    return this._accessibilityAuditConfig !== null;
  }

  setMemPerfAuditMode(enabled: boolean): void {
    this._memPerfAuditEnabled = enabled;
  }

  isMemPerfAuditEnabled(): boolean {
    return this._memPerfAuditEnabled;
  }

  setPredictiveUiEnabled(enabled: boolean): void {
    this._predictiveUiEnabled = enabled;
  }

  isPredictiveUiEnabled(): boolean {
    return this._predictiveUiEnabled;
  }

  setRawElementSearchEnabled(enabled: boolean): void {
    this._rawElementSearchEnabled = enabled;
  }

  isRawElementSearchEnabled(): boolean {
    return this._rawElementSearchEnabled;
  }

  setPlanExecutionLockScope(scope: PlanExecutionLockScope): void {
    this._planExecutionLockScope = scope;
  }

  getPlanExecutionLockScope(): PlanExecutionLockScope {
    return this._planExecutionLockScope;
  }

  setVideoRecordingDefaults(defaults: VideoRecordingConfigInput): void {
    this._videoRecordingDefaults = { ...defaults };
  }

  getVideoRecordingDefaults(): VideoRecordingConfigInput {
    return { ...this._videoRecordingDefaults };
  }

  setDeviceSnapshotDefaults(defaults: DeviceSnapshotConfigInput): void {
    this._deviceSnapshotDefaults = { ...defaults };
  }

  getDeviceSnapshotDefaults(): DeviceSnapshotConfigInput {
    return { ...this._deviceSnapshotDefaults };
  }

  setAppearanceDefaults(defaults: AppearanceConfigInput): void {
    this._appearanceDefaults = { ...defaults };
  }

  getAppearanceDefaults(): AppearanceConfigInput {
    return { ...this._appearanceDefaults };
  }

  setSkipCtrlProxyDownload(skip: boolean): void {
    this._skipCtrlProxyDownload = skip;
  }

  isSkipCtrlProxyDownloadEnabled(): boolean {
    return this._skipCtrlProxyDownload;
  }

  setNetworkMockableEnabled(enabled: boolean): void {
    this._networkMockableEnabled = enabled;
  }

  isNetworkMockableEnabled(): boolean {
    return this._networkMockableEnabled;
  }

  setMcpRecordingEnabled(enabled: boolean): void {
    this._mcpRecordingEnabled = enabled;
  }

  isMcpRecordingEnabled(): boolean {
    return this._mcpRecordingEnabled;
  }

  setDismissKeyboardAfterInputEnabled(enabled: boolean): void {
    this._dismissKeyboardAfterInputEnabled = enabled;
  }

  isDismissKeyboardAfterInputEnabled(): boolean {
    return this._dismissKeyboardAfterInputEnabled;
  }

  setNavigationScreenshotsEnabled(enabled: boolean): void {
    this._navigationScreenshotsEnabled = enabled;
  }

  isNavigationScreenshotsEnabled(): boolean {
    return this._navigationScreenshotsEnabled;
  }

  setWaitForPollingOverheadEnabled(enabled: boolean): void {
    this._waitForPollingOverheadEnabled = enabled;
  }

  isWaitForPollingOverheadEnabled(): boolean {
    return this._waitForPollingOverheadEnabled;
  }

  setA11yIncludeNotImportantViews(enabled: boolean): void {
    this._a11yIncludeNotImportantViews = enabled;
  }

  setA11yReportViewIds(enabled: boolean): void {
    this._a11yReportViewIds = enabled;
  }

  setA11yRetrieveInteractiveWindows(enabled: boolean): void {
    this._a11yRetrieveInteractiveWindows = enabled;
  }

  getAccessibilityFlagsConfig(): { includeNotImportantViews: boolean; reportViewIds: boolean; retrieveInteractiveWindows: boolean } {
    return {
      includeNotImportantViews: this._a11yIncludeNotImportantViews,
      reportViewIds: this._a11yReportViewIds,
      retrieveInteractiveWindows: this._a11yRetrieveInteractiveWindows,
    };
  }

  setPlanExecutionActive(active: boolean): void {
    this._planExecutionActive = active;
  }

  isPlanExecutionActive(): boolean {
    return this._planExecutionActive;
  }

}

export const serverConfig = ServerConfig.getInstance();
