package com.heyu.api.controller.imagerecog;


import com.aliyun.imagerecog20190930.models.ClassifyingRubbishResponse;
import com.aliyun.imagerecog20190930.models.ClassifyingRubbishResponseBody;
import com.heyu.api.alibaba.handle.imagerecog.AClassifyingRubbishHandle;
import com.heyu.api.alibaba.request.imagerecog.AClassifyingRubbishRequest;
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.imagerecog.ClassifyingRubbishReq;
import com.heyu.api.resp.imagerecog.ClassifyingRubbishElements;
import com.heyu.api.resp.imagerecog.ClassifyingRubbishResp;
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/imagerecog/2019-09-30/ClassifyingRubbish?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 *
 * ClassifyingRubbish
 *
 * 垃圾分类识别ClassifyingRubbish的语法及示例
 *
 * 常见垃圾输出列举值
 * 湿垃圾： 八宝粥，冰激凌，冰糖葫芦，饼干，菠萝，菠萝蜜，菜根菜叶，残渣剩饭，
 *
 */
@Slf4j
@RestController
@RequestMapping("/pubbish")
@NotIntercept
public class ClassifyingRubbishController extends BaseController {

    @Autowired
    private AClassifyingRubbishHandle classifyingRubbishHandle;


    @RequestMapping("/classifying")
    @CacheResult
    public R classifying(ClassifyingRubbishReq req) {
        ClassifyingRubbishResp resp = new ClassifyingRubbishResp();
        AClassifyingRubbishRequest request = new AClassifyingRubbishRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        ApiR<ClassifyingRubbishResponse> aR = classifyingRubbishHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            ClassifyingRubbishResponseBody.ClassifyingRubbishResponseBodyData responseBodyData = aR.getData().getBody().getData();
            resp.setSensitive(responseBodyData.getSensitive());
            List<ClassifyingRubbishElements> elements = new ArrayList<>();

            if (CollectionUtils.isNotEmpty(responseBodyData.getElements())) {
                for (ClassifyingRubbishResponseBody.ClassifyingRubbishResponseBodyDataElements element : responseBodyData.getElements()) {

                    ClassifyingRubbishElements classifyingRubbishElements = new ClassifyingRubbishElements();
                    classifyingRubbishElements.setRubbishScore(element.getRubbishScore());
                    classifyingRubbishElements.setRubbish(element.getRubbish());
                    classifyingRubbishElements.setCategory(element.getCategory());
                    classifyingRubbishElements.setCategoryScore(element.getCategoryScore());

                    elements.add(classifyingRubbishElements);

                }
            }

            resp.setElements(elements);

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


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


}
