package gfx

import (
	"image"
	"image/color"
	"image/gif"
	"io"
	"math/rand"
)

func CreateNoise(out io.Writer) {

	var palette = []color.Color{color.White, color.Black}

	const (
		whiteIdx = 0
		blackIdx = 1
	)

	const (
		size    = 30 // [-size, size]
		nframes = 10
		delay   = 2
	)

	anim := gif.GIF{}
	for i := 0; i < nframes; i++ {
		rect := image.Rect(0, 0, 2*size+1, 2*size+1)
		img := image.NewPaletted(rect, palette)
		for y := -size; y <= size; y++ {
			for x := -size; x <= size; x++ {
				xPx := x + size
				yPx := y + size
				clrIdx := rand.Intn(len(palette))
				img.SetColorIndex(xPx, yPx, uint8(clrIdx))
			}
		}
		anim.Delay = append(anim.Delay, delay)
		anim.Image = append(anim.Image, img)
	}

	gif.EncodeAll(out, &anim)
}
