package org.kne.codec.kif;

import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriter;
import javax.imageio.spi.IIORegistry;
import javax.imageio.spi.ImageWriterSpi;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.util.Locale;

/**
 * KIF格式的ImageIO服务提供者（Writer端）。
 * 这个类负责告诉ImageIO：我能写入.kif格式，并且我能创建对应的ImageWriter。
 */
public class KIFImageWriterSpi extends ImageWriterSpi {

    private static final String VENDOR_NAME = "KIFCodec Project";
    private static final String VERSION = "1.0";
    private static final String[] FORMAT_NAMES = {"kif", "KIF"};
    private static final String[] SUFFIXES = {"kif"};
    private static final String[] MIME_TYPES = {"image/kif"};
    private static final String WRITER_CLASS_NAME = KIFImageWriter.class.getName();

    private static final boolean SUPPORTS_STANDARD_STREAM_METADATA = false;
    private static final boolean SUPPORTS_STANDARD_IMAGE_METADATA = false;

    // 单例
    private static KIFImageWriterSpi instance;

    public KIFImageWriterSpi() {
        super(
            VENDOR_NAME,
            VERSION,
            FORMAT_NAMES,
            SUFFIXES,
            MIME_TYPES,
            WRITER_CLASS_NAME,
            new Class<?>[]{ImageOutputStream.class},
            new String[]{KIFImageReaderSpi.class.getName()}, // readerSpiNames
            SUPPORTS_STANDARD_STREAM_METADATA,
            null, // nativeStreamMetadataFormatName
            null, // nativeStreamMetadataFormatClassName
            null, // extraStreamMetadataFormatNames
            null, // extraStreamMetadataFormatClassNames
            SUPPORTS_STANDARD_IMAGE_METADATA,
            null, // nativeImageMetadataFormatName
            null, // nativeImageMetadataFormatClassName
            null, // extraImageMetadataFormatNames
            null  // extraImageMetadataFormatClassNames
        );
    }

    // ========== 手动注册方法 ==========

    public static void register() {
        IIORegistry registry = IIORegistry.getDefaultInstance();
      
        if (instance == null) {
            instance = new KIFImageWriterSpi();
        registry.registerServiceProvider(instance);
        }
    }

    public static void unregister() {
        IIORegistry registry = IIORegistry.getDefaultInstance();
        if (instance != null) {
            registry.deregisterServiceProvider(instance);
            instance=null;
        }
    }


    // ========== SPI 必须实现的方法 ==========

    @Override
    public boolean canEncodeImage(ImageTypeSpecifier type) {
        // 检查是否支持此图像类型
        // 简化版：支持大部分常见类型
        int sampleSize = type.getSampleModel().getSampleSize(0);
        // 支持 8-bit 灰度或 RGB
        return sampleSize == 8;
    }

    @Override
    public ImageWriter createWriterInstance(Object extension) throws IOException {
        return new KIFImageWriter(this);
    }

    @Override
    public String getDescription(Locale locale) {
        return "KIF (KIFCodec Image Format) Image Writer";
    }
}