import { logger } from "../../utils/logger";
import { DeepLinkManager } from "../../utils/DeepLinkManager";
import { BootedDevice, IntentChooserResult, ObserveResult } from "../../models";
import { BaseVisualChange } from "../action/BaseVisualChange";
import { AdbClient } from "../../utils/android-cmdline-tools/AdbClient";

export class DetectIntentChooser extends BaseVisualChange {
  private deepLinkManager: DeepLinkManager;

  constructor(device: BootedDevice, adb: AdbClient | null = null) {
    super(device, adb);
    this.deepLinkManager = new DeepLinkManager(device);
  }

  /**
     * Execute intent chooser detection
     * @returns Promise with intent chooser detection results
     */
  async execute(): Promise<IntentChooserResult> {
    return this.observedInteraction(
      async (observeResult: ObserveResult) => {
        try {

          const viewHierarchy = observeResult.viewHierarchy;
          if (!viewHierarchy) {
            return { success: false, error: "View hierarchy not found" };
          }

          logger.info("[DetectIntentChooser] Starting intent chooser detection");
          const detected =
            viewHierarchy.intentChooserDetected ??
            this.deepLinkManager.detectIntentChooser(viewHierarchy);

          logger.info(`[DetectIntentChooser] Intent chooser detection completed. Detected: ${detected}`);

          return {
            success: true,
            detected
          };
        } catch (error) {
          logger.error(`[DetectIntentChooser] Failed to detect intent chooser: ${error}`);

          return {
            success: false,
            detected: false,
            error: error instanceof Error ? error.message : String(error)
          };
        }
      },
      {
        changeExpected: false
      }
    );
  }
}
