package com.search.supersearch.model;

import cn.hutool.core.io.file.FileReader;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 喜欢番茄火锅
 * @date 2023/10/19 15:01
 */
@Data
public class OcrResult {
    @JsonProperty("code")
    private String code;
    @JsonProperty("success")
    private Boolean success;
    @JsonProperty("data")
    private String data;
    @JsonProperty("msg")
    private String msg;
    @JsonProperty("ext")
    private String ext;

    @SneakyThrows
    public static void main(String[] args) {
        FileReader fileReader = new FileReader("tmp.txt");
        String result = fileReader.readString();

        List<String> list = new ArrayList<>();
//        OcrResult ocrResult = JSONUtil.toBean(result, OcrResult.class);
        OcrResult.OcrResultData ocrResultData = JSONUtil.toBean(result, OcrResult.OcrResultData.class);
        List<OcrResult.OcrResultDataResult> ocrResultDataResults = JSONUtil.toList(ocrResultData.getResult(), OcrResult.OcrResultDataResult.class);
        for (OcrResult.OcrResultDataResult ocrResultDataResult : ocrResultDataResults) {
            List<OcrResult.OcrResultDataResultData> ocrResultDataResultData = JSONUtil.toList(ocrResultDataResult.getData(), OcrResult.OcrResultDataResultData.class);
            for (OcrResult.OcrResultDataResultData ocrResultDataResultDatum : ocrResultDataResultData) {
                list.add(ocrResultDataResultDatum.getText());
            }

        }


//        OcrResult ocrResult = JSONUtil.toBean(result, OcrResult.class);
//        OcrResultData ocrResultData = JSONUtil.toBean(ocrResult.getData(), OcrResultData.class);
//        List<OcrResultDataResult> ocrResultDataResults = JSONUtil.toList(ocrResultData.getResult(), OcrResultDataResult.class);
//        StringBuilder sb = new StringBuilder();
//        for (OcrResult.OcrResultDataResult ocrResultDataResult : ocrResultDataResults) {
//            List<OcrResult.OcrResultDataResultData> ocrResultDataResultData = JSONUtil.toList(ocrResultDataResult.getData(), OcrResult.OcrResultDataResultData.class);
//            for (OcrResult.OcrResultDataResultData ocrResultDataResultDatum : ocrResultDataResultData) {
//                sb.append(ocrResultDataResultDatum.getText());
//            }
//
//        }
        System.out.println(list);
    }


    @Data
    public class OcrResultData {
        @JsonProperty("status")
        private String status;
        @JsonProperty("base64")
        private String base64;
        @JsonProperty("result")
        private String result;
    }

    @Data
    public class OcrResultDataResult {
        @JsonProperty("data")
        private String data;
        @JsonProperty("page")
        private Integer page;
    }

    @Data
    public class OcrResultDataResultData {
        @JsonProperty("confidence")
        private String confidence;
        @JsonProperty("text")
        private String text;
        @JsonProperty("text_box_position")
        private String text_box_position;
    }

}
