package com.search.supersearch.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.search.supersearch.common.R;
import com.search.supersearch.service.OcrService;
import com.search.supersearch.util.HttpUtils;
import io.swagger.annotations.Api;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test/ocr")
@Slf4j
@Api(value = "ocr 接口测试", tags = "ocr 接口测试")
public class OcrController {
    private static final String serverUrl = "http://10.104.32.32:8000";
    private static final String projectId = "29";
    private static final String ocrUrl = serverUrl + "/v2/review/projects/" + projectId + "/o/ocr";
    private static final String tokenUrl = serverUrl + "/o/token/";

    @Resource
    private OcrService ocrService;

    @GetMapping("/ocr")
    @SneakyThrows
    public R<?> ocr(String accessToken, String filePath, String format) {
        log.info("方法名:ocr,参数accessToken:{},参数filePath:{}", accessToken, filePath);
        Map<String, String> parameters = new HashMap<>();
        parameters.put("format", format);
        Map<String, File> files = new HashMap<>();
        files.put("file", new File(filePath));
        log.info("识别接口参数,ocrUrl:{},parameters:{},files:{},accessToken:{}", ocrUrl, parameters, files, accessToken);
        String r = HttpUtils.post(ocrUrl, parameters, files, accessToken);
        log.info("识别接口返回值,{}", r);
        if (r.contains("身份认证信息未提供")) {
            log.info("token已过期");
            Map<String, Object> toeknParameters = new HashMap<>();
            toeknParameters.put("client_id", "UZ81v8EYVWSckt787x0eQvIyTXVVGx4RHKVQ5riZ");
            toeknParameters.put("client_secret", "cZXylKpqgLoVHFSIeKZWJlTMBixQDN6n2HPvpeX2VFLPZqkt66PnSFtmA3Ad7OKi3ZIuY1rFjmFVd9DPYqj3WKiHcEQhcCNfrNllaUMPMnHTlN7qyiln2f15lepzRW31");
            toeknParameters.put("grant_type", "client_credentials");
            toeknParameters.put("scope", "review");

            String post = HttpUtil.post(tokenUrl, toeknParameters);
            log.info("获取 token接口返回值,{}", post);
            JSONObject entries = JSONUtil.parseObj(post);
            accessToken = (String) entries.get("access_token");
            r = HttpUtils.post(ocrUrl, parameters, files, accessToken);
        }
        return R.data(r);
    }

    @GetMapping("/getToken")
    @SneakyThrows
    public R<?> getToken() {

        Map<String, Object> toeknParameters = new HashMap<>();
        toeknParameters.put("client_id", "UZ81v8EYVWSckt787x0eQvIyTXVVGx4RHKVQ5riZ");
        toeknParameters.put("client_secret", "cZXylKpqgLoVHFSIeKZWJlTMBixQDN6n2HPvpeX2VFLPZqkt66PnSFtmA3Ad7OKi3ZIuY1rFjmFVd9DPYqj3WKiHcEQhcCNfrNllaUMPMnHTlN7qyiln2f15lepzRW31");
        toeknParameters.put("grant_type", "client_credentials");
        toeknParameters.put("scope", "review");

        String post = HttpUtil.post(tokenUrl, toeknParameters);
        log.info("获取 token接口返回值,{}", post);
        JSONObject entries = JSONUtil.parseObj(post);
        String accessToken = (String) entries.get("access_token");

        return R.data(accessToken);
    }

    @PostMapping("/ocrFile")
    @SneakyThrows
    public R<?> ocrFile(MultipartFile file, String format) {
        log.info("MultipartFile={},format={}", file, format);
        if (file == null) {
            return R.fail("file不可为空!");
        }
        List<String> ocr = ocrService.ocr(file, format);
        return R.data(ocr);
    }
}
