{
    "$comment": "=== 自动化安装器 Pipeline v2 — 循环状态机模式 ===",
    "$comment2": "使用 StepDispatcher + [JumpBack] 实现循环扫描：每完成一步自动回到 Dispatcher 继续识别下一步",

    "AutoInstallEntry": {
        "recognition": "DirectHit",
        "action": "DoNothing",
        "post_delay": 300,
        "next": [
            "StepDispatcher"
        ]
    },

    "$comment_dispatcher": "=== 核心调度器：循环扫描所有可能的步骤 ===",

    "StepDispatcher": {
        "recognition": "DirectHit",
        "action": "DoNothing",
        "timeout": 300000,
        "rate_limit": 1000,
        "next": [
            "[JumpBack]Step_UncheckedBundleware",
            "[JumpBack]Step_AgreeCheckbox",
            "[JumpBack]Step_AgreeTextButton",
            "[JumpBack]Step_CustomInstall",
            "[JumpBack]Step_ChangeInstallPath",
            "[JumpBack]Step_ClickInstallPathInput",
            "[JumpBack]Step_ClickNext",
            "[JumpBack]Step_ClickInstall",
            "Step_Finish"
        ],
        "on_error": [
            "HandleTimeout"
        ]
    },

    "$comment_bundleware": "=== 取消捆绑软件勾选：识别已勾选的 checkbox 并点击取消 ===",

    "Step_UncheckedBundleware": {
        "recognition": "TemplateMatch",
        "template": "checkbox/checkbox_checked.png",
        "threshold": 0.7,
        "roi": [0, 0, 0, 0],
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 500,
        "max_hit": 10
    },

    "$comment_agree_checkbox": "=== 勾选同意协议 checkbox：识别未勾选的 checkbox 并点击 ===",

    "Step_AgreeCheckbox": {
        "recognition": "Or",
        "any_of": [
            {
                "sub_name": "checkbox_template",
                "recognition": "TemplateMatch",
                "template": "checkbox/checkbox_unchecked.png",
                "threshold": 0.65,
                "method": 5
            },
            {
                "sub_name": "checkbox_color",
                "recognition": "ColorMatch",
                "method": 4,
                "lower": [220, 220, 220],
                "upper": [255, 255, 255],
                "count": 100,
                "connected": true
            }
        ],
        "roi": [0, 0, 0, 0],
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 800,
        "max_hit": 5
    },

    "$comment_agree_text": "=== 点击同意/接受文字按钮 ===",

    "Step_AgreeTextButton": {
        "recognition": "OCR",
        "expected": [
            "我同意", "我接受", "同意并安装", "同意", "接受",
            "I Agree", "I Accept", "Accept", "Agree"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 800,
        "max_hit": 5
    },

    "$comment_custom_install": "=== 点击自定义安装（避免默认安装到C盘或捆绑组件）===",

    "Step_CustomInstall": {
        "recognition": "OCR",
        "expected": [
            "自定义安装", "自定义", "Custom", "Custom Install", "Advanced"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 1000,
        "max_hit": 1
    },

    "$comment_change_path": "=== 修改安装路径：三步流程（点击输入框 → 全选 → 输入新路径）===",
    "$comment_change_path2": "默认禁用。当指定 --install-dir 时，build_pipeline_override 会启用并注入路径",

    "Step_ChangeInstallPath": {
        "recognition": "OCR",
        "expected": [
            "浏览", "Browse", "更改", "Change", "更改目录", "选择目录"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 500,
        "max_hit": 2,
        "enabled": false,
        "$comment": "默认禁用。点击浏览/更改按钮进入路径选择"
    },

    "$comment_click_path_input": "=== 点击安装路径输入框获取焦点 ===",

    "Step_ClickInstallPathInput": {
        "recognition": "OCR",
        "expected": [
            "C:", "D:", "E:", "F:",
            "Program Files",
            "Users", "AppData", "ProgramData"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.5,
        "action": "Click",
        "pre_delay": 200,
        "post_delay": 500,
        "max_hit": 3,
        "enabled": false,
        "next": [
            "Step_SelectAllPath_KeyDown"
        ],
        "$comment": "默认禁用。识别路径文本（如 C:\\Program Files\\...）并点击获取焦点"
    },

    "$comment_select_all_path": "=== 全选路径输入框内容：KeyDown(Ctrl) → ClickKey(A) → KeyUp(Ctrl) ===",

    "Step_SelectAllPath_KeyDown": {
        "recognition": "DirectHit",
        "action": "KeyDown",
        "key": 17,
        "post_delay": 50,
        "next": [
            "Step_SelectAllPath_PressA"
        ],
        "$comment": "按下 Ctrl 键 (VK_CONTROL = 17)"
    },

    "Step_SelectAllPath_PressA": {
        "recognition": "DirectHit",
        "action": "ClickKey",
        "key": 65,
        "post_delay": 50,
        "next": [
            "Step_SelectAllPath_KeyUp"
        ],
        "$comment": "在 Ctrl 按下状态下按 A 键 (VK_A = 65)，实现 Ctrl+A 全选"
    },

    "Step_SelectAllPath_KeyUp": {
        "recognition": "DirectHit",
        "action": "KeyUp",
        "key": 17,
        "post_delay": 200,
        "next": [
            "Step_InputInstallPath"
        ],
        "$comment": "松开 Ctrl 键，全选完成，链接到输入新路径"
    },

    "$comment_input_path": "=== 输入自定义安装路径 ===",

    "Step_InputInstallPath": {
        "recognition": "DirectHit",
        "action": "Custom",
        "custom_action": "TypeInstallPath",
        "custom_action_param": {"input_text":"","select_all_before":true,"char_delay_ms":10},
        "post_delay": 500,
        "$comment": "输入自定义安装路径。input_text 由 override 动态注入到 custom_action_param 中"
    },

    "$comment_next": "=== 点击 下一步 / 继续 ===",

    "Step_ClickNext": {
        "recognition": "OCR",
        "expected": [
            "下一步", "Next", "继续", "Continue", "下一步(N)>", "Next >", "Next>"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 1000,
        "max_hit": 20
    },

    "$comment_install": "=== 点击 安装 按钮 ===",

    "Step_ClickInstall": {
        "recognition": "OCR",
        "expected": [
            "安装", "Install", "快速安装", "立即安装", "开始安装", "一键安装",
            "Quick Install", "Start Install", "Install Now"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 1000,
        "max_hit": 3,
        "next": ["Step_WaitProgress"],
        "$comment": "点击安装后串行进入进度检测，避免 DirectHit 的 WaitProgress 在并行扫描中抢先执行"
    },

    "$comment_progress": "=== 等待安装进度（基于帧差法检测界面是否在变化）===",

    "Step_WaitProgress": {
        "recognition": "DirectHit",
        "action": "Custom",
        "custom_action": "DetectProgress",
        "custom_action_param": {
            "roi": [0, 0, 0, 0],
            "sample_interval_ms": 3000,
            "min_change_ratio": 0.005,
            "max_change_ratio": 0.30,
            "stall_timeout_ms": 15000,
            "per_pixel_threshold": 30
        },
        "post_delay": 1000,
        "max_hit": 100,
        "on_error": ["StepDispatcher"],
        "$comment": "通过帧差法检测进度：截取两帧对比像素变化。action 返回 false 时通过 on_error 回到 StepDispatcher 继续扫描，而非终止 pipeline"
    },

    "$comment_finish": "=== 识别完成按钮：结束循环 ===",

    "Step_Finish": {
        "recognition": "OCR",
        "expected": [
            "完成", "Finish", "完成安装", "关闭", "Close",
            "退出", "Exit", "Done", "结束", "安装完成", "安装成功",
            "Installation Complete", "Setup Complete"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "action": "Click",
        "pre_delay": 500,
        "post_delay": 1000,
        "next": [
            "InstallDone"
        ]
    },

    "$comment_done": "=== 安装完成终点节点 ===",

    "InstallDone": {
        "recognition": "DirectHit",
        "action": "DoNothing"
    },

    "$comment_error": "=== 异常处理节点 ===",

    "HandleTimeout": {
        "recognition": "DirectHit",
        "action": "DoNothing",
        "next": [
            "HandleUACPopup",
            "HandleSystemDialog",
            "HandleRetryOrAbort"
        ]
    },

    "HandleUACPopup": {
        "recognition": "OCR",
        "expected": [
            "用户账户控制", "User Account Control",
            "你要允许", "Do you want to allow",
            "管理员", "Administrator"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.5,
        "timeout": 3000,
        "action": "DoNothing",
        "$comment": "检测到 UAC 弹窗时不操作（需要用户手动确认或已通过管理员启动）"
    },

    "HandleSystemDialog": {
        "recognition": "OCR",
        "expected": [
            "Windows 防火墙", "Windows Firewall",
            ".NET Framework", "Visual C++",
            "Windows Defender", "Microsoft Defender",
            "允许访问", "Allow access"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.5,
        "timeout": 3000,
        "action": "Click",
        "pre_delay": 500,
        "post_delay": 1000,
        "on_error": ["HandleRetryOrAbort"],
        "$comment": "检测到系统弹窗（防火墙等）时点击允许/确定"
    },

    "HandleRetryOrAbort": {
        "recognition": "OCR",
        "expected": [
            "重试", "Retry", "确定", "OK", "是", "Yes"
        ],
        "roi": [0, 0, 0, 0],
        "threshold": 0.6,
        "timeout": 5000,
        "action": "Click",
        "pre_delay": 300,
        "post_delay": 1000,
        "on_error": [
            "InstallDone"
        ]
    }
}
