package com.ycwl.basic.task;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.facebody.model.v20191230.GetFaceEntityRequest;
import com.aliyuncs.facebody.model.v20191230.GetFaceEntityResponse;
import com.aliyuncs.facebody.model.v20191230.ListFaceEntitiesRequest;
import com.aliyuncs.facebody.model.v20191230.ListFaceEntitiesResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.ycwl.basic.config.FaceDetectConfig;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.mapper.FaceSampleMapper;
import com.ycwl.basic.mapper.ScenicMapper;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.model.pc.faceSample.req.FaceSampleReqQuery;
import com.ycwl.basic.model.pc.faceSample.resp.FaceSampleRespVO;
import com.ycwl.basic.model.pc.scenic.req.ScenicReqQuery;
import com.ycwl.basic.model.pc.scenic.resp.ScenicRespVO;
import com.ycwl.basic.ratelimiter.FixedRateLimiter;
import com.ycwl.basic.service.task.TaskFaceService;
import com.ycwl.basic.utils.DateUtils;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import static com.ycwl.basic.constant.FaceConstant.USER_FACE_DB_NAME;
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class FaceCleanerTest {
    @Autowired
    private FaceMapper faceMapper;
    @Autowired
    private TaskFaceService taskFaceService;
    @Autowired
    private FaceSampleMapper faceSampleMapper;
    @Autowired
    private FaceDetectConfig faceDetectConfig;
    @Autowired
    private ScenicMapper scenicMapper;

    private IAcsClient getClient() {
        DefaultProfile profile = DefaultProfile.getProfile(
                faceDetectConfig.getRegion(),faceDetectConfig.getAccessKeyId(), faceDetectConfig.getAccessKeySecret());
        IAcsClient client = new DefaultAcsClient(profile);
        return client;
    }
    private static final String DATE_FORMAT="yyyyMMddHHmmss";
    private final FixedRateLimiter entityLimiter1 = new FixedRateLimiter(600, TimeUnit.MILLISECONDS);
    private final FixedRateLimiter entityLimiter2 = new FixedRateLimiter(600, TimeUnit.MILLISECONDS);

    @Test
    public void test() {
        ScenicReqQuery scenicQuery = new ScenicReqQuery();
        List<ScenicRespVO> scenicList = scenicMapper.list(scenicQuery);
        scenicList.forEach(scenic -> {
            log.info("当前景区{}，开始删除人脸样本", scenic.getName());
            taskFaceService.batchDeleteExpiredFace(scenic.getId());
        });
    }

    @Test
    public void testC() {
        List<List<Integer>> list = Arrays.asList(Arrays.asList(1,2,3), Arrays.asList(4,5,6), Arrays.asList(7,8,9), Arrays.asList(10,11,12));
        ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 16, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(16));
        list.parallelStream().forEach(integers -> {
            executor.execute(() -> {
                integers.forEach(integer -> {
                    executor.execute(() -> {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException ignored) {
                        }
                        log.info("{}", integer);
                    });
                });
            });
        });
        try {
            Thread.sleep(2000);
            executor.shutdown();
            executor.awaitTermination(1, TimeUnit.MINUTES);
        } catch (InterruptedException ignored) {

        }
    }
}