package de.larsgrefer.sense_hat;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.time.Duration;

/**
 * @author Lars Grefer
 */
@RequiredArgsConstructor
@Getter
@Setter
@Slf4j
public class TextDisplay {

    private final SenseHat senseHat;
    private Color background = Color.BLACK;
    private Color foreground = Color.WHITE;
    private Duration duration = Duration.ofMillis(250);

    public void displayText(String text) throws IOException {
        BufferedImage image = new BufferedImage(8, 8, BufferedImage.TYPE_USHORT_565_RGB);

        Graphics2D graphics = image.createGraphics();

        Font sansSerif = new Font("SansSerif", Font.PLAIN, 8);
        graphics.setFont(sansSerif);
        int i = graphics.getFontMetrics().stringWidth(text) - 8;

        long durationInMillis = duration.toMillis();

        for (int j = 0; j <= i; j++) {
            long start = System.currentTimeMillis();
            graphics.setColor(background);
            graphics.setPaint(background);
            graphics.fillRect(0, 0, 8, 8);

            graphics.setColor(foreground);
            graphics.setPaint(foreground);
            graphics.drawString(text, -j, 6);
            senseHat.fadeTo(image, duration.dividedBy(2));

            long timeToSleep = durationInMillis - (System.currentTimeMillis() - start);
            try {
                Thread.sleep(timeToSleep);
            } catch (InterruptedException e) {
                log.error(e.getLocalizedMessage(), e);
            }
        }

        graphics.dispose();


    }

    public void setForeground(SenseHatColor foreground) {
        this.foreground = foreground.toColor();
    }

    public void setBackground(SenseHatColor background) {
        this.background = background.toColor();
    }
}

@@ -813,17 +813,18 @@
 .PLAIN, 
-8
+10
 );%0A     
@@ -1325,17 +1325,17 @@
 xt, -j, 
-6
+8
 );%0A     

increase font size