package com.zsc.edu.gateway.service.iot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zsc.edu.gateway.domain.iot.ParamBuilder;
import com.zsc.edu.gateway.exception.ConstraintException;
import com.zsc.edu.gateway.modules.iot.tsl.dto.EventDto;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ParamDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.DataType;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Param;
import com.zsc.edu.gateway.modules.iot.tsl.repo.ParamRepository;
import com.zsc.edu.gateway.modules.iot.tsl.service.ParamService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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

@SpringBootTest
@Transactional
public class ParamServiceTest {

    @Autowired
    private ParamService service;

    @Autowired
    private ParamRepository repo;

    private Param param1;
    private Param param2;

    @BeforeEach
    void setUp() {
        param1 = ParamBuilder.aParam().name("PARAM_NAME_1").foreignId(1L).dataType(DataType.DATE).build();
        repo.insert(param1);
        param2 = ParamBuilder.aParam().name("PARAM_NAME_2").foreignId(2L).dataType(DataType.DATE).build();
        repo.insert(param2);
    }

    @AfterEach
    void tearDown() {
        repo.delete(new LambdaQueryWrapper<Param>()
                .in(Param::getName, "PARAM_NAME_1", "PARAM_NAME_2", "PARAM_NAME_3", "PARAM_UPDATE"));
    }

    @Test
    void list() {
        LambdaQueryWrapper<Param> queryWrapper = new LambdaQueryWrapper<>();
        assertEquals(2, service.list(queryWrapper.like(Param::getName, "PARAM")).size());
        assertEquals(1, service.list(queryWrapper.eq(Param::getName, param1.getName())).size());
    }

    @Test
    void createParams() {
//        ParamDto dto = new ParamDto("PARAM_NAME_3", "测试1", "备注", DataType.DATE, Param.Type.OUTPUT);
        List<ParamDto> params = new ArrayList<>();
//        params.add(dto);
        Long foreignId = 1L;
        Boolean result = service.create(params, foreignId, null);
        assertTrue(result);
        List<Param> insertedParams = service.list(new LambdaQueryWrapper<Param>().like(Param::getName, "PARAM"));
        assertEquals(3, insertedParams.size());
    }

    @Test
    void updateParams() {
        ParamDto dto = new ParamDto();
        dto.setName("PARAM_UPDATE");
        dto.setIdentifier("测试更新");
        List<ParamDto> params = new ArrayList<>();
        params.add(dto);
        assertTrue(service.update(params, param2.getForeignId()));
        Param updatedParam = repo.selectOne(new LambdaQueryWrapper<Param>().eq(Param::getName, dto.getName()));
        assertNotNull(updatedParam);
        assertEquals("PARAM_UPDATE", updatedParam.getName());
        assertEquals(param2.getId(), updatedParam.getId());
    }

}
