//
//  SettingsView.swift
//  MagicCounter
//
//  Created by Atridad Lahiji on 2025-12-06.
//

import SwiftUI

struct SettingsView: View {
    @AppStorage("accentColorName") private var accentColorName = "Blue"
    @AppStorage("hapticFeedbackEnabled") private var hapticFeedbackEnabled = true
    
    private let colors: [(name: String, color: Color)] = [
        ("Blue", .blue),
        ("Purple", .purple),
        ("Pink", .pink),
        ("Red", .red),
        ("Orange", .orange),
        ("Green", .green),
        ("Teal", .teal),
        ("Indigo", .indigo),
        ("Mint", .mint),
        ("Brown", .brown),
        ("Cyan", .cyan)
    ]
    
    private var currentVersion: String {
        Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "1.0"
    }
    
    private var buildNumber: String {
        Bundle.main.infoDictionary?["CFBundleVersion"] as? String ?? "1"
    }
    
    private func foregroundColor(for colorName: String) -> Color {
        switch colorName {
        case "Mint", "Cyan", "Yellow":
            return .black
        default:
            return .white
        }
    }
    
    var body: some View {
        NavigationStack {
            Form {
                Section("General") {
                    Toggle("Haptic Feedback", isOn: $hapticFeedbackEnabled)
                }
                
                Section("Appearance") {
                    VStack(alignment: .leading, spacing: 16) {
                        Text("ACCENT COLOR")
                            .font(.caption)
                            .foregroundStyle(.secondary)
                        
                        LazyVGrid(columns: [GridItem(.adaptive(minimum: 44))], spacing: 12) {
                            ForEach(colors, id: \.name) { item in
                                Circle()
                                    .fill(item.color)
                                    .frame(width: 44, height: 44)
                                    .overlay {
                                        if accentColorName == item.name {
                                            Image(systemName: "checkmark")
                                                .font(.headline)
                                                .foregroundStyle(foregroundColor(for: item.name))
                                        }
                                    }
                                    .onTapGesture {
                                        accentColorName = item.name
                                    }
                            }
                        }
                        
                        Divider()
                        
                        Button("Reset to Default") {
                            accentColorName = "Blue"
                        }
                        .foregroundStyle(.red)
                    }
                    .padding(.vertical, 8)
                }
                
                Section("About") {
                    HStack {
                        Text("App Name")
                        Spacer()
                        Text("Magic Counter")
                            .foregroundStyle(.secondary)
                    }
                    HStack {
                        Text("Version")
                        Spacer()
                        Text("\(currentVersion) (\(buildNumber))")
                            .foregroundStyle(.secondary)
                    }
                }
            }
            .navigationTitle("Settings")
        }
    }
}
