package com.supervision;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileTypeUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import com.supervision.domain.IrFile;
import com.supervision.dto.QueryProcessDTO;
import com.supervision.dto.robot.ByteWrapper;
import com.supervision.mapper.RobotDataMapper;
import com.supervision.service.IrFileService;
import com.supervision.service.IrKnowledgeService;
import com.supervision.service.QueryTemplateProcessor;
import com.supervision.util.mybatis.RowSqlMapper;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@RunWith(SpringJUnit4ClassRunner.class)
@RequiredArgsConstructor
class AskApplicationTests {

    @Autowired
    private IrKnowledgeService irKnowledgeService;


    @Autowired
    private RowSqlMapper sqlMapper;

    @Test
    void rowSqlMapperTest() {

        HashMap<String, Object> params = new HashMap<>();
        params.put("id", "111");
        //params.put("dept_name","沈阳公安");
        // and dept_name= #{dept_name}
        List<Map<String, Object>> maps = sqlMapper.selectList("<script> select * from interro_robot.ir_robot_config where id = #{id} <if test= \"dept_name != null\">" +
                " and dept_name= #{dept_name} </if> </script>", params);
        System.out.println(maps);
    }

    @Autowired
    private Configuration databaseFreemarkerConfiguration;

    @Test
    void dbFreemarkerLoadTest() throws IOException, TemplateException {

        Template temp = databaseFreemarkerConfiguration.getTemplate("1770984442920292354");
        StringWriter writer = new StringWriter();
        HashMap<String, Object> params = new HashMap<>();
        params.put("jykh_count", "ss");
        temp.process(params, writer);
        System.out.println(writer);
        writer.close();

    }


    @Test
    void stringTemplateLoaderTest() throws IOException, TemplateException {
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_30);

        // 创建一个 StringTemplateLoader 对象
        StringTemplateLoader stringLoader = new StringTemplateLoader();

        // 添加模板到 StringTemplateLoader 中
        String templateContent = "Hello, ${user}!";
        stringLoader.putTemplate("myTemplate", templateContent);

        // 将 StringTemplateLoader 添加到 Configuration 中
        cfg.setTemplateLoader(stringLoader);

        // 获取模板
        Template template = cfg.getTemplate("myTemplate");

        // 创建模板数据模型
        Map<String, Object> data = new HashMap<>();
        data.put("user", "John");

        // 渲染模板
        StringWriter output = new StringWriter();
        template.process(data, output);

        // 打印输出结果
        System.out.println(output.toString());
    }


    @Autowired
    private QueryTemplateProcessor queryTemplateProcessor;

    @Test
    void QueryTemplateProcessorProcessTest() {

        QueryProcessDTO process = queryTemplateProcessor.process("1770984442920292354", "123", null);

        System.out.println(process);
    }

    @Autowired
    private IrFileService irFileService;

    @Test
    void fileByteSaveTest() {
        IrFile irFile = new IrFile();
        irFile.setFileName("ddd");
        irFile.setFileType("dddaa");
        irFile.setFileSize(100);
        irFile.setFileByte("ddd".getBytes());
        irFileService.save(irFile);
    }

    @Test
    void fileByteGetTest() {
        IrFile byId = irFileService.getById("1772822158139199490");
        System.out.println(byId.getFileName());
    }


    @Resource
    private RobotDataMapper robotDataMapper;

    @Test
    void base64WriteTest() throws FileNotFoundException {

        /*AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(null)*/

        ByteWrapper byteWrapper = robotDataMapper.capitalFlowImage("12");

        FileOutputStream fos = new FileOutputStream("F:\\tmp\\1\\image11.png");


        IoUtil.write(fos, true, Base64.decode(byteWrapper.getResult()));

    }


    @Test
    void videoLengthTest() throws IOException, UnsupportedAudioFileException {

        MockMultipartFile mockMultipartFile = new MockMultipartFile("test.xls", "originalFilename",
                "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                FileUtil.getInputStream("F:\\tmp\\1\\心肺听诊-大湿罗音.mp3"));


        InputStream inputStream = mockMultipartFile.getInputStream();

        byte[] bytes = IoUtil.readBytes(inputStream);

        System.out.println(bytes.length);
        byte[] bytes1 = IoUtil.readBytes(inputStream);

        System.out.println(bytes1.length);
    }
}
