package main

import (
	"flag"
	"image"
	"image/color"
	"image/draw"
	"image/png"
	"os"
	"time"

	"github.com/golang/freetype/truetype"
	"golang.org/x/image/font"
	"golang.org/x/image/font/gofont/gomono"
	"golang.org/x/image/math/fixed"

	"git.nkpl.cc/twocookedfaggots/imageutils/pkg/mesh/wallpaper"
	//	"github.com/droyo/styx"
	//	"io/fs"
)

type Mesh struct {
	image.Image
	X, Y int // divide
}

// using source's color.Model
func (mesh Mesh) ColorModel() color.Model { return mesh.Image.ColorModel() }

// using source's image.Rectangle
func (mesh Mesh) Bounds() image.Rectangle { return mesh.Image.Bounds() }
func (mesh Mesh) At(x, y int) color.Color {
	dx, dy := mesh.Image.Bounds().Dx(), mesh.Image.Bounds().Dy()
	if (x%(dx/mesh.X)) == 0 || (y%(dy/mesh.Y)) == 0 {
		return color.Black
	}
	return mesh.Image.At(x, y)
}

func render(img image.Image) draw.Image {
	dst := image.NewRGBA(img.Bounds())
	draw.Draw(dst, dst.Bounds(), img, image.Point{}, draw.Src)
	return dst
}

func main() {
	//	x := flag.Int("x", 2, "use to grid by x axis")
	//	y := flag.Int("y", 2, "use to grid by y axis")
	flag.Parse() // now flags is ready to use

	img, err := png.Decode(os.Stdin)
	if err != nil {
		panic(err)
	}
	//		Mesh{ // our image modifyer
	//			Image: img,
	//			X:     *x,
	//			Y:     *y,
	//		},

	dst := render(
		img,
	)

	f, err := truetype.Parse(gomono.TTF)
	if err != nil {
		panic(err)
	}
	face := truetype.NewFace(f, &truetype.Options{
		Size:    float64(img.Bounds().Dx() / 19),
		DPI:     72,
		Hinting: font.HintingNone,
	})

	d := &font.Drawer{
		Dst:  dst,
		Src:  image.NewUniform(color.Black),
		Face: face,
		Dot: fixed.Point26_6{fixed.Int26_6(img.Bounds().Dx() /
			9 * 64,
		),
			fixed.Int26_6(img.Bounds().Dy() /
				5 * 64,
			),
		},
	}
	d.DrawString(time.Now().Format(time.Kitchen))

	err = wallpaper.Wallpaper(dst)
	if err != nil {
		panic(err)
	}
}
