package flow

import (
	"code.ur.gs/lupine/ordoor/internal/ship"
)

func (f *Flow) linkNewGame() {
	// New game
	f.onClick(newGame, "2.1", f.setReturningDriver(newGame, levelPly))  // New campaign button
	f.onClick(newGame, "2.2", f.setReturningDriver(newGame, singles))   // Single scenario button
	f.onClick(newGame, "2.3", f.setReturningDriver(newGame, randomMap)) // Random scenario button
	f.onClick(newGame, "2.4", f.returnToLastDriver(newGame))            // Back button

	f.linkLevelPly()
	f.linkSingles()
	f.linkRandomMap()
}

func (f *Flow) resetLevelPlyInventorySelect() {
	// FIXME: Make the radio button respect changes via setValue
	for _, v := range []string{"2.1", "2.2", "2.3", "2.4"} {
		f.setValueBool(levelPly, v, false)
	}

	switch f.ship.Difficulty {
	case ship.DifficultyLevelMarine:
		f.setValueBool(levelPly, "2.1", true)
	case ship.DifficultyLevelVeteran:
		f.setValueBool(levelPly, "2.2", true)
	case ship.DifficultyLevelHero:
		f.setValueBool(levelPly, "2.3", true)
	case ship.DifficultyLevelMighty:
		f.setValueBool(levelPly, "2.4", true)
	}
}

func (f *Flow) linkLevelPly() {
	f.onClick(levelPly, "2.5", func() { // Back button
		f.resetLevelPlyInventorySelect() // FIXME: should use data binding
		f.returnToLastDriverNow(levelPly)
	})

	// FIXME: we should be able to read the difficulty level from the group
	f.onClick(levelPly, "2.7", func() { // Select button
		if f.valueBool(levelPly, "2.1") {
			f.ship.Difficulty = ship.DifficultyLevelMarine
		}

		if f.valueBool(levelPly, "2.2") {
			f.ship.Difficulty = ship.DifficultyLevelVeteran
		}

		if f.valueBool(levelPly, "2.3") {
			f.ship.Difficulty = ship.DifficultyLevelHero
		}

		if f.valueBool(levelPly, "2.4") {
			// FIXME: we should select a savegame. Mighty Hero disables manual saves.
			f.ship.Difficulty = ship.DifficultyLevelMighty
		}

		f.ship.NextScenario = f.generic.CampaignMaps[0]

		// FIXME: we should show a movie here. Need an internal SMK player first

		f.setDriverNow(bridge)
	})

	// Link children
	f.linkBridge()
}

func (f *Flow) linkSingles() {
	f.onClick(singles, "4.11", f.returnToLastDriver(singles)) // Back button
}

func (f *Flow) linkRandomMap() {
	f.onClick(randomMap, "2.19", f.returnToLastDriver(randomMap)) // Back button
}
