/*
 *  Copyright (c) 2023-2026, Easy-Agents (fuhai999@gmail.com).
 *  <p>
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  <p>
 *  http://www.apache.org/licenses/LICENSE-2.0
 *  <p>
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.easyagents.core.message;

import com.easyagents.core.util.Copyable;
import com.easyagents.core.util.Maps;
import com.alibaba.fastjson2.JSON;

import java.io.Serializable;
import java.util.Map;

public class ToolCall implements Serializable, Copyable<ToolCall> {

    private String id;
    private String name;
    private String arguments;

    public ToolCall() {
    }

    public ToolCall(String id, String name, String arguments) {
        this.id = id;
        this.name = name;
        this.arguments = arguments;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getArguments() {
        return arguments;
    }

    public void setArguments(String arguments) {
        this.arguments = arguments;
    }

    public Map<String, Object> getArgsMap() {
        if (arguments == null || arguments.isEmpty()) {
            return null;
        }

        String jsonStr = arguments.trim();

        try {
            return JSON.parseObject(jsonStr);
        } catch (Exception e) {
            if (jsonStr.contains("{") && jsonStr.contains("}")) {
                String json = jsonStr.substring(jsonStr.indexOf("{"), jsonStr.lastIndexOf("}") + 1);
                return JSON.parseObject(json);
            }

            if (!jsonStr.startsWith("{")) jsonStr = "{" + jsonStr;
            if (!jsonStr.endsWith("}")) jsonStr = jsonStr + "}";

            return JSON.parseObject(jsonStr);
        }
    }

    @Override
    public String toString() {
        return "ToolCall{" +
            "id='" + id + '\'' +
            ", name='" + name + '\'' +
            ", arguments='" + arguments + '\'' +
            '}';
    }

    public String toJsonString() {
        return Maps.of("id", id)
            .set("name", name)
            .set("arguments", arguments)
            .toJSON();
    }

    /**
     * 创建并返回当前对象的副本。
     *
     * @return 一个新的、内容相同但内存独立的对象
     */
    @Override
    public ToolCall copy() {
        ToolCall copy = new ToolCall();
        copy.id = this.id;
        copy.name = this.name;
        copy.arguments = this.arguments;
        return copy;
    }
}
