package com.qmth.boot.test.tools.excel.write;

import com.qmth.boot.test.tools.excel.model.ExcelTestEntity;
import com.qmth.boot.test.tools.excel.model.GenderEnum;
import com.qmth.boot.tools.excel.ExcelWriter;
import com.qmth.boot.tools.excel.enums.ExcelType;
import com.qmth.boot.tools.excel.model.CellValue;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.junit.Assert;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class ExcelWriteTest {

    @Test
    public void testXlsx() throws IOException {
        //        ExcelTestEntity entity = new ExcelTestEntity();
        //        entity.setId(System.currentTimeMillis());
        //        entity.setName("name");
        //        entity.setScore(95.5);
        //        entity.setGender(GenderEnum.FEMALE);
        //        entity.setTime(new Date());
        //        entity.setEnable(true);
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        //FileOutputStream ous = new FileOutputStream(new File("/Users/luoshi/Downloads/test.xlsx"));
        ExcelWriter writer = ExcelWriter.create(ExcelType.XLSX);
        //
        CellStyle cellStyle = writer.createCellStyle();
        Font font = writer.createFont();
        font.setColor(IndexedColors.RED.index);
        font.setFontHeightInPoints((short) 20);
        cellStyle.setFillForegroundColor(IndexedColors.GREY_40_PERCENT.index);
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cellStyle.setFont(font);
        writer.setCellStyle("sheet1", cellStyle, 0, 0);
        writer.setCellStyle("sheet1", cellStyle, 1, 0, 1);
        //        writer.writeObjects("sheet1", new String[] { "标题" }, ExcelTestEntity.class,
        //                Collections.singletonList(entity).iterator());
        List<CellValue[]> dataList = new LinkedList<>();
        for (int i = 0; i < 10000; i++) {
            dataList.add(
                    new CellValue[] { CellValue.of(i + 1), CellValue.of(RandomStringUtils.random(16, true, true)) });
        }
        writer.writeDataArrays("sheet1", new String[] { "考生表" }, new String[] { "编号", "姓名" }, dataList.iterator());
        writer.output(ous);
        Assert.assertTrue(ous.toByteArray().length > 0);
        ous.close();
    }

    @Test
    public void testXls() throws IOException {
        ExcelTestEntity entity = new ExcelTestEntity();
        entity.setId(System.currentTimeMillis() * 100);
        entity.setName("name");
        entity.setScore(65d);
        entity.setGender(GenderEnum.MALE);
        entity.setTime(new Date());
        entity.setEnable(false);
        entity.setClassName("   ");
        entity.setTeacher("!@#");
        ByteArrayOutputStream ous = new ByteArrayOutputStream();
        ExcelWriter writer = ExcelWriter.create(ExcelType.XLS);
        writer.writeStringArrays("sheet1", new String[] { "考生表" }, new String[] { "编号", "姓名" },
                Collections.singletonList(new String[] { "1", "张三" }).iterator());
        writer.output(ous);
        Assert.assertTrue(ous.toByteArray().length > 0);
    }
}
