package com.heyu.api.controller.face;


import com.aliyun.facebody20191230.models.PedestrianDetectAttributeResponse;
import com.aliyun.facebody20191230.models.PedestrianDetectAttributeResponseBody;
import com.heyu.api.alibaba.handle.facebody.APedestrianDetectAttributeHandle;
import com.heyu.api.alibaba.request.facebody.APedestrianDetectAttributeRequest;
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.PedestrianDetectAttributeReq;
import com.heyu.api.resp.face.PedestrianDetectAttributeResp;
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/PedestrianDetectAttribute?RegionId=cn-shanghai
 *人体结构化属性
 *
 * PedestrianDetectAttribute
 *
 *
 * 功能描述
 * 人体结构化属性能力可以检测图片中人体的属性，具体功能包括人体检测以及属性预估。例如：性别、年龄、朝向、帽子、眼镜、包、衣服、颜色等。
 *
 */
@Slf4j
@RestController
@RequestMapping("/face")
@NotIntercept
public class PedestrianDetectAttributeController extends BaseController {


    @Autowired
    private APedestrianDetectAttributeHandle aPedestrianDetectAttributeHandle;


    @RequestMapping("/attribute")
    @CacheResult
    public R attribute(PedestrianDetectAttributeReq req) {
        PedestrianDetectAttributeResp resp = new PedestrianDetectAttributeResp();

        APedestrianDetectAttributeRequest request = new APedestrianDetectAttributeRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        ApiR<PedestrianDetectAttributeResponse> aR = aPedestrianDetectAttributeHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyData responseBodyData = aR.getData().getBody().getData();
            List<PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributes> attributes = responseBodyData.getAttributes();
            resp.setWidth(responseBodyData.getWidth());
            resp.setHeight(responseBodyData.getHeight());
            resp.setPersonNumber(responseBodyData.getPersonNumber());
            List<PedestrianDetectAttributeResp.AttributesDTO> attributesDTOList = new ArrayList<>();
            List<PedestrianDetectAttributeResp.BoxesDTO> boxesDTOList = new ArrayList<>();


            if (CollectionUtils.isNotEmpty(attributes)) {
                for (PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributes attribute : attributes) {
                    PedestrianDetectAttributeResp.AttributesDTO attributeDTO = new PedestrianDetectAttributeResp.AttributesDTO();

                    PedestrianDetectAttributeResp.AttributesDTO.LowerWearDTO lowerWear = new PedestrianDetectAttributeResp.AttributesDTO.LowerWearDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesLowerWear attributesLowerWear = attribute.getLowerWear();
                    lowerWear.setName(attributesLowerWear.getName());
                    lowerWear.setScore(attributesLowerWear.getScore());

                    PedestrianDetectAttributeResp.AttributesDTO.BackpackDTO backpack = new PedestrianDetectAttributeResp.AttributesDTO.BackpackDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesBackpack attributeBackpack = attribute.getBackpack();
                    backpack.setName(attributeBackpack.getName());
                    backpack.setScore(attributeBackpack.getScore());


                    PedestrianDetectAttributeResp.AttributesDTO.OrientDTO orient = new PedestrianDetectAttributeResp.AttributesDTO.OrientDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesOrient attributesOrient = attribute.getOrient();
                    orient.setName(attributesOrient.getName());
                    orient.setScore(attributesOrient.getScore());


                    PedestrianDetectAttributeResp.AttributesDTO.ShoulderBagDTO shoulderBag = new PedestrianDetectAttributeResp.AttributesDTO.ShoulderBagDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesShoulderBag attributesShoulderBag = attribute.getShoulderBag();
                    shoulderBag.setName(attributesShoulderBag.getName());
                    shoulderBag.setScore(attributesShoulderBag.getScore());

                    PedestrianDetectAttributeResp.AttributesDTO.LowerColorDTO lowerColor = new PedestrianDetectAttributeResp.AttributesDTO.LowerColorDTO();

                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesLowerColor attributesLowerColor = attribute.getLowerColor();
                    lowerColor.setName(attributesLowerColor.getName());
                    lowerColor.setScore(attributesLowerColor.getScore());


                    PedestrianDetectAttributeResp.AttributesDTO.GlassesDTO glasses = new PedestrianDetectAttributeResp.AttributesDTO.GlassesDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesGlasses attributesGlasses = attribute.getGlasses();
                    glasses.setName(attributesGlasses.getName());
                    glasses.setScore(attributesGlasses.getScore());


                    PedestrianDetectAttributeResp.AttributesDTO.UpperColorDTO upperColor = new PedestrianDetectAttributeResp.AttributesDTO.UpperColorDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesUpperColor attributesUpperColor = attribute.getUpperColor();
                    upperColor.setName(attributesUpperColor.getName());
                    upperColor.setScore(attributesUpperColor.getScore());

                    PedestrianDetectAttributeResp.AttributesDTO.HatDTO hat = new PedestrianDetectAttributeResp.AttributesDTO.HatDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesHat attributesHat = attribute.getHat();
                    hat.setName(attributesHat.getName());
                    hat.setScore(attributesHat.getScore());


                    PedestrianDetectAttributeResp.AttributesDTO.HandbagDTO handbag = new PedestrianDetectAttributeResp.AttributesDTO.HandbagDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesHandbag attributesHandbag = attribute.getHandbag();
                    handbag.setName(attributesHandbag.getName());
                    handbag.setScore(attributesHandbag.getScore());

                    PedestrianDetectAttributeResp.AttributesDTO.GenderDTO gender = new PedestrianDetectAttributeResp.AttributesDTO.GenderDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesGender attributesGender = attribute.getGender();
                    gender.setName(attributesGender.getName());
                    gender.setScore(attributesGender.getScore());

                    PedestrianDetectAttributeResp.AttributesDTO.AgeDTO age = new PedestrianDetectAttributeResp.AttributesDTO.AgeDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesAge attributesAge = attribute.getAge();
                    age.setName(attributesAge.getName());
                    age.setScore(attributesAge.getScore());

                    PedestrianDetectAttributeResp.AttributesDTO.UpperWearDTO upperWear = new PedestrianDetectAttributeResp.AttributesDTO.UpperWearDTO();
                    PedestrianDetectAttributeResponseBody.PedestrianDetectAttributeResponseBodyDataAttributesUpperWear attributesUpperWear = attribute.getUpperWear();
                    upperWear.setName(attributesUpperWear.getName());
                    upperWear.setScore(attributesUpperWear.getScore());


                    attributeDTO.setLowerWear(lowerWear);
                    attributeDTO.setBackpack(backpack);
                    attributeDTO.setOrient(orient);
                    attributeDTO.setShoulderBag(shoulderBag);
                    attributeDTO.setLowerColor(lowerColor);
                    attributeDTO.setGlasses(glasses);
                    attributeDTO.setUpperColor(upperColor);
                    attributeDTO.setHat(hat);
                    attributeDTO.setHandbag(handbag);
                    attributeDTO.setGender(gender);
                    attributeDTO.setAge(age);
                    attributeDTO.setUpperWear(upperWear);

                    attributesDTOList.add(attributeDTO);


                }

            }

            resp.setAttributes(attributesDTOList);
            resp.setBoxes(boxesDTOList);


        }


        return R.error();
    }

}
