package com.heyu.api.controller.video;


import com.aliyun.ocr20191230.models.RecognizeVideoCharacterResponse;
import com.aliyun.ocr20191230.models.RecognizeVideoCharacterResponseBody;
import com.heyu.api.alibaba.handle.common.text.ARecognizeVideoCharacterHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeVideoCharacterRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.request.video.RecognizeVideoCharacterReq;
import com.heyu.api.resp.video.RecognizeVideoCharacterResp;
import com.heyu.api.resp.video.RecognizeVideoCharacterTextResp;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@Slf4j
@RestController
@RequestMapping("/video/character")
@NotIntercept
public class RecognizeVideoCharacterController extends BaseController {


    @Autowired
    private ARecognizeVideoCharacterHandle arRecognizeVideoCharacterHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(RecognizeVideoCharacterReq request) {
        List<RecognizeVideoCharacterResp> resp = new ArrayList<>();
        ARecognizeVideoCharacterRequest aRecognizeVideoCharacterRequest = new ARecognizeVideoCharacterRequest();

        aRecognizeVideoCharacterRequest.setImageBase64(request.getVideoBase64());
        aRecognizeVideoCharacterRequest.setImageUrl(request.getVideoURL());
        ApiR<RecognizeVideoCharacterResponse> aR = arRecognizeVideoCharacterHandle.handle(aRecognizeVideoCharacterRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeVideoCharacterResponseBody.RecognizeVideoCharacterResponseBodyData responseBodyData = aR.getData().getBody().getData();
            List<RecognizeVideoCharacterResponseBody.RecognizeVideoCharacterResponseBodyDataFrames> framesList = responseBodyData.getFrames();
            for (RecognizeVideoCharacterResponseBody.RecognizeVideoCharacterResponseBodyDataFrames frames : framesList) {
                RecognizeVideoCharacterResp recognizeVideoCharacterResp = new RecognizeVideoCharacterResp();
                recognizeVideoCharacterResp.setTimestamp(frames.getTimestamp());
                List<RecognizeVideoCharacterTextResp> textResps = new ArrayList<>();
                for (RecognizeVideoCharacterResponseBody.RecognizeVideoCharacterResponseBodyDataFramesElements element : frames.getElements()) {
                    RecognizeVideoCharacterTextResp recognizeVideoCharacterTextResp = new RecognizeVideoCharacterTextResp();
                    recognizeVideoCharacterTextResp.setText(element.getText());
                    textResps.add(recognizeVideoCharacterTextResp);
                }
                recognizeVideoCharacterResp.setRecognizeVideoCharacterTextRespList(textResps);

                resp.add(recognizeVideoCharacterResp);
            }
            R.ok().setData(resp);
        }
        return R.error(aR.getErrorMsg());
    }
}