package com.example;

import java.io.FileOutputStream;
import java.nio.file.Path;

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.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class FillPatternTypeSamples {

    // ★指定された順序
    private static final FillPatternType[] ORDERED_FILL_PATTERNS = {
            FillPatternType.ALT_BARS,
            FillPatternType.BIG_SPOTS,
            FillPatternType.BRICKS,
            FillPatternType.DIAMONDS,
            FillPatternType.FINE_DOTS,
            FillPatternType.LEAST_DOTS,
            FillPatternType.LESS_DOTS,
            FillPatternType.NO_FILL,
            FillPatternType.SOLID_FOREGROUND,
            FillPatternType.SPARSE_DOTS,
            FillPatternType.SQUARES,
            FillPatternType.THICK_BACKWARD_DIAG,
            FillPatternType.THICK_FORWARD_DIAG,
            FillPatternType.THICK_HORZ_BANDS,
            FillPatternType.THICK_VERT_BANDS,
            FillPatternType.THIN_BACKWARD_DIAG,
            FillPatternType.THIN_FORWARD_DIAG,
            FillPatternType.THIN_HORZ_BANDS,
            FillPatternType.THIN_VERT_BANDS
    };

    public static void main(String[] args) throws Exception {
        Path out = Path.of("temp\\FillPatternType_Samples.xlsx");

        try (Workbook wb = new XSSFWorkbook()) {
            Sheet sheet = wb.createSheet("FillPatternType Samples");

            // 列幅：0=名前、1..10=サンプル（結合して広く見せる）
            sheet.setColumnWidth(0, 34 * 256);
            for (int c = 1; c <= 10; c++) {
                sheet.setColumnWidth(c, 10 * 256);
            }

            // ヘッダ
            createHeader(sheet, wb);

            int rowIndex = 1;

            for (FillPatternType pt : ORDERED_FILL_PATTERNS) {
                Row row = sheet.createRow(rowIndex);
                row.setHeightInPoints(26);

                // 左：パターン名
                Cell name = row.createCell(0);
                name.setCellValue(pt.name());
                CellStyle nameStyle = wb.createCellStyle();
                nameStyle.setVerticalAlignment(VerticalAlignment.CENTER);
                setThinBorder(nameStyle);
                name.setCellStyle(nameStyle);

                // 右：サンプル（1..10 を結合）
                sheet.addMergedRegion(new CellRangeAddress(rowIndex, rowIndex, 1, 10));

                Cell sample = row.createCell(1);
                CellStyle sampleStyle = createPatternStyle(wb, pt);
                sample.setCellStyle(sampleStyle);

                // 結合領域の残りセルにも同スタイル（塗り/罫線を崩さないため）
                for (int c = 2; c <= 10; c++) {
                    Cell sc = row.createCell(c);
                    sc.setCellStyle(sampleStyle);
                }

                rowIndex++;
            }

            try (FileOutputStream fos = new FileOutputStream(out.toFile())) {
                wb.write(fos);
            }
        }

        System.out.println("Created: " + out.toAbsolutePath());
    }

    private static void createHeader(Sheet sheet, Workbook wb) {
        Row r = sheet.createRow(0);
        r.setHeightInPoints(22);

        Cell h1 = r.createCell(0);
        h1.setCellValue("FillPatternType");

        Cell h2 = r.createCell(1);
        h2.setCellValue("Sample");

        CellStyle header = wb.createCellStyle();
        Font f = wb.createFont();
        f.setBold(true);
        header.setFont(f);
        header.setAlignment(HorizontalAlignment.CENTER);
        header.setVerticalAlignment(VerticalAlignment.CENTER);
        header.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        header.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
        setThinBorder(header);

        h1.setCellStyle(header);
        h2.setCellStyle(header);

        // Sample 見出しを広く（1..10 結合）
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 1, 10));
        for (int c = 2; c <= 10; c++) {
            Cell hc = r.createCell(c);
            hc.setCellStyle(header);
        }
    }

    private static CellStyle createPatternStyle(Workbook wb, FillPatternType pt) {
        CellStyle st = wb.createCellStyle();
        setThinBorder(st);

        st.setFillPattern(pt);

        // 見やすい配色
        if (pt == FillPatternType.NO_FILL) {
            // 塗りなし
        } else if (pt == FillPatternType.SOLID_FOREGROUND) {
            st.setFillForegroundColor(IndexedColors.GREY_50_PERCENT.getIndex());
        } else {
            st.setFillForegroundColor(IndexedColors.BLACK.getIndex());
            st.setFillBackgroundColor(IndexedColors.WHITE.getIndex());
        }

        return st;
    }

    private static void setThinBorder(CellStyle st) {
        st.setBorderTop(BorderStyle.THIN);
        st.setBorderBottom(BorderStyle.THIN);
        st.setBorderLeft(BorderStyle.THIN);
        st.setBorderRight(BorderStyle.THIN);
    }
}
