package assetstore

import (
	"path/filepath"
	"strings"

	"code.ur.gs/lupine/ordoor/internal/config"
	"code.ur.gs/lupine/ordoor/internal/data"
)

// Generic returns a struct containing a grab-bag of otherwise-unrelated data
// TODO: it would be nice if this could be cleaner
func (a *AssetStore) Generic() (*data.Generic, error) {
	if a.generic != nil {
		return a.generic, nil
	}

	filename, err := a.lookup("GenericData", "dat", "Data")
	if err != nil {
		return nil, err
	}

	generic, err := data.LoadGeneric(filename)
	if err != nil {
		return nil, err
	}

	// These changes are made so data in generic plays nicer with assetstore
	for i, filename := range generic.CampaignMaps {
		generic.CampaignMaps[i] =
			strings.TrimSuffix(filename, filepath.Ext(filename))
	}

	a.generic = generic

	return generic, nil
}

func (a *AssetStore) DefaultOptions() (*config.Options, error) {
	cfg := &config.Options{}
	g, err := a.Generic()
	if err != nil {
		return nil, err
	}

	cfg.PlayMovies = intToBool(g.Options[data.OptionMovies])
	cfg.PlayMusic = intToBool(g.Options[data.OptionMusic])
	cfg.CombatVoices = intToBool(g.Options[data.OptionCombatVoices])
	cfg.ShowGrid = intToBool(g.Options[data.OptionGrid])
	cfg.ShowPaths = intToBool(g.Options[data.OptionShowPaths])
	cfg.PointSaving = intToBool(g.Options[data.OptionPointSave])
	cfg.AutoCutLevel = intToBool(g.Options[data.OptionAutoCutLevel])
	cfg.Animations = intToBool(g.Options[data.OptionShowUnitAnimations])

	// These are overrides of data.OptionCombatResolution. *This* default from
	// 1998 is no good at all!
	cfg.XRes = 1280
	cfg.YRes = 1024

	cfg.MusicVolume = g.Options[data.OptionMusicVolume]
	cfg.SFXVolume = g.Options[data.OptionSoundEffectsVolume]

	cfg.UnitSpeed = g.Options[data.OptionUnitAnimationSpeed]
	cfg.AnimSpeed = g.Options[data.OptionEffectAnimationSpeed]

	return cfg, nil
}

func intToBool(i int) bool {
	return i > 0
}
