package main

import (
	"opslag.de/schobers/allg5"
)

func newDefaultSettings() settings {
	return settings{
		Controls: controls{
			MoveUp:    allg5.KeyUp,
			MoveRight: allg5.KeyRight,
			MoveDown:  allg5.KeyDown,
			MoveLeft:  allg5.KeyLeft,
		},
		Video: video{
			Windowed:    true,
			DisplayMode: "",
		},
	}
}

type settings struct {
	Controls controls
	Video    video
}

func (s *settings) DefaultPath() (string, error) { return userFile("settings.json") }

func (s *settings) Load(path string) error {
	var fromFile settings
	err := decodeJSON(path, &fromFile)
	if err != nil {
		return err
	}
	*s = fromFile
	return nil
}

func (s *settings) LoadDefault() error {
	path, err := s.DefaultPath()
	if err != nil {
		return err
	}
	return s.Load(path)
}

func (s *settings) Store(path string) error { return encodeJSON(path, s) }

func (s *settings) StoreDefault() error {
	path, err := s.DefaultPath()
	if err != nil {
		return err
	}
	return s.Store(path)
}

type controls struct {
	MoveUp    allg5.Key
	MoveRight allg5.Key
	MoveDown  allg5.Key
	MoveLeft  allg5.Key
}

type video struct {
	Windowed    bool
	DisplayMode string
}

func (c controls) MovementKeys() []allg5.Key {
	return []allg5.Key{c.MoveUp, c.MoveRight, c.MoveDown, c.MoveLeft}
}
