package app

import (
	"os"
	"path/filepath"
	"testing"

	"github.com/stretchr/testify/assert"

	"fyne.io/fyne/theme"
)

func TestSettingsLoad(t *testing.T) {
	settings := &settings{}

	err := settings.loadFromFile(filepath.Join("testdata", "light-theme.json"))
	if err != nil {
		t.Error(err)
	}

	assert.Equal(t, "light", settings.schema.ThemeName)

	err = settings.loadFromFile(filepath.Join("testdata", "dark-theme.json"))
	if err != nil {
		t.Error(err)
	}

	assert.Equal(t, "dark", settings.schema.ThemeName)
}

func TestOverrideTheme(t *testing.T) {
	set := &settings{}
	set.setupTheme()
	assert.Equal(t, defaultTheme(), set.Theme())

	set.schema.ThemeName = "light"
	set.setupTheme()
	assert.Equal(t, theme.LightTheme(), set.Theme())

	set.schema.ThemeName = "dark"
	set.setupTheme()
	assert.Equal(t, theme.DarkTheme(), set.Theme())

	set = &settings{}
	set.setupTheme()
	assert.Equal(t, defaultTheme(), set.Theme())

	err := os.Setenv("FYNE_THEME", "light")
	if err != nil {
		t.Error(err)
	}
	set.setupTheme()
	assert.Equal(t, theme.LightTheme(), set.Theme())
	err = os.Setenv("FYNE_THEME", "")
	if err != nil {
		t.Error(err)
	}
}

func TestOverrideTheme_IgnoresSettingsChange(t *testing.T) {
	// check that a file-load does not overwrite our value
	set := &settings{}
	err := os.Setenv("FYNE_THEME", "light")
	if err != nil {
		t.Error(err)
	}
	set.setupTheme()
	assert.Equal(t, theme.LightTheme(), set.Theme())
	err = os.Setenv("FYNE_THEME", "")
	if err != nil {
		t.Error(err)
	}

	err = set.loadFromFile(filepath.Join("testdata", "dark-theme.json"))
	if err != nil {
		t.Error(err)
	}

	set.setupTheme()
	assert.Equal(t, theme.LightTheme(), set.Theme())
}
