package com.heyu.api.controller.imageseg;


import com.aliyun.imageseg20191230.models.ParseFaceResponse;
import com.aliyun.imageseg20191230.models.ParseFaceResponseBody;
import com.heyu.api.alibaba.handle.imageseg.AParseFaceHandle;
import com.heyu.api.alibaba.request.imageseg.AParseFaceRequest;
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.imageseg.ParseFaceReq;
import com.heyu.api.resp.imageseg.ParseFaceDataElements;
import com.heyu.api.resp.imageseg.ParseFaceResp;
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/imageseg/2019-12-30/ParseFace?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 *
 *五官分割 ParseFace
 *
 *
 *功能描述
 * 五官分割能力可以检测输入的人脸正面图像，对眼睛、鼻子、嘴进行像素级语义分割。
 */
@Slf4j
@RestController
@RequestMapping("/face")
@NotIntercept
public class ParseFaceController extends BaseController {


    @Autowired
    private AParseFaceHandle parseFaceHandle;


    @RequestMapping("/parse")
    @CacheResult
    public R changeSky(ParseFaceReq request) {
        ParseFaceResp resp = new ParseFaceResp();
        AParseFaceRequest aParseFaceRequest = new AParseFaceRequest();
        aParseFaceRequest.setImageBase64(request.getImageBase64());
        aParseFaceRequest.setImageUrl(request.getImageUrl());

        ApiR<ParseFaceResponse> aR = parseFaceHandle.handle(aParseFaceRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            ParseFaceResponseBody.ParseFaceResponseBodyData responseBodyData = aR.getData().getBody().getData();

            resp.setOriginImageURL(responseBodyData.getOriginImageURL());
            List<ParseFaceDataElements> parseFaceDataElements = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(responseBodyData.getElements())) {
                for (ParseFaceResponseBody.ParseFaceResponseBodyDataElements element : responseBodyData.getElements()) {

                    ParseFaceDataElements elements = new ParseFaceDataElements();
                    elements.setName(element.getName());
                    elements.setImageURL(element.getImageURL());

                    parseFaceDataElements.add(elements);
                }
            }
            resp.setParseFaceDataElements(parseFaceDataElements);
            return R.ok().setData(resp);
        }
        return R.error();
    }
}
