package shop.alien.entity.store.excelVo.util;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.LifeClassManage;
import shop.alien.entity.store.vo.StoreCommentAppealVo;
import shop.alien.entity.store.vo.StoreDynamicDiscountVo;
import shop.alien.entity.store.vo.StoreInfoVo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

public class ExcelExporter {

    public static <T> byte[] exportToExcel(List<T> data, String[] headers, String sheetName) throws IOException {
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet(sheetName);

        Row headerRow = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            headerRow.createCell(i).setCellValue(headers[i]);
        }
        SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        int rowNum = 1;
        for (T item : data) {
            Row row = sheet.createRow(rowNum++);
            if (item instanceof StoreInfoVo) {
                StoreInfoVo storeInfoVo = (StoreInfoVo) item;
                row.createCell(0).setCellValue(storeInfoVo.getStoreName());
                row.createCell(1).setCellValue(storeInfoVo.getStoreContact());
                row.createCell(2).setCellValue(storeInfoVo.getStorePhone());
                row.createCell(3).setCellValue(storeInfoVo.getStoreTypeStr());
                if (!StringUtils.isEmpty(storeInfoVo.getCreatedTime())) {
                    String date = simpleFormat.format(storeInfoVo.getCreatedTime());
                    row.createCell(4).setCellValue(date);
                }
//                row.createCell(6).setCellValue(storeInfoVo.getStoreState());
            } else if (item instanceof StoreCommentAppealVo) {
                StoreCommentAppealVo user = (StoreCommentAppealVo) item;
                row.createCell(0).setCellValue(user.getStoreName());
                row.createCell(1).setCellValue(user.getStoreContact());
                row.createCell(2).setCellValue(user.getStorePhone());
                row.createCell(3).setCellValue(user.getStoreTypeStr());
                if (!StringUtils.isEmpty(user.getCreatedTime())) {
                    String date = simpleFormat.format(user.getCreatedTime());
                    row.createCell(4).setCellValue(date);
                }
                row.createCell(5).setCellValue(user.getCommentContent());
                row.createCell(6).setCellValue(user.getAppealReason());
                row.createCell(7).setCellValue(user.getAppealStatusStr());
            } else if (item instanceof LifeClassManage) {
                LifeClassManage manage = (LifeClassManage) item;
                row.createCell(0).setCellValue(manage.getClassName());
                row.createCell(1).setCellValue(manage.getClassLength());
                row.createCell(2).setCellValue(manage.getClassTeacher());
                row.createCell(3).setCellValue(manage.getPlayCount());
                row.createCell(4).setCellValue(manage.getClassBigType());
                if (!StringUtils.isEmpty(manage.getCreatedTime())) {
                    String date = simpleFormat.format(manage.getCreatedTime());
                    row.createCell(5).setCellValue(date);
                }
                if (!StringUtils.isEmpty(manage.getClassState())) {
                    row.createCell(6).setCellValue(manage.getClassState().equals("1") ? "正常" : "隐藏");
                }
            } else if (item instanceof StoreDynamicDiscountVo) {
                StoreDynamicDiscountVo dynamicDiscountVo = (StoreDynamicDiscountVo) item;
                row.createCell(0).setCellValue(dynamicDiscountVo.getActivityNo());
                row.createCell(1).setCellValue(dynamicDiscountVo.getActivityName());
                row.createCell(2).setCellValue(dynamicDiscountVo.getCreatedTime());
                row.createCell(3).setCellValue(dynamicDiscountVo.getActivityStartTime() + " - " + dynamicDiscountVo.getActivityEndTime());
                switch (dynamicDiscountVo.getStatus()) {
                    case 0:
                        row.createCell(4).setCellValue("待审核");
                        break;
                    case 1:
                        row.createCell(4).setCellValue("已通过");
                        break;
                    case 2:
                        row.createCell(4).setCellValue("已拒绝");
                        break;
                }
            }
        }
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();
        return outputStream.toByteArray();
    }
}
