package thumb

import (
	"strings"

	"github.com/photoprism/photoprism/pkg/fs"
)

// Name represents a thumbnail size name.
type Name string

// Jpeg returns the thumbnail name with a jpeg file extension suffix as string.
func (n Name) Jpeg() string {
	return string(n) + fs.ExtJpeg
}

// String returns the thumbnail name as string.
func (n Name) String() string {
	return string(n)
}

// Names of thumbnail sizes.
const (
	Colors   Name = "colors"
	Tile50   Name = "tile_50"
	Tile100  Name = "tile_100"
	Left224  Name = "left_224"
	Right224 Name = "right_224"
	Tile224  Name = "tile_224"
	Left384  Name = "left_384"
	Right384 Name = "right_384"
	Tile384  Name = "tile_384"
	Fit720   Name = "fit_720"
	Tile480  Name = "tile_480"
	Left480  Name = "left_480"
	Right480 Name = "right_480"
	Tile500  Name = "tile_500"
	Tile1080 Name = "tile_1080"
	Fit1280  Name = "fit_1280"
	Fit1600  Name = "fit_1600"
	Fit1920  Name = "fit_1920"
	Fit2048  Name = "fit_2048"
	Fit2560  Name = "fit_2560"
	Fit3840  Name = "fit_3840"
	Fit4096  Name = "fit_4096"
	Fit5120  Name = "fit_5120"
	Fit7680  Name = "fit_7680"
)

// Names contains all default size names.
var Names = []Name{
	Fit7680,
	Fit5120,
	Fit4096,
	Fit2560,
	Fit1920,
	Fit1280,
	Tile500,
	Fit720,
	Tile224,
	Right224,
	Left224,
	Tile100,
	Tile50,
	Colors,
}

// Find returns the largest thumbnail type for the given pixel size.
func Find(pixels int) (name Name, size Size) {
	for _, name = range Names {
		t := Sizes[name]

		if t.Width <= pixels && t.Height <= pixels {
			return name, t
		}
	}

	return "", Size{}
}

// Vision returns a suitable tile size for computer vision applications.
func Vision(resolution int) (size Size) {
	// If specifically requested, return the 720x720 fit size,
	// which should always exist.
	if resolution == SizeFit720.Width {
		return SizeFit720
	}

	// Check existing tile sizes.
	for _, size = range All {
		if size.Height != size.Width {
			continue
		} else if !strings.HasPrefix(size.Name.String(), "tile_") {
			continue
		}

		if size.Width >= resolution {
			return size
		}
	}

	// If no other size matches,
	// return the default size.
	return SizeTile224
}
