package dithering

import (
	"image"
	"image/color"
	"image/png"
	"math/rand"
	"os"
	"os/exec"
	"testing"
	"time"

	"git.nkpl.cc/twocookedfaggots/imageutils"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func callImage(img image.Image) {
	for y := 0; y < img.Bounds().Dy(); y++ {
		for x := 0; x < img.Bounds().Dx(); x++ {
			_ = img.At(x, y)
		}
	}
}

func TestGrid(t *testing.T) {
	command := exec.Command("imv", "-")
	w, err := command.StdinPipe()
	if err != nil {
		t.Fatal(err)
	}
	err = command.Start()
	if err != nil {
		t.Fatal(err)
	}

	n := uint8(rand.Int() % 256)
	t.Log(n)
	img := imageutils.Scale(1<<6, grid{color.Gray{n}})
	err = png.Encode(w, img)
	if err != nil {
		t.Fatal(err)
	}

	callImage(grid{color.Gray{4}})
	callImage(grid{color.Gray{0}})
}

func TestDithering(t *testing.T) {
	r, err := os.Open("/home/potassium/documents/wallpaper/8.png")
	if err != nil {
		t.Fatal(err)
	}
	defer r.Close()

	w, err := os.Create("sample.png")
	if err != nil {
		t.Fatal(err)
	}
	defer w.Close()

	img, err := png.Decode(r)
	if err != nil {
		t.Fatal(err)
	}

	err = png.Encode(w, Dithering{img})
	if err != nil {
		t.Fatal(err)
	}
}
