package main

import (
	_ "embed"
	"encoding/json"
	"log"
	"os"
	"path/filepath"
)

type WallpaperType string

const (
	WPTheme WallpaperType = "theme"
	WPImage WallpaperType = "image"
	WPBing  WallpaperType = "bing"
	WPColor WallpaperType = "color"
)

type Layout string

const (
	LayoutSingle Layout = "single"
	LayoutMulti  Layout = "multi"
)

type ThemeName string

const (
	ThemeAurora   ThemeName = "aurora"
	ThemeStar     ThemeName = "starfield"
	ThemeGradient ThemeName = "gradient"
	ThemeParticle ThemeName = "particles"
	ThemeFractal  ThemeName = "fractal"
	ThemeText     ThemeName = "text"
	ThemeAudioViz ThemeName = "audio-viz"
)

type SavedColor struct {
	Color1   string `json:"color1"`
	Color2   string `json:"color2"`
	Gradient bool   `json:"gradient"`
}

type Config struct {
	Zodiac           string        `json:"zodiac"`
	City             string        `json:"city"`
	WallpaperType    WallpaperType `json:"wallpaperType"`
	Theme            ThemeName     `json:"theme"`
	ImagePath        string        `json:"imagePath"`
	Color1           string        `json:"color1"`
	Color2           string        `json:"color2"`
	ColorGradient    bool          `json:"colorGradient"`
	WallpaperText    string        `json:"wallpaperText"`
	Layout           Layout        `json:"layout"`
	HideWallpaper    bool          `json:"hideWallpaper"`
	HideTime         bool          `json:"hideTime"`
	HideWeather      bool          `json:"hideWeather"`
	HideZodiac       bool          `json:"hideZodiac"`
	HideAINews       bool          `json:"hideAINews"`
	ShowSeconds      bool          `json:"showSeconds"`
	PhotoDir         string        `json:"photoDir"`
	PhotoInterval    int           `json:"photoInterval"`
	HidePhoto        bool          `json:"hidePhoto"`
	PhotoFrameMode   bool          `json:"photoFrameMode"`
	KnowledgeKeyword string        `json:"knowledgeKeyword"`
	KnowledgePrompt  string        `json:"knowledgePrompt"`
	HideKnowledge    bool          `json:"hideKnowledge"`
	SavedColors      []SavedColor  `json:"savedColors"`
	BingAutoRefresh  bool          `json:"bingAutoRefresh"`
	AutoStart        bool          `json:"autoStart"`
	QWeatherKey      string        `json:"qweatherKey,omitempty"`
	TianapiKey       string        `json:"tianapiKey,omitempty"`
	CPAKey           string        `json:"cpaKey,omitempty"`
	UpdateURL        string        `json:"updateUrl,omitempty"`
	AutoCheckUpdate  bool          `json:"autoCheckUpdate"`
	SkipVersion      string        `json:"skipVersion,omitempty"`
	AudioVizStyle    string        `json:"audioVizStyle"`
	AudioSensitivity float64       `json:"audioSensitivity"`
	AudioSmoothing   float64       `json:"audioSmoothing"`
	AudioColorScheme string        `json:"audioColorScheme"`
}

const defaultZodiac = "射手座"

var configPath string

//go:embed assets/icons/tray.ico
var trayIcon []byte

func configDir() string {
	return filepath.Dir(configPath)
}

func loadConfig() *Config {
	data, err := os.ReadFile(configPath)
	if err != nil {
		return defaultConfig()
	}
	var cfg Config
	if json.Unmarshal(data, &cfg) != nil {
		return defaultConfig()
	}
	if cfg.Zodiac == "" {
		cfg.Zodiac = defaultZodiac
	}
	if cfg.WallpaperType == "" {
		cfg.WallpaperType = WPTheme
	}
	if cfg.Theme == "" {
		cfg.Theme = ThemeAurora
	}
	if cfg.Color1 == "" {
		cfg.Color1 = "#1a1a2e"
	}
	if cfg.Layout == "" {
		cfg.Layout = LayoutSingle
	}
	return &cfg
}

func defaultConfig() *Config {
	return &Config{
		Zodiac:           defaultZodiac,
		WallpaperType:    WPTheme,
		Theme:            ThemeAurora,
		Color1:           "#1a1a2e",
		Color2:           "#16213e",
		AudioVizStyle:    "bars",
		AudioSensitivity: 1.5,
		AudioSmoothing:   0.7,
		AudioColorScheme: "neon",
	}
}

func saveConfig(cfg *Config) error {
	data, err := json.MarshalIndent(cfg, "", "  ")
	if err != nil {
		log.Println("配置序列化失败:", err)
		return err
	}
	return os.WriteFile(configPath, data, 0644)
}

func getSecret(envName, cfgValue string) string {
	if v := os.Getenv(envName); v != "" {
		return v
	}
	return cfgValue
}

func (c *Config) qweatherKey() string {
	return getSecret("U_DESKTOP_QWEATHER_KEY", c.QWeatherKey)
}

func (c *Config) tianapiKey() string {
	return getSecret("U_DESKTOP_TIANAPI_KEY", c.TianapiKey)
}

func (c *Config) cpaKey() string {
	return getSecret("U_DESKTOP_CPA_KEY", c.CPAKey)
}
