package com.example.todolist.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
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.HorizontalAlignment;
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.stereotype.Service;

@Service
public class SampleExcelService {
    private static final DateTimeFormatter DISPLAY_FORMAT = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

    public byte[] createSampleExcel(LocalDateTime currentTime) {
        // ①
        try (Workbook workbook = new XSSFWorkbook();
                ByteArrayOutputStream out = new ByteArrayOutputStream()) {

            // ②網掛け + 罫線 上右下左 + フォントサイズ 12 + 文字中央寄せ + 太字
            CellStyle pattern_borderAll_fontSize12_center_bold = workbook.createCellStyle();
            pattern_borderAll_fontSize12_center_bold.setFillPattern(FillPatternType.LESS_DOTS);
            pattern_borderAll_fontSize12_center_bold.setBorderTop(BorderStyle.HAIR);
            pattern_borderAll_fontSize12_center_bold.setBorderRight(BorderStyle.HAIR);
            pattern_borderAll_fontSize12_center_bold.setBorderBottom(BorderStyle.HAIR);
            pattern_borderAll_fontSize12_center_bold.setBorderLeft(BorderStyle.HAIR);
            pattern_borderAll_fontSize12_center_bold.setAlignment(HorizontalAlignment.CENTER);

            Font font12_bold = workbook.createFont();
            font12_bold.setFontName("Meiryo UI");
            font12_bold.setFontHeightInPoints((short) 12);
            font12_bold.setBold(true);
            pattern_borderAll_fontSize12_center_bold.setFont(font12_bold);

            // ②罫線 上右下左 + フォントサイズ 12
            CellStyle borderAll_fontSize12 = workbook.createCellStyle();
            borderAll_fontSize12.setBorderTop(BorderStyle.HAIR);
            borderAll_fontSize12.setBorderRight(BorderStyle.HAIR);
            borderAll_fontSize12.setBorderBottom(BorderStyle.HAIR);
            borderAll_fontSize12.setBorderLeft(BorderStyle.HAIR);

            Font font12 = workbook.createFont();
            font12.setFontName("Meiryo UI");
            font12.setFontHeightInPoints((short) 12);
            borderAll_fontSize12.setFont(font12);

            // ③シート作成
            Sheet sheet = workbook.createSheet("Sample");
            // 列幅設定（B列を 40文字分）
            sheet.setColumnWidth(1, 256 * 40);

            // ④データセット
            // 2 行目（index=1）
            Row row = sheet.createRow(1);
            Cell cell = row.createCell(1);

            // B2 に文字列と書式をセット
            cell.setCellValue("currentTime");
            cell.setCellStyle(pattern_borderAll_fontSize12_center_bold);

            // 3 行目（index=2）に時刻と書式
            row = sheet.createRow(2);
            cell = row.createCell(1);
            cell.setCellValue(currentTime.format(DISPLAY_FORMAT));
            cell.setCellStyle(borderAll_fontSize12);

            workbook.write(out); // ⑤
            return out.toByteArray();

        } catch (IOException e) {
            throw new IllegalStateException("Failed to create excel.", e);
        }
    }
}
