package main

import (
	"flag"
	"image"
	"log"
	"math"
	"os"

	"github.com/hajimehoshi/ebiten"

	"code.ur.gs/lupine/ordoor/internal/assetstore"
	"code.ur.gs/lupine/ordoor/internal/ui"
)

var (
	gamePath = flag.String("game-path", "./orig", "Path to a WH40K: Chaos Gate installation")
	objFile  = flag.String("obj-file", "", "Path of an .obj file, e.g. ./orig/Obj/TZEENTCH.OBJ")
	objName  = flag.String("obj-name", "", "Name of an .obj file, e.g. TZEENTCH")
	sprIdx   = flag.Int("spr-idx", 0, "Sprite index to start at")

	winX = flag.Int("win-x", 1280, "Pre-scaled window X dimension")
	winY = flag.Int("win-y", 1024, "Pre-scaled window Y dimension")
)

type env struct {
	obj  *assetstore.Object
	spr  *assetstore.Sprite
	step int

	state     state
	lastState state
}

type state struct {
	spriteIdx int

	zoom   float64
	origin image.Point
}

func main() {
	flag.Parse()

	if *gamePath == "" || (*objName == "" && *objFile == "") {
		flag.Usage()
		os.Exit(1)
	}

	assets, err := assetstore.New(*gamePath)
	if err != nil {
		log.Fatal("Failed to set up asset store: %v", err)
	}

	var obj *assetstore.Object
	if *objName != "" {
		obj, err = assets.Object(*objName)
	} else {
		obj, err = assets.ObjectByPath(*objFile)
	}
	if err != nil {
		log.Fatalf("Failed to load %s%s: %v", *objName, *objFile, err)
	}

	state := state{
		zoom:      6.0,
		origin:    image.Point{0, 0},
		spriteIdx: *sprIdx,
	}

	env := &env{
		obj:       obj,
		state:     state,
		lastState: state,
	}

	win, err := ui.NewWindow(env, "View Object: "+*objName, *winX, *winY)
	if err != nil {
		log.Fatal(err)
	}

	win.OnKeyUp(ebiten.KeyMinus, env.changeSprite(-1))
	win.OnKeyUp(ebiten.KeyEqual, env.changeSprite(+1))
	win.OnKeyUp(ebiten.KeyLeft, env.changeOrigin(+4, +0))
	win.OnKeyUp(ebiten.KeyRight, env.changeOrigin(-4, +0))
	win.OnKeyUp(ebiten.KeyUp, env.changeOrigin(+0, +4))
	win.OnKeyUp(ebiten.KeyDown, env.changeOrigin(+0, -4))
	win.OnMouseWheel(env.changeZoom)

	// The main thread now belongs to ebiten
	if err := win.Run(); err != nil {
		log.Fatal(err)
	}
}

func (e *env) Update(screenX, screenY int) error {
	if e.step == 0 || e.lastState != e.state {
		sprite, err := e.obj.Sprite(e.state.spriteIdx)
		if err != nil {
			return err
		}
		e.spr = sprite

		log.Printf(
			"new state: sprite=%d/%d bounds=%+#v zoom=%.2f, origin=%+v",
			e.state.spriteIdx,
			e.obj.NumSprites,
			e.spr.Rect,
			e.state.zoom,
			e.state.origin,
		)
	}

	// This should be the final action
	e.step += 1
	e.lastState = e.state

	return nil
}

func (e *env) Draw(screen *ebiten.Image) error {
	sprite, err := e.obj.Sprite(e.state.spriteIdx)
	if err != nil {
		return err
	}

	cam := ebiten.GeoM{}
	cam.Translate(float64(e.state.origin.X), float64(e.state.origin.Y)) // Move to origin
	cam.Scale(e.state.zoom, e.state.zoom)                               // apply current zoom factor

	return screen.DrawImage(sprite.Image, &ebiten.DrawImageOptions{GeoM: cam})
}

func (e *env) changeSprite(by int) func() {
	return func() {
		e.state.spriteIdx += by

		if e.state.spriteIdx < 0 {
			e.state.spriteIdx = 0
		}

		if e.state.spriteIdx > e.obj.NumSprites-1 {
			e.state.spriteIdx = e.obj.NumSprites - 1
		}
	}
}

func (e *env) changeOrigin(byX, byY int) func() {
	return func() {
		e.state.origin.X += byX
		e.state.origin.Y += byY
	}
}

func (e *env) changeZoom(_, y float64) {
	// Zoom in and out with the mouse wheel
	e.state.zoom *= math.Pow(1.2, y)
}
