//
//  SettingsSystemView.swift
//  punchnet
//
//  Created by 安礼成 on 2026/1/19.
//
import SwiftUI

struct SettingsSystemView: View {
    // 启动管理
    @State private var launchManager = LaunchManager()
    
    // 为每个设置项提供独立的状态
    @AppStorage("autoConnect") private var autoConnect: Bool = false
    @AppStorage("autoUpdate") private var autoUpdate: Bool = true

    @State private var showMainUI: Bool = true
    
    var body: some View {
        ScrollView(.vertical, showsIndicators: false) {
            VStack(alignment: .leading, spacing: 28) {
                
                // MARK: - 启动行为设置
                systemSectionHeader(title: "启动与运行", icon: "power.circle.fill")
                
                VStack(spacing: 0) {
                    ToggleRow(icon: "macwindow.badge.plus", title: "开机时自动启动", isOn: Binding(
                        get: {
                            launchManager.launchAtLogin
                        },
                        set: { newValue in
                            do {
                                try launchManager.toggleLaunchAtLogin(enabled: newValue)
                            } catch let err {
                                NSLog("toggle get error: \(err)")
                            }
                        }
                    ))
                    
                    Divider()
                        .padding(.leading, 48) // 为图标留出间距
                    
                    ToggleRow(icon: "bolt.horizontal.icloud.fill", title: "应用启动后自动连接", isOn: $autoConnect)
                    
                    Divider()
                        .padding(.leading, 48)
                    
                    ToggleRow(icon: "macwindow", title: "启动时显示主界面", isOn: $showMainUI)
                }
                .background(Color.primary.opacity(0.03))
                .cornerRadius(12)
                .overlay(RoundedRectangle(cornerRadius: 12).stroke(Color.primary.opacity(0.05), lineWidth: 1))
                
                // MARK: - 软件维护
                systemSectionHeader(title: "软件更新", icon: "arrow.clockwise.circle.fill")
                
                VStack(spacing: 0) {
                    ToggleRow(icon: "arrow.down.circle.fill", title: "自动下载并安装更新", isOn: $autoUpdate)
                }
                .background(Color.primary.opacity(0.03))
                .cornerRadius(12)
                .overlay(RoundedRectangle(cornerRadius: 12).stroke(Color.primary.opacity(0.05), lineWidth: 1))
                
                // MARK: - 底部版本提示
                Text("当前版本：1.0.4 (Build 202603) - 已是最新版本")
                    .font(.caption)
                    .foregroundColor(.secondary)
                    .padding(.horizontal, 4)
                
                Spacer()
            }
            .padding(32)
            .frame(maxWidth: 600, alignment: .leading)
        }
        .onAppear {
            
        }
    }
    
    // 辅助头部
    private func systemSectionHeader(title: String, icon: String) -> some View {
        HStack {
            Image(systemName: icon)
                .foregroundColor(.blue)
                .font(.system(size: 14, weight: .semibold))
            Text(title)
                .font(.system(size: 15, weight: .bold))
                .foregroundColor(.secondary)
        }
        .padding(.leading, 4)
    }
}

// MARK: - 子组件：开关行
private struct ToggleRow: View {
    let icon: String
    let title: String
    @Binding var isOn: Bool
    
    var body: some View {
        HStack(spacing: 16) {
            // 图标容器
            ZStack {
                RoundedRectangle(cornerRadius: 6, style: .continuous)
                    .fill(Color.blue.opacity(0.1))
                    .frame(width: 28, height: 28)
                
                Image(systemName: icon)
                    .font(.system(size: 14, weight: .medium))
                    .foregroundColor(.blue)
            }
            
            Text(title)
                .font(.system(size: 14))
            
            Spacer()
            
            Toggle("", isOn: $isOn)
                .toggleStyle(.switch) // 强制使用 macOS 切换样式
                .labelsHidden() // 隐藏自带的 Label 以便我们自定义布局
        }
        .padding(.horizontal, 16)
        .padding(.vertical, 12)
    }
}

#Preview {
    SettingsSystemView()
}
