package com.fs.aicall.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.aicall.params.AiobChangeTashStatusParam;
import com.fs.aicall.params.AiobImportPhoneParam;
import com.fs.aicall.params.RobotParams;
import com.fs.aicall.vo.AiobBaiduTaskVo;
import com.fs.aicall.vo.AiobImportPhoneVo;
import com.fs.aicall.vo.RobotVo;
import com.fs.aiob.domain.AiobBaiduTask;
import com.fs.common.exception.base.BaseException;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
@AllArgsConstructor
public class AiobApiUtils {
    private static String ACCESS_KEY = "c91145dd1b474ea58d34cdd1d6065b30";
    private static String SECRET_KEY = "3f417df5795b4bb09c21a47c688d0c7f";

    private static final RestTemplate restTemplate = new RestTemplate();
    private final AiobTokenUtils aiobTokenUtils;

    public void createTask(AiobBaiduTask task) {
        String url = "https://aiob-open.baidu.com/api/v3/console/apitask/create";
        AiobBaiduTaskVo aiobBaiduTaskVo = new AiobBaiduTaskVo();
        BeanUtils.copyProperties(task, aiobBaiduTaskVo);
        if(task.getForbidDialDate() != null && !task.getForbidDialDate().isEmpty()){
            aiobBaiduTaskVo.setForbidDialDate(Arrays.stream(task.getForbidDialDate().split(",")).map(Integer::parseInt).collect(Collectors.toList()));
        }
        JSONObject send = send(HttpMethod.POST, ACCESS_KEY, SECRET_KEY, url, aiobBaiduTaskVo);
        task.setTaskId(send.getString("taskId"));
    }
    public List<AiobImportPhoneVo> importPhone(AiobImportPhoneParam vo) {
        String url = "https://aiob-open.baidu.com/api/v3/console/apitask/import";
        JSONObject send = send(HttpMethod.POST, ACCESS_KEY, SECRET_KEY, url, vo);
        return send.getJSONArray("resList").toJavaList(AiobImportPhoneVo.class);
    }
    public List<RobotVo> getRobotList() {
        String url = "https://aiob-open.baidu.com/api/v1/robot/list";
        JSONObject result = send(HttpMethod.GET, ACCESS_KEY, SECRET_KEY, url, RobotParams.builder().pn(1).ps(100).build());
        return result.getJSONArray("list").toJavaList(RobotVo.class);
    }
    public void changeStatus(String taskId, Integer status) {
        String url = "https://aiob-open.baidu.com/api/v3/console/apitask/task/status/update";
        send(HttpMethod.POST, ACCESS_KEY, SECRET_KEY, url, AiobChangeTashStatusParam.builder().taskId(taskId).taskStatus(status).build());
    }

    public void deleteTask(List<String> taskIds) {
        taskIds.forEach(this::deleteTask);
    }
    public void deleteTask(String taskId) {
        String url = "https://aiob-open.baidu.com/api/v3/console/apitask/delete";
        Map<String, String> param = new HashMap<>();
        param.put("taskId", taskId);
        send(HttpMethod.POST, ACCESS_KEY, SECRET_KEY, url, param);
    }


    public  <T> JSONObject send(HttpMethod type, String accessKey, String secretKey, String url, T t) {
        String token = aiobTokenUtils.getToken(accessKey, secretKey);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;charset:utf-8;");  // 添加认证头
        headers.add("Authorization", token);
        // 2. 创建 HttpEntity 对象，将请求头放入其中
        HttpEntity<String> entity = new HttpEntity<>(JSONObject.toJSONString(t), headers);
        // 3. 使用 RestTemplate 发送请求
        ResponseEntity<String> response = restTemplate.exchange(url, type, entity, String.class);
        JSONObject jsonObject = JSON.parseObject(response.getBody());
        if(jsonObject.getInteger("code") != 200){
            throw new BaseException(jsonObject.getString("msg"));
        }
        return jsonObject.getJSONObject("data");
    }


//    public static void main(String[] args) {
////        System.out.println(getRobotList());
//        AiobBaiduTask task = new AiobBaiduTask();
//        task.setTaskName("测试接口创建");
//        task.setRobotId("5114c2c9-70f9-45e5-b637-15eb31b10c8d");
//        task.setDialStartDate(LocalDate.now());
//        task.setDialStartTime(LocalTime.of(9, 0));
//        task.setDialEndTime(LocalTime.of(20, 0));
//        task.setIsOpenEmptyNum(false);
//        task.setIsOpenPhoneDown(false);
//        task.setIsOpenRepeatFilter(true);
//        createTask(task);
//        System.out.println(task);
//    }
}
