package com.fs.aiTongueApi.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.fs.aiTongueApi.config.AiTongueConfig;
import com.fs.aiTongueApi.domain.AITongueResult;
import com.fs.aiTongueApi.domain.QueryAiTongue;
import com.fs.aiTongueApi.domain.QueryQuanXi;
import com.fs.aiTongueApi.domain.inner.ConfidenceData;
import com.fs.aiTongueApi.domain.inner.TongueData;
import com.fs.aiTongueApi.service.AiTongueService;
import com.fs.common.utils.uuid.UUID;
import org.apache.hc.core5.net.URIBuilder;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
@Service
public class AiTongueServiceImpl implements AiTongueService {

    @Override
    public AITongueResult<TongueData> getHistoryTongue(QueryAiTongue queryAiTongue) {
        queryAiTongue.setAppkey(AiTongueConfig.appKey);
        String result = sendPost(AiTongueConfig.getFaceHistoryByIDUrl, queryAiTongue);
        System.out.println(result);
        AITongueResult<TongueData> aiTongueResult = JSON.parseObject(result, new TypeReference<AITongueResult<TongueData>>(){});
        return aiTongueResult;
    }

    @Override
    public AITongueResult<ConfidenceData> checkTongue(String url) {
        String s="";
        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(AiTongueConfig.checkTongue);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            URL urlItem = new URL(url);
            InputStream inputStream =urlItem.openStream();
            // 添加文件或表单参数
            builder.addBinaryBody("file", inputStream, ContentType.DEFAULT_BINARY, "图片.jpg");

            builder.addTextBody("appkey", AiTongueConfig.appKey);
            HttpEntity multipart = builder.build();
            httpPost.setEntity(multipart);
            // 执行请求
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println(responseBody);
            s=responseBody;
        } catch (Exception e) {
            e.printStackTrace();
        }

        AITongueResult<ConfidenceData> aiTongueResult = JSON.parseObject(s, new TypeReference<AITongueResult<ConfidenceData>>(){});
        return aiTongueResult;
    }

    @Override
    public AITongueResult<TongueData> quanXiTongue(QueryQuanXi queryQuanXi) {
        String s="";
        try {
            HttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(AiTongueConfig.quanxiUrl);
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
            URL urlItem = new URL(queryQuanXi.getFile());
            InputStream inputStream =urlItem.openStream();
            // 添加文件或表单参数
            builder.addBinaryBody("file", inputStream, ContentType.DEFAULT_BINARY, UUID.randomUUID().toString()+".jpg");

            builder.addTextBody("age", queryQuanXi.getAge(), ContentType.create("text/plain", Charset.forName("UTF-8")));
            builder.addTextBody("male", queryQuanXi.getMale(), ContentType.create("text/plain", Charset.forName("UTF-8")));
            builder.addTextBody("appkey", AiTongueConfig.appKey);
            if (queryQuanXi.getIsYuejin()!=null){
                builder.addTextBody("isYuejin", queryQuanXi.getIsYuejin(), ContentType.create("text/plain", Charset.forName("UTF-8")));
            }
            if (queryQuanXi.getName()!=null){
                builder.addTextBody("name", queryQuanXi.getName(), ContentType.create("text/plain", Charset.forName("UTF-8")));
            }
            if (queryQuanXi.getDesease()!=null){
                builder.addTextBody("desease", queryQuanXi.getDesease(), ContentType.create("text/plain", Charset.forName("UTF-8")));
            }

            HttpEntity multipart = builder.build();
            httpPost.setEntity(multipart);

            // 执行请求
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println(responseBody);
            s=responseBody;
        } catch (Exception e) {
            e.printStackTrace();
        }

        AITongueResult<TongueData> aiTongueResult = JSON.parseObject(s, new TypeReference<AITongueResult<TongueData>>(){});
        return aiTongueResult;
    }


    public  String sendPost(String url,Object param){
        HttpClient httpClient = HttpClients.createDefault();
        try {
            URIBuilder builder = new URIBuilder(url);
            URI uri = builder.build();
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setHeader("Content-Type", "application/json"); // 设置Content-Type为"application/json"
            httpPost.setEntity(new StringEntity(JSON.toJSONString(param), StandardCharsets.UTF_8));
            HttpResponse response = httpClient.execute(httpPost);
            String reJson = EntityUtils.toString(response.getEntity());
            return reJson;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
