package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.BodyPostureResponse;
import com.aliyun.facebody20191230.models.BodyPostureResponseBody;
import com.heyu.api.alibaba.handle.facebody.ABodyPostureHandle;
import com.heyu.api.alibaba.request.facebody.ABodyPostureRequest;
import com.heyu.api.controller.BaseController;
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 com.heyu.api.request.face.BodyPostureReq;
import com.heyu.api.resp.face.BodyPostureBodiesResp;
import com.heyu.api.resp.face.BodyPostureResp;
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/BodyPosture?RegionId=cn-shanghai&tab=DEBUG&lang=JAVA
 *人体姿态关键点 BodyPosture
 *
 *
 * 人体姿态关键点能力可以获取人体的十八个关键点信息。包括：nose、neck、right_shoulder、right_elbow、right_wrist、left_shoulder、left_elbow、left_wrist、right_hip、right_knee、right_ankle、left_hip、left_knee、left_ankle、right_eye、left_eye、right_ear、left_ear。
 */
@Slf4j
@RestController
@RequestMapping("/body")
@NotIntercept
public class BodyPostureController extends BaseController {

    @Autowired
    private ABodyPostureHandle aBodyPostureHandle;


    @RequestMapping("/posture")
    @CacheResult
    public R mask(BodyPostureReq request) {
        List<BodyPostureResp> resp = new ArrayList<>();


        ABodyPostureRequest bodyPostureRequest = new ABodyPostureRequest();
        bodyPostureRequest.setImageBase64(request.getImageBase64());
        bodyPostureRequest.setImageUrl(request.getImageUrl());

        ApiR<BodyPostureResponse> apiR = aBodyPostureHandle.handle(bodyPostureRequest);
        if (apiR.isSuccess() && isSuccessStatusCode(apiR.getData().getStatusCode())) {
            BodyPostureResponseBody responseBody = apiR.getData().getBody();
            BodyPostureResponseBody.BodyPostureResponseBodyData responseBodyData = responseBody.getData();

            List<BodyPostureResponseBody.BodyPostureResponseBodyDataOutputs> responseBodyDataOutputsList = responseBodyData.getOutputs();
            if (CollectionUtils.isNotEmpty(responseBodyDataOutputsList)) {
                for (BodyPostureResponseBody.BodyPostureResponseBodyDataOutputs bodyPostureResponseBodyDataOutputs : responseBodyDataOutputsList) {
                    BodyPostureResp bodyPostureResp = new BodyPostureResp();
                    bodyPostureResp.setHumanCount(bodyPostureResponseBodyDataOutputs.getHumanCount());
                    List<BodyPostureResponseBody.BodyPostureResponseBodyDataOutputsResults> results = bodyPostureResponseBodyDataOutputs.getResults();
                    if (CollectionUtils.isNotEmpty(results)) {

                        List<BodyPostureBodiesResp> bodyPostureBodiesResps = new ArrayList<>();
                        for (BodyPostureResponseBody.BodyPostureResponseBodyDataOutputsResults result : results) {
                            if (CollectionUtils.isNotEmpty(result.getBodies())) {
                                for (BodyPostureResponseBody.BodyPostureResponseBodyDataOutputsResultsBodies body : result.getBodies()) {

                                    BodyPostureBodiesResp bodyPostureBodiesResp = new BodyPostureBodiesResp();
                                    bodyPostureBodiesResp.setConfident(body.getConfident());
                                    bodyPostureBodiesResp.setLabel(body.getLabel());
                                    bodyPostureBodiesResps.add(bodyPostureBodiesResp);

                                }

                            }
                        }
                    }
                }

                return R.ok().setData(resp);
            }
        }

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

}
