#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""WiFi扫描Skill"""

import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(__file__))))

from skills.base_skill import BaseSkill
from network_tools import run_wifi_scan, run_wifi_password_view


class WiFiSkill(BaseSkill):
    """WiFi扫描Skill"""
    
    def get_metadata(self):
        return {
            "name": "wifi",
            "display_name": "WiFi管理",
            "description": (
                "扫描WiFi网络 或 查看已保存的WiFi密码。"
                "action=scan 扫描周围热点；action=password 显示本机所有已保存WiFi的密码。"
                "注意：skill名称固定为 wifi，不要使用 wifi_password 或 wifi_scan。"
            ),
            "category": "info",
            "keywords": ["wifi", "无线", "热点", "密码", "扫描", "wifi_password", "wifi_scan"],
            "parameters": [
                {
                    "name": "action",
                    "type": "string",
                    "required": False,
                    "default": "scan",
                    "description": "操作类型: scan(扫描WiFi) 或 password(查看已保存WiFi密码)"
                }
            ],
            "examples": [
                "扫描WiFi",
                "查看WiFi密码",
                "显示保存的WiFi密码",
                "wifi_password",
                "列出所有WiFi密码"
            ],
            "requires_admin": False,
            "requires_ui": False
        }
    
    def execute(self, action: str = "scan") -> str:
        """执行WiFi操作"""
        action = action.lower()
        
        if action in ["password", "密码", "查看密码"]:
            return run_wifi_password_view()
        else:
            return run_wifi_scan()
    
    def match_query(self, query: str) -> float:
        """增强的匹配逻辑"""
        query_lower = query.lower()

        # 精确匹配：wifi_password / wifi_scan 等别名直接打满分
        if query_lower in ("wifi_password", "wifi_scan", "wifi"):
            return 1.0

        # 密码相关（包含wifi或无线+密码）
        has_wifi  = any(k in query_lower for k in ["wifi", "无线", "热点", "wlan"])
        has_pwd   = "密码" in query_lower
        has_scan  = any(k in query_lower for k in ["扫描", "搜索", "查看", "列出", "显示"])

        if has_wifi and has_pwd:
            return 0.95
        if has_wifi and has_scan:
            return 0.90
        if has_wifi:
            return 0.85

        return super().match_query(query)
