aboutsummaryrefslogtreecommitdiff
path: root/internal/config.go
blob: a8007ac002072c02c3d5c4f8a78f8b0c7c3f048f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package internal

import (
	"encoding/json"
	"fmt"
	"os"
	"path/filepath"
)

type ConfigManager struct {
	config *Config
	path   string
}

func NewConfigManager() (*ConfigManager, error) {
	home, err := os.UserHomeDir()
	if err != nil {
		return nil, err
	}

	dir := filepath.Join(home, ".config", "zmk-battman")
	path := filepath.Join(dir, "config.json")

	if err := os.MkdirAll(dir, 0755); err != nil {
		return nil, err
	}

	cm := &ConfigManager{
		path: path,
		config: &Config{
			UpdateInterval:      30,
			LowBatteryThreshold: 20,
			ShowNotifications:   true,
		},
	}
	cm.Load()
	return cm, nil
}

func (cm *ConfigManager) Load() error {
	data, err := os.ReadFile(cm.path)
	if err != nil {
		return nil // Use defaults if file doesn't exist
	}
	return json.Unmarshal(data, cm.config)
}

func (cm *ConfigManager) Save() error {
	data, err := json.MarshalIndent(cm.config, "", "  ")
	if err != nil {
		return err
	}
	return os.WriteFile(cm.path, data, 0644)
}

func (cm *ConfigManager) SetDevice(name, address string) {
	cm.config.Name = name
	cm.config.Address = address
}

func (cm *ConfigManager) SetInterval(interval int) {
	if interval >= 5 && interval <= 300 {
		cm.config.UpdateInterval = interval
	}
}

func (cm *ConfigManager) SetLowBatteryThreshold(threshold int) {
	if threshold >= 5 && threshold <= 50 {
		cm.config.LowBatteryThreshold = threshold
	}
}

func (cm *ConfigManager) SetNotifications(enabled bool) {
	cm.config.ShowNotifications = enabled
}

func (cm *ConfigManager) IsConfigured() bool {
	return cm.config.Address != ""
}

func (cm *ConfigManager) Get() *Config {
	return cm.config
}

func (cm *ConfigManager) Print() {
	fmt.Printf("Configuration:\n")
	fmt.Printf("  Device: %s (%s)\n", cm.config.Name, cm.config.Address)
	fmt.Printf("  Update interval: %d seconds\n", cm.config.UpdateInterval)
	fmt.Printf("  Low battery threshold: %d%%\n", cm.config.LowBatteryThreshold)
	fmt.Printf("  Notifications: %v\n", cm.config.ShowNotifications)
	fmt.Printf("  Config file: %s\n", cm.path)
}

func (cm *ConfigManager) InitWithDefaults() error {
	home, err := os.UserHomeDir()
	if err != nil {
		return err
	}

	dir := filepath.Join(home, ".config", "zmk-battman")
	cm.path = filepath.Join(dir, "config.json")

	if err := os.MkdirAll(dir, 0755); err != nil {
		return err
	}

	cm.config = &Config{
		UpdateInterval:      30,
		LowBatteryThreshold: 20,
		ShowNotifications:   true,
	}

	return nil
}