package assetstore

import (
	"image"

	"github.com/hajimehoshi/ebiten"
)

// These are just offsets into the Cursors.cur file
type CursorName int

type Cursor struct {
	Hotspot image.Point
	Image   *ebiten.Image
}

const (
	UltPointer   CursorName = 0
	ChaosPointer CursorName = 1
	UltWaiter    CursorName = 2
	ChaosWaiter  CursorName = 3

	// I think these cursors are used in drag + drop
	ChaosMarine1 CursorName = 4
	ChaosMarine2 CursorName = 5
	ChaosMarine3 CursorName = 6

	UltMarine1 CursorName = 7
	UltMarine2 CursorName = 8
	UltMarine3 CursorName = 9
	UltMarine4 CursorName = 10
	UltMarine5 CursorName = 11

	ChaosHeavy1 CursorName = 12
	ChaosHeavy2 CursorName = 13

	UltHeavy1 CursorName = 14
	UltHeavy2 CursorName = 15
	UltHeavy3 CursorName = 16
	UltHeavy4 CursorName = 17
	UltHeavy5 CursorName = 18
	UltHeavy6 CursorName = 19

	ChaosTerminator1 CursorName = 20
	ChaosTerminator2 CursorName = 21

	UltTerminator1 CursorName = 22
	UltTerminator2 CursorName = 23
	UltTerminator3 CursorName = 24
	UltTerminator4 CursorName = 25
	UltTerminator5 CursorName = 26

	Deny CursorName = 27 // Red X

	UltLogo         CursorName = 28
	UltSquadMarine  CursorName = 29
	UltSquadHeavy   CursorName = 30
	UltSquadAssault CursorName = 31

	UltCaptain    CursorName = 32
	UltChaplain   CursorName = 33 // (maybe?)
	UltApothecary CursorName = 34
	UltTechmarine CursorName = 35
	UltLibrarian  CursorName = 36

	DenyAgain CursorName = 37 // Identical to Deny as far as I can see *shrug*
)

func (a *AssetStore) Cursor(name CursorName) (*Cursor, error) {
	if cur, ok := a.cursors[name]; ok {
		return cur, nil
	}

	if a.cursorObj == nil {
		filename, err := a.lookup("Cursors.cur", "", "Cursor")
		if err != nil {
			return nil, err
		}

		obj, err := a.ObjectByPath(filename)
		if err != nil {
			return nil, err
		}

		a.cursorObj = obj
	}

	spr, err := a.cursorObj.Sprite(int(name))
	if err != nil {
		return nil, err
	}

	// TODO: hotspot info. We're using Cursor.cur because it's object format,
	// but we do also have .ani files that might contain hotspots.
	cur := &Cursor{Image: spr.Image}

	a.cursors[name] = cur

	return cur, nil
}
