package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.RecognizeActionResponse;
import com.aliyun.facebody20191230.models.RecognizeActionResponseBody;
import com.heyu.api.alibaba.handle.facebody.ARecognizeActionHandle;
import com.heyu.api.alibaba.request.ACommonTextRequest;
import com.heyu.api.alibaba.request.facebody.ARecognizeActionRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.request.CommonReq;
import com.heyu.api.request.face.RecognizeImageActionReq;
import com.heyu.api.resp.face.RecognizeImageActionResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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;

/***
 * https://next.api.aliyun.com/api/facebody/2019-12-30/RecognizeAction?RegionId=cn-shanghai
 *
 *
 * 动作行为识别 RecognizeAction
 *
 */
@Slf4j
@RestController
@RequestMapping("/action/image")
@NotIntercept
public class RecognizeImageActionController extends BaseController {


    @Autowired
    private ARecognizeActionHandle aRecognizeActionHandle;


    @RequestMapping("/recognize")
    public R recognize(RecognizeImageActionReq req) {

        ARecognizeActionRequest actionRequest = new ARecognizeActionRequest();
        List<ACommonTextRequest> imageUrlList = new ArrayList<>();

        List<CommonReq> imageUrls = req.getImageUrls();
        for (CommonReq imageReq : imageUrls) {
            ACommonTextRequest commonTextRequest = new ACommonTextRequest();
            commonTextRequest.setImageUrl(imageReq.getImageUrl());
            commonTextRequest.setImageBase64(imageReq.getImageBase64());
            imageUrlList.add(commonTextRequest);
        }
        actionRequest.setImageUrlList(imageUrlList);


        List<RecognizeImageActionResp> resps = new ArrayList<>();

        ApiR<RecognizeActionResponse> aR = aRecognizeActionHandle.handle(actionRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeActionResponseBody.RecognizeActionResponseBodyData responseBodyData = aR.getData().getBody().getData();
            List<RecognizeActionResponseBody.RecognizeActionResponseBodyDataElements> elements = responseBodyData.getElements();
            if (CollectionUtils.isNotEmpty(elements)) {
                for (RecognizeActionResponseBody.RecognizeActionResponseBodyDataElements element : elements) {
                    RecognizeImageActionResp actionResp = new RecognizeImageActionResp();

                    actionResp.setLabels(element.getLabels());
                    actionResp.setScores(element.getScores());
                    resps.add(actionResp);
                }
            }
            return R.ok().setData(resps);
        }

        return R.error(aR.getErrorMsg());
    }


}