package com.rj.audio;

import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.rj.entity.FaceDetectLog;
import com.rj.service.IFaceDetectLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
import java.time.LocalDateTime;
import java.util.UUID;

@Component
public class FaceDetectTest {
    
    private static final String API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/face-detect";
    private static final String API_KEY = System.getenv("DASHSCOPE_API_KEY");
    
    @Autowired
    private IFaceDetectLogService faceDetectLogService;
    
    public static void main(String[] args) {
        if (API_KEY == null || API_KEY.trim().isEmpty()) {
            System.err.println("请设置DASHSCOPE_API_KEY环境变量");
            return;
        }
        String imageUrl = "http://101.35.52.237:19006/api/v1/download-shared-object/aHR0cDovLzEyNy4wLjAuMTo5MDA1L3NyYy1iYXNlLWF1ZGlvLXZpZGVvLyVFNSVCRSVBRSVFNCVCRiVBMSVFNSU5QiVCRSVFNyU4OSU4N18yMDI1MTAwNTEyMDIwNV81Nl80OS5qcGc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD0wRUE4RlhHQUlTN1FQSk9VQUZIOSUyRjIwMjUxMDA1JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI1MTAwNVQwNDE3NDVaJlgtQW16LUV4cGlyZXM9NDMyMDAmWC1BbXotU2VjdXJpdHktVG9rZW49ZXlKaGJHY2lPaUpJVXpVeE1pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmhZMk5sYzNOTFpYa2lPaUl3UlVFNFJsaEhRVWxUTjFGUVNrOVZRVVpJT1NJc0ltVjRjQ0k2TVRjMU9UWTNPVGN4TkN3aWNHRnlaVzUwSWpvaWJXbHVhVzloWkcxcGJpSjkuLWhSOFRobUpXUFNQdGVQM2V4TzdITmlkUHQ1RXBDeWRJMVV2cG0yQXJxczJsR0hBcnBTeEZWZEdJY1ZOUHdKNlF2QUZjbUJQOWQtQmtFampyUXpGaGcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JnZlcnNpb25JZD1udWxsJlgtQW16LVNpZ25hdHVyZT1mN2NjODUxNWFiYzgxZDFlNDZmNjg1YzBkMzAxMzM0MjAyZjM5NmIxODMzMzY4MGM0OWE3ZGYxYmQ5ODk5ZWM4";
        try {
            // 创建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "liveportrait-detect");
            
            Map<String, String> input = new HashMap<>();
            input.put("image_url",imageUrl);
            requestBody.put("input", input);
            
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.setBearerAuth(API_KEY);
            
            // 创建请求实体
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
            
            // 发送请求
            RestTemplate restTemplate = new RestTemplate();
            System.out.println("发送图像检测请求到: " + API_URL);
            System.out.println("请求参数: " + new ObjectMapper().writeValueAsString(requestBody));
            
            ResponseEntity<String> response = restTemplate.exchange(
                API_URL,
                HttpMethod.POST,
                requestEntity,
                String.class
            );
            
            // 处理响应
            System.out.println("响应状态码: " + response.getStatusCode());
            System.out.println("响应头: " + response.getHeaders());
            System.out.println("响应体: " + response.getBody());
            
            // 解析JSON响应
            if (response.getStatusCode() == HttpStatus.OK) {
                ObjectMapper mapper = new ObjectMapper();
                JsonNode jsonResponse = mapper.readTree(response.getBody());
                System.out.println("解析后的响应: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonResponse));
                
                // 检查是否有检测到图像
                if (jsonResponse.has("output") && jsonResponse.get("output").has("faces")) {
                    JsonNode faces = jsonResponse.get("output").get("faces");
                    System.out.println("检测到图像数量: " + faces.size());
                    for (int i = 0; i < faces.size(); i++) {
                        JsonNode face = faces.get(i);
                        System.out.println("图像 " + (i + 1) + ": " + face.toString());
                    }
                }
            }
            
        } catch (HttpClientErrorException e) {
            System.err.println("客户端错误 (4xx): " + e.getStatusCode());
            System.err.println("错误响应: " + e.getResponseBodyAsString());
            e.printStackTrace();
        } catch (HttpServerErrorException e) {
            System.err.println("服务器错误 (5xx): " + e.getStatusCode());
            System.err.println("错误响应: " + e.getResponseBodyAsString());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("图像检测请求失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}