package com.pavis.admin.aigc.core.agent.tool;

import com.pavis.admin.aigc.model.entity.ToolDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.model.ToolContext;

import java.util.List;
import java.util.Map;

@Slf4j
public class BrowserUseTool implements ToolCallBiFunctionDef {

    /**
     * 计划ID
     */
    private Long planId;

    /**
     * 工具基类
     */
    private ToolDO tool;

    private Map<String, Object> envData;

    public BrowserUseTool(Long planId, ToolDO tool, Map<String, Object> envData) {
        this.tool = tool;
        this.planId = planId;
        this.envData = envData;
    }

    @Override
    public String getToolGroup() {
        return tool.getToolGroup() == null ? "default-tool-group" : tool.getToolGroup();
    }

    @Override
    public String getName() {
        return tool.getName();
    }

    @Override
    public String getDescription() {
        return tool.getDescription();
    }

    @Override
    public String getParameters() {
        return tool.getParameters();
    }

    @Override
    public Class<?> getInputType() {
        // 这边先简单处理一下，后续看一下是否需要做复杂处理
        return String.class;
    }

    @Override
    public boolean isReturnDirect() {
        return tool.getIsReturnDirect();
    }

    @Override
    public void setPlanId(Long planId) {
        this.planId = planId;
    }

    @Override
    public String getCurrentToolStateString() {
        // 构建URL和标题信息
        String urlInfo = String.format("\n   URL: %s\n   Title: %s", envData.get("url"), envData.get("title"));

        // 构建标签页信息
        @SuppressWarnings("unchecked") List<Map<String, Object>> tabs = (List<Map<String, Object>>)envData.get("tabs");
        String tabsInfo = (tabs != null) ? String.format("\n   %d tab(s) available", tabs.size()) : "";

        // 获取滚动信息
        @SuppressWarnings("unchecked") Map<String, Object> scrollInfo = (Map<String, Object>)envData.get("scroll_info");
        String contentAbove = "";
        String contentBelow = "";
        if (scrollInfo != null) {
            Long pixelsAbove = (Long)scrollInfo.get("pixels_above");
            Long pixelsBelow = (Long)scrollInfo.get("pixels_below");
            contentAbove = pixelsAbove > 0 ? String.format(" (%d pixels)", pixelsAbove) : "";
            contentBelow = pixelsBelow > 0 ? String.format(" (%d pixels)", pixelsBelow) : "";
        }

        // 获取交互元素信息
        String elementsInfo = (String)envData.get("interactive_elements");

        // 构建最终的状态字符串
        String retString = String.format("""
            When you see [Current state starts here], focus on the following:
            - Current URL and page title:
            %s

            - Available tabs:
            %s

            - Interactive elements and their indices:
            %s

            - Content above%s or below%s the viewport (if indicated)

            - Any action results or errors:
            %s
            """, urlInfo, tabsInfo, elementsInfo != null ? elementsInfo : "", contentAbove, contentBelow, envData
            .containsKey("error") ? envData.get("error") : "");

        return retString;
    }

    @Override
    public void cleanup(Long planId) {
        // 暂不处理
    }

    @Override
    public ToolExecuteResult apply(String s, ToolContext toolContext) {
        log.info("Browser Use toolInput: {}", s);
        return new ToolExecuteResult(s);
    }
}
