package com.fs.aicall.service;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fs.aicall.config.AiCallConfig;
import com.fs.aicall.domain.BaseDomain;
import com.fs.aicall.domain.TaskInfo;
import com.fs.aicall.domain.param.CalltaskcreateaiCustomizeDomain;
import com.fs.aicall.domain.param.EditDialogDomain;
import com.fs.aicall.domain.param.getDialogMapDomain;
import com.fs.aicall.domain.result.CalltaskcreateaiCustomizeResult;
import com.fs.aicall.domain.result.EditDialogResult;
import com.fs.aicall.domain.result.GetairobotResult;
import com.fs.aicall.domain.result.QueryCallTaskInfoResult;
import com.fs.aicall.utils.AiCallUtils;
import com.fs.company.domain.CompanyVoiceApi;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;

public class AiCallService {


    private final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
    private final AiCallUtils utils;
    public AiCallService(AiCallUtils utils) {
        this.utils = utils;
    }
    public static Builder builder(){
        return new Builder();
    }
    public static class Builder {

        private  AiCallUtils utils;

        public Builder config(CompanyVoiceApi api) {
            JSONObject jsonObject = JSONObject.parseObject(api.getApiJson());
            AiCallConfig config = new AiCallConfig();
            config.setCustomer(jsonObject.getString("account"));
            config.setPassword(jsonObject.getString("password"));
            config.setUrl(jsonObject.getString("url"));
            config.setDialogUrl(jsonObject.getString("dialogUrl"));
            this.utils = new AiCallUtils(config);
            return this;
        }

        public AiCallService build() {
            return new AiCallService(this.utils);
        }
    }

    public String getDialogUrl(){
        return utils.getDialogUrl();
    }


    private <T extends BaseDomain> Function<JSONObject, T> getObj(Class<T> clazz){
        return getObj("data", clazz);
    }
    private <T extends BaseDomain> Function<JSONObject, List<T>> getList(Class<T> clazz){
        return getList("list", clazz);
    }
    private <T extends BaseDomain> Function<JSONObject, T> getObj(String attr, Class<T> clazz){
        return e -> {
            T t = e.getJSONObject(attr).toJavaObject(clazz);
            t.setUserData(e.getString("userData"));
            t.setSeq(e.getString("seq"));
            return t;
        };
    }
    private <T extends BaseDomain> Function<JSONObject, List<T>> getList(String attr, Class<T> clazz){
        return e -> {
            List<T> list = e.getJSONArray(attr).toJavaList(clazz);
            String userData = e.getString("userData");
            String seq = e.getString("seq");
            list.forEach(t -> {
                t.setUserData(userData);
                t.setSeq(seq);
            });
            return list;
        };
    }

    /**
     * 登录
     */
    public String getToken(){
        return utils.send("login", e -> e.getString("token"));
    }

    /**
     * 获取机器人列表
     */
    public List<GetairobotResult> getairobotlist(){
        return utils.send("getairobotlist", getList(GetairobotResult.class));
    }

    /**
     * 获取话术列表
     */
    public List<EditDialogResult> queryDialog(){
        return utils.send("queryDialog", getList("data", EditDialogResult.class));
    }
    /**
     * 获取话术详情
     */
    public EditDialogResult getDialog(){
        return utils.send("getDialog", getObj(EditDialogResult.class));
    }

    /**
     * 修改/添加话术
     * @param param 话术参数
     */
    public EditDialogResult editDialog(EditDialogDomain param){
        return utils.send("editDialog", OBJECT_MAPPER.valueToTree(param), getObj(EditDialogResult.class));
    }

    /**
     * 创建机器人外呼任务
     * @param param 参数
     */
    public CalltaskcreateaiCustomizeResult calltaskcreateaiCustomize(CalltaskcreateaiCustomizeDomain param){
        return utils.send("calltaskcreateaiCustomize", OBJECT_MAPPER.valueToTree(param), getObj(CalltaskcreateaiCustomizeResult.class));
    }
    /**
     * 查询外呼任务状态信息
     * @param param 参数
     */
    public QueryCallTaskInfoResult queryCallTaskInfo(TaskInfo param){
        return utils.send("queryCallTaskInfo", OBJECT_MAPPER.valueToTree(param), getObj(QueryCallTaskInfoResult.class));
    }
    /**
     * 启动外呼任务
     * @param param 参数
     */
    public TaskInfo startCallTask(TaskInfo param){
        return utils.send("startCallTask", OBJECT_MAPPER.valueToTree(param), getObj(TaskInfo.class));
    }
    /**
     * 停止外呼任务
     * @param param 参数
     */
    public TaskInfo stopCallTask(TaskInfo param){
        return utils.send("stopCallTask", OBJECT_MAPPER.valueToTree(param), getObj(TaskInfo.class));
    }
    /**
     * 对话图查询（uuid）
     * @param param 参数
     */
    public TaskInfo getDialogMap(getDialogMapDomain param){
        return utils.send("getDialogMap", OBJECT_MAPPER.valueToTree(param), getObj("telData", TaskInfo.class));
    }
}
