package cokr.xit.fims.sprt;

import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;

import cokr.xit.applib.PstnAndSize;
import cokr.xit.foundation.AbstractComponent;
import cokr.xit.foundation.data.DataObject;

public class MediaUtil extends AbstractComponent {
	public static List<PstnAndSize> getTransformedMosaic(List<DataObject> mosaicInfos, InputStream inputStream, PstnAndSize pstnAndSize_in_print) {
		if (isEmpty(mosaicInfos))
			return Collections.emptyList();

		try {
			BufferedImage image = ImageIO.read(inputStream);
			float originalWidth = image.getWidth(null);
			float originalHeight = image.getHeight(null);
			float transX = pstnAndSize_in_print.getWidth() / originalWidth;
			float transY = pstnAndSize_in_print.getHeight() / originalHeight;

			return mosaicInfos.stream()
				.map(mosaicInfo -> {
    				float x = toFloat(mosaicInfo.get("X_AXS"));
    				float y = toFloat(mosaicInfo.get("Y_AXS"));
    				float w = toFloat(mosaicInfo.get("MOSC_LEN"));
    				float h = toFloat(mosaicInfo.get("MOSC_HGT"));

    				PstnAndSize mosaic = new PstnAndSize(x, y, w, h);
    				mosaic.transXY(transX, transY);
    				mosaic.addPstn(pstnAndSize_in_print.getPstn());

    				return mosaic;
				})
				.toList();
		} catch (Exception e) {
			throw new RuntimeException("이미지 모자이크 처리 오류");
		}
	}
}