package com.heyu.api.controller.imageaudit;


import com.aliyun.imageaudit20191230.models.ScanImageResponse;
import com.aliyun.imageaudit20191230.models.ScanImageResponseBody;
import com.heyu.api.alibaba.handle.imageaudit.AScanImageHandle;
import com.heyu.api.alibaba.request.imageaudit.AScanImageRequest;
import com.heyu.api.alibaba.request.imageaudit.AScanImageRequestTask;
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.imageaudit.ScanImageReq;
import com.heyu.api.resp.imageaudit.ScanImageResp;
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/imageaudit/2019-12-30/ScanImage?RegionId=cn-shanghai
 *
 *图片内容安全 Scanlmage
 *
 */
@Slf4j
@RestController
@RequestMapping("/scan")
@NotIntercept
public class ScanImageController extends BaseController {


    @Autowired
    private AScanImageHandle aScanImageHandle;

    @RequestMapping("/image")
    @CacheResult
    public R scan(ScanImageReq req) {
        ScanImageResp resp = new ScanImageResp();
        AScanImageRequest request = new AScanImageRequest();

        request.setScenes(req.getScenes());

        List<AScanImageRequestTask> tasks = new ArrayList<>();
        AScanImageRequestTask task = new AScanImageRequestTask();

        task.setImageUrl(req.getImageUrl());
        task.setImageBase64(req.getImageBase64());

        tasks.add(task);
        request.setTasks(tasks);

        ApiR<ScanImageResponse> aR = aScanImageHandle.handle(request);

        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {

            ScanImageResponseBody.ScanImageResponseBodyData responseBodyData = aR.getData().getBody().getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getResults())) {
                for (ScanImageResponseBody.ScanImageResponseBodyDataResults result : responseBodyData.getResults()) {
                    List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults> subResults = result.getSubResults();
                    resp.setImageURL(resp.getImageURL());
                    List<ScanImageResp.SubResults> subResultsList = new ArrayList<>();
                    for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResults resultsSubResults : subResults) {
                        ScanImageResp.SubResults subResult = new ScanImageResp.SubResults();

                        List<ScanImageResp.SubResultsFrames> subResultsFrames = new ArrayList<>();

                        List<ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsFrames> resultsSubResultsFrames = resultsSubResults.getFrames();
                        for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsFrames resultsSubResultsFrame : resultsSubResultsFrames) {
                            ScanImageResp.SubResultsFrames resultsFrames = new ScanImageResp.SubResultsFrames();

                            resultsFrames.setURL(resultsSubResultsFrame.getURL());
                            resultsFrames.setRate(resultsSubResultsFrame.getRate());

                            subResultsFrames.add(resultsFrames);
                        }

                        subResult.setSubResultsFrames(subResultsFrames);

                        List<ScanImageResp.SubResultsHintWordsInfoList> subResultsHintWordsInfoList = new ArrayList<>();
                        for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsHintWordsInfoList resultsSubResultsHintWordsInfoList : resultsSubResults.getHintWordsInfoList()) {

                            ScanImageResp.SubResultsHintWordsInfoList resultsHintWordsInfoList = new ScanImageResp.SubResultsHintWordsInfoList();
                            resultsHintWordsInfoList.setContext(resultsSubResultsHintWordsInfoList.getContext());
                            subResultsHintWordsInfoList.add(resultsHintWordsInfoList);
                        }

                        subResult.setSubResultsHintWordsInfoList(subResultsHintWordsInfoList);

                        List<ScanImageResp.SubResultsLogoDataList> subResultsLogoDataList = new ArrayList<>();
                        for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsLogoDataList resultsSubResultsLogoDataList : resultsSubResults.getLogoDataList()) {

                            ScanImageResp.SubResultsLogoDataList resultsLogoDataList = new ScanImageResp.SubResultsLogoDataList();
                            resultsLogoDataList.setX(resultsSubResultsLogoDataList.getX());
                            resultsLogoDataList.setY(resultsSubResultsLogoDataList.getY());
                            resultsLogoDataList.setWidth(resultsSubResultsLogoDataList.getWidth());
                            resultsLogoDataList.setHeight(resultsSubResultsLogoDataList.getHeight());
                            resultsLogoDataList.setName(resultsSubResultsLogoDataList.getName());
                            resultsLogoDataList.setType(resultsSubResultsLogoDataList.getType());

                            subResultsLogoDataList.add(resultsLogoDataList);

                        }
                        subResult.setSubResultsLogoDataList(subResultsLogoDataList);

                        subResult.setOCRDataList(resultsSubResults.getOCRDataList());

                        List<ScanImageResp.SubResultsProgramCodeDataList> subResultsProgramCodeDataListList = new ArrayList<>();
                        for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsProgramCodeDataList resultsSubResultsProgramCodeDataList : resultsSubResults.getProgramCodeDataList()) {
                            ScanImageResp.SubResultsProgramCodeDataList resultsProgramCodeDataList = new ScanImageResp.SubResultsProgramCodeDataList();
                            resultsProgramCodeDataList.setHeight(resultsSubResultsProgramCodeDataList.getHeight());
                            resultsProgramCodeDataList.setWidth(resultsSubResultsProgramCodeDataList.getWidth());
                            resultsProgramCodeDataList.setX(resultsSubResultsProgramCodeDataList.getX());
                            resultsProgramCodeDataList.setY(resultsSubResultsProgramCodeDataList.getY());
                            subResultsProgramCodeDataListList.add(resultsProgramCodeDataList);
                        }
                        subResult.setSubResultsProgramCodeDataListList(subResultsProgramCodeDataListList);

                        List<ScanImageResp.SubResultsSfaceDataList> sfaceDataList = new ArrayList<>();

                        for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsSfaceDataList resultsSubResultsSfaceDataList : resultsSubResults.getSfaceDataList()) {
                            ScanImageResp.SubResultsSfaceDataList resultsSfaceDataList = new ScanImageResp.SubResultsSfaceDataList();
                            List<ScanImageResp.SubResultsSfaceDataListFaces> faces = new ArrayList<>();
                            for (ScanImageResponseBody.ScanImageResponseBodyDataResultsSubResultsSfaceDataListFaces face : resultsSubResultsSfaceDataList.getFaces()) {
                                ScanImageResp.SubResultsSfaceDataListFaces facesData = new ScanImageResp.SubResultsSfaceDataListFaces();
                                facesData.setId(face.getId());
                                facesData.setName(face.getName());
                                facesData.setRate(face.getRate());
                                faces.add(facesData);
                            }
                            resultsSfaceDataList.setFaces(faces);

                            sfaceDataList.add(resultsSfaceDataList);
                        }

                        subResult.setSfaceDataList(sfaceDataList);
                        subResultsList.add(subResult);
                    }
                    resp.setSubResults(subResultsList);
                    return R.ok().setData(resp);
                }
            }
        }
        return R.error(aR.getErrorMsg());
    }


}
