package me.hammerle.supersnuvi.javafx;

import javafx.scene.image.PixelWriter;

public class WritableImage extends Image
{
    private final javafx.scene.image.WritableImage fxImage;
    private final PixelWriter pixelWriter;
    private final int width;
    private final int height;
    
    public WritableImage(int width, int height)
    {
        fxImage = new javafx.scene.image.WritableImage(width, height);
        pixelWriter = fxImage.getPixelWriter();
        this.width = width;
        this.height = height;
    }
    
    private int toARGB(int r, int g, int b, int alpha)
    {
        return (b & 0xFF) | ((g & 0xFF) << 8) | ((r & 0xFF) << 16) | ((alpha & 0xFF) << 24);
    }
    
    public void setPixel(int x, int y, int r, int g, int b, int alpha)
    {
        pixelWriter.setArgb(0, 0, toARGB(r, g, b, alpha));
    }
    
    public void clear(int r, int g, int b, int alpha)
    {
        int black = toARGB(r, g, b, alpha);
        for(int x = 0; x < width; x++)
        {
            for(int y = 0; y < height; y++)
            {
                pixelWriter.setArgb(x, y, black);
            }
        }
    }

    @Override
    protected javafx.scene.image.Image getImage() 
    {
        return fxImage;
    }

    @Override
    public double getWidth() 
    {
        return width;
    }

    @Override
    public double getHeight() 
    {
        return height;
    }
}
