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.xssf.usermodel.XSSFWorkbook;

public class IndexedColorsSamples {
    private static final IndexedColors[] ORDERED_COLORS = {
            IndexedColors.AQUA,
            IndexedColors.AUTOMATIC,
            IndexedColors.BLACK,
            IndexedColors.BLACK1,
            IndexedColors.BLUE,
            IndexedColors.BLUE_GREY,
            IndexedColors.BLUE1,
            IndexedColors.BRIGHT_GREEN,
            IndexedColors.BRIGHT_GREEN1,
            IndexedColors.BROWN,
            IndexedColors.CORAL,
            IndexedColors.CORNFLOWER_BLUE,
            IndexedColors.DARK_BLUE,
            IndexedColors.DARK_GREEN,
            IndexedColors.DARK_RED,
            IndexedColors.DARK_TEAL,
            IndexedColors.DARK_YELLOW,
            IndexedColors.GOLD,
            IndexedColors.GREEN,
            IndexedColors.GREY_25_PERCENT,
            IndexedColors.GREY_40_PERCENT,
            IndexedColors.GREY_50_PERCENT,
            IndexedColors.GREY_80_PERCENT,
            IndexedColors.INDIGO,
            IndexedColors.LAVENDER,
            IndexedColors.LEMON_CHIFFON,
            IndexedColors.LIGHT_BLUE,
            IndexedColors.LIGHT_CORNFLOWER_BLUE,
            IndexedColors.LIGHT_GREEN,
            IndexedColors.LIGHT_ORANGE,
            IndexedColors.LIGHT_TURQUOISE,
            IndexedColors.LIGHT_TURQUOISE1,
            IndexedColors.LIGHT_YELLOW,
            IndexedColors.LIME,
            IndexedColors.MAROON,
            IndexedColors.OLIVE_GREEN,
            IndexedColors.ORANGE,
            IndexedColors.ORCHID,
            IndexedColors.PALE_BLUE,
            IndexedColors.PINK,
            IndexedColors.PINK1,
            IndexedColors.PLUM,
            IndexedColors.RED,
            IndexedColors.RED1,
            IndexedColors.ROSE,
            IndexedColors.ROYAL_BLUE,
            IndexedColors.SEA_GREEN,
            IndexedColors.SKY_BLUE,
            IndexedColors.TAN,
            IndexedColors.TEAL,
            IndexedColors.TURQUOISE,
            IndexedColors.TURQUOISE1,
            IndexedColors.VIOLET,
            IndexedColors.WHITE,
            IndexedColors.WHITE1,
            IndexedColors.YELLOW,
            IndexedColors.YELLOW1
    };

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

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

            final int columnsPerRow = 6; // 1行に並べる色数
            final int cellWidth = 22; // 列幅（文字が収まる程度）

            for (int c = 0; c < columnsPerRow; c++) {
                sheet.setColumnWidth(c, cellWidth * 256);
            }

            IndexedColors[] colors = ORDERED_COLORS;

            int rowIndex = 0;
            int colIndex = 0;

            for (IndexedColors color : colors) {
                if (colIndex == 0) {
                    Row row = sheet.createRow(rowIndex);
                    row.setHeightInPoints(22);
                }

                Row row = sheet.getRow(rowIndex);
                Cell cell = row.createCell(colIndex);

                cell.setCellValue(color.name());

                CellStyle style = wb.createCellStyle();
                style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
                style.setFillForegroundColor(color.getIndex());
                style.setAlignment(HorizontalAlignment.CENTER);
                style.setVerticalAlignment(VerticalAlignment.CENTER);

                // 罫線
                style.setBorderTop(BorderStyle.THIN);
                style.setBorderBottom(BorderStyle.THIN);
                style.setBorderLeft(BorderStyle.THIN);
                style.setBorderRight(BorderStyle.THIN);

                Font font = wb.createFont();
                font.setBold(true);
                font.setFontHeightInPoints((short) 10);
                font.setColor(isDarkColor(color)
                        ? IndexedColors.WHITE.getIndex()
                        : IndexedColors.BLACK.getIndex());
                style.setFont(font);

                cell.setCellStyle(style);

                colIndex++;
                if (colIndex >= columnsPerRow) {
                    colIndex = 0;
                    rowIndex++;
                }
            }
            try (FileOutputStream fos = new FileOutputStream(out.toFile())) {
                wb.write(fos);
            }
        }

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

    /**
     * 暗い色かどうかの簡易判定
     * （視認性向上用：正確な輝度計算まではしない）
     */
    private static boolean isDarkColor(IndexedColors c) {
        switch (c) {
        case BLACK:
        case BLUE:
        case DARK_BLUE:
        case DARK_GREEN:
        case DARK_RED:
        case DARK_TEAL:
        case DARK_YELLOW:
        case BROWN:
        case INDIGO:
        case MAROON:
        case OLIVE_GREEN:
        case TEAL:
        case VIOLET:
            return true;
        default:
            return false;
        }
    }
}
