package profilebtn

import (
	"image"
	"image/color"
	"image/draw"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

type profileBtn struct {
	widget.BaseWidget

	OutlineWidth int
	OutlineColor color.Color

	profileImage image.Image
}

func (b *profileBtn) Resize(s fyne.Size) {
	b.BaseWidget.Resize(s)
}

func (b *profileBtn) CreateRenderer() fyne.WidgetRenderer {
	return NewProfileBtnRenderer(b)
}

func NewProfileBtnRenderer(b *profileBtn) *profileBtnRenderer {
	r := &profileBtnRenderer{
		canvas.NewRasterFromImage(image.NewRGBA(image.Rect(0, 0, 10, 10))),
		b,
	}
	b.ExtendBaseWidget(b)
	return r
}

func NewProfileBtn() *profileBtn {
	w := &profileBtn{}
	w.ExtendBaseWidget(w)
	w.OutlineWidth = 0
	w.OutlineColor = color.White
	return w
}

func (b *profileBtn) SetProfileResource(r fyne.Resource) {
	b.profileImage = canvas.NewImageFromResource(r).Image
}

func (b *profileBtn) SetProfileImage(i image.Image) {
	b.profileImage = i
}

type profileBtnRenderer struct {
	rast *canvas.Raster
	w    *profileBtn
}

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}
func minf(x, y float32) float32 {
	if x < y {
		return x
	}
	return y
}

func (r *profileBtnRenderer) Render(w, h int) image.Image {
	i := image.NewRGBA(image.Rect(0, 0, w, h))
	if r.w.profileImage != nil {
		src := r.w.profileImage
		b := src.Bounds().Size()
		wh := image.NewUniform(r.w.OutlineColor)
		dst := image.NewRGBA(image.Rect(0, 0, b.X, b.Y))
		draw.DrawMask(dst, dst.Bounds(), wh, image.ZP, &circle{image.Point{b.X / 2, b.Y / 2}, (min(b.Y, b.Y) / 2)}, image.ZP, draw.Over)
		draw.DrawMask(dst, dst.Bounds(), src, image.ZP, &circle{image.Point{b.X / 2, b.Y / 2}, (min(b.Y, b.Y) / 2) - r.w.OutlineWidth}, image.ZP, draw.Over)
		return dst
	} else {
		return i
	}
}

func (r *profileBtnRenderer) MinSize() fyne.Size {
	return fyne.NewSize(theme.Padding()*4, theme.Padding()*4)
}

func (r *profileBtnRenderer) Layout(size fyne.Size) {
	squareSize := fyne.NewSize(minf(size.Width, size.Height), minf(size.Width, size.Height))
	r.rast.Resize(squareSize)
	if squareSize.Width < size.Width {
		r.rast.Move(fyne.NewPos((size.Width-squareSize.Width)/2, 0))
	} else if squareSize.Height < size.Height {
		r.rast.Move(fyne.NewPos(0, (size.Height-squareSize.Height)/2))
	}
}

func (r *profileBtnRenderer) ApplyTheme() {
	r.Refresh()
}

func (r *profileBtnRenderer) BackgprofileColor() color.Color {
	return theme.ButtonColor()
}

func (r *profileBtnRenderer) Refresh() {
	r.rast = canvas.NewRaster(r.Render)
	r.Layout(r.w.Size())
}

func (r *profileBtnRenderer) Destroy() {
}

func (r *profileBtnRenderer) Objects() []fyne.CanvasObject {
	r.Refresh()
	return []fyne.CanvasObject{r.rast}
}
