package com.ycwl.basic.facebody.adapter;

import com.ycwl.basic.facebody.entity.AddFaceResp;
import com.ycwl.basic.facebody.entity.BceFaceBodyConfig;
import com.ycwl.basic.mapper.FaceSampleMapper;
import com.ycwl.basic.model.pc.faceSample.req.FaceSampleReqQuery;
import com.ycwl.basic.model.pc.faceSample.resp.FaceSampleRespVO;
import lombok.extern.slf4j.Slf4j;
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.SpringRunner;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;


@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
public class BceFaceBodyAdapterIT {
    @Autowired
    private FaceSampleMapper faceSampleMapper;

    private BceFaceBodyAdapter getAdapter() {
        BceFaceBodyAdapter adapter = new BceFaceBodyAdapter();
        BceFaceBodyConfig config = new BceFaceBodyConfig();
        config.setAppId("118363836");
        config.setApiKey("J2IRPrGFQWNeMtYFr6bgr6XO");
        config.setSecretKey("Gp6dx0ngG1VZN8Ono3y4kCgKo6nhURK8");
        config.setAddQps(9.9f);
        config.setSearchQps(9.9f);
        adapter.setConfig(config);
        return adapter;
    }

    @Test
    public void testDbCreate() {
        BceFaceBodyAdapter adapter = getAdapter();
        boolean b = adapter.addFaceDb("test");
        assertTrue(b);
        boolean b0 = adapter.assureFaceDb("test");
        assertTrue(b0);
        boolean b1 = adapter.deleteFaceDb("test");
        assertTrue(b1);
        boolean b2 = adapter.assureFaceDb("test");
        assertTrue(b2);
        boolean b3 = adapter.deleteFaceDb("test");
        assertTrue(b3);
    }

    @Test
    public void testAddFace() {
        BceFaceBodyAdapter adapter = getAdapter();
        adapter.assureFaceDb("test");
        adapter.addFace("test", "test", "https://frametour-assets.oss-cn-shanghai.aliyuncs.com/user-faces/user-face/c925d970-216a-4eff-b699-cd047c0f9088.jpg", "test");
        List<String> strings = adapter.listUserFace("test", "test");
        assertFalse(strings.isEmpty());
        adapter.deleteFace("test", "test");
        List<String> stringList = adapter.listUserFace("test", "test");
        assertTrue(stringList.isEmpty());
        List<String> listFace = adapter.listFace("test", null, 0, 10);
        assertTrue(listFace.isEmpty());
    }

    @Test
    public void testMigrateToBCE() {
        BceFaceBodyAdapter adapter = getAdapter();
        long scenicId = 3972138108618674176L;
        adapter.assureFaceDb(String.valueOf(scenicId));
        FaceSampleReqQuery query = new FaceSampleReqQuery();
        query.setScenicId(scenicId);
        List<FaceSampleRespVO> list = faceSampleMapper.list(query);
        list.parallelStream().forEach(faceSampleRespVO -> {
            AddFaceResp addFaceResp = adapter.addFace(String.valueOf(scenicId), faceSampleRespVO.getId().toString(), faceSampleRespVO.getFaceUrl(), faceSampleRespVO.getId().toString());
            if (addFaceResp == null) {
                addFaceResp = adapter.addFace(String.valueOf(scenicId), faceSampleRespVO.getId().toString(), faceSampleRespVO.getFaceUrl(), faceSampleRespVO.getId().toString());
                if (addFaceResp == null) {
                    log.error("当前景区{}，人脸样本ID{}，迁移失败", scenicId, faceSampleRespVO.getId());
                } else {
                    log.info("当前景区{}，人脸样本ID{}，迁移1成功", scenicId, faceSampleRespVO.getId());
                }
            } else {
                log.info("当前景区{}，人脸样本ID{}，迁移成功", scenicId, faceSampleRespVO.getId());
            }
        });
    }

    @Test
    public void testDeleteFace() {
        BceFaceBodyAdapter adapter = getAdapter();
        String db = "3972138108618674176";
        List<String> entityIds = adapter.listFace(db, "", null, null);
        entityIds.forEach(entityId -> {
            adapter.deleteFace(db, entityId);
        });
    }

    @Test
    public void testDeleteDb() {
        BceFaceBodyAdapter adapter = getAdapter();
        adapter.deleteFaceDb("test");
    }
}