package ax

import (
	"surdeus.su/core/gg"
)

// Unique identifier for animation
// in the animation set.
type AnimationId int

// The type describes set of animations
// to switch between.
type AnimationSet map[AnimationId] Animation

// Make new animation set from an image.
func AnimationSetFromImage(
	img *gg.Image,
	// Width and height of one frame respectively.
	w, h int,
	// Gaps for X and Y respectively.
	gx, gy int,
	defines ...AnimationDefine,
) (AnimationSet, error) {
	set := AnimationSet{}

	r := img.Bounds()
	fw, fh := r.Dx()/w, r.Dy()/h

	for _, define := range defines {
		animation := make(Animation, len(define.Indexes))
		for i := range animation {
			idx := define.Indexes[i]
			rect := gg.ImageRect{
				Min: gg.ImagePoint{
					idx.X*fw+gx,
					idx.Y*fh+gy,
				},
				Max: gg.ImagePoint{
					(idx.X+1)*fw-gx,
					(idx.Y+1)*fh-gy,
				},
			}
			subImg := img.SubImage(rect)
			animation[i] = gg.NewImageFromImage(subImg)
		}
		set[define.Id] = animation
	}

	return set, nil
}