//
//  SettingsAccountView.swift
//  punchnet
//
//  Created by 安礼成 on 2026/1/16.
//
import SwiftUI

struct SettingsAccountView: View {
    @Environment(AppContext.self) var appContext: AppContext
    @Environment(\.openWindow) var openWindow
    @Environment(\.openURL) var openURL
    
    var body: some View {
        ScrollView(.vertical, showsIndicators: false) {
            VStack(alignment: .leading, spacing: 24) {
                // MARK: - 账户部分
                sectionHeader(title: "账户安全", icon: "shield.lefthalf.filled")
                
                VStack(spacing: 0) {
                    if let loginCredit = appContext.loginCredit {
                        switch loginCredit {
                        case .token(let token, _):
                            TokenCreditView(token: token)
                        case .accountAndPasword(let account, _, _):
                            AccountCreditView(username: account)
                        }
                    } else {
                        // 蓝色主按钮
                        Button {
                            self.openWindow(id: "main")
                        } label: {
                            Text("登录")
                                .fontWeight(.medium)
                        }
                        .buttonStyle(.borderedProminent)
                    }
                }
                .background(Color.primary.opacity(0.03))
                .cornerRadius(12)
                .overlay(RoundedRectangle(cornerRadius: 12).stroke(Color.primary.opacity(0.05), lineWidth: 1))
            }
            .frame(maxWidth: 600) // 限制宽度防止在大屏幕上显得太散
        }
    }
    
    // 辅助头部组件
    private func sectionHeader(title: String, icon: String) -> some View {
        HStack {
            Image(systemName: icon)
                .foregroundColor(.blue)
            
            Text(title)
                .font(.system(size: 16, weight: .bold))
        }
        .padding(.leading, 4)
    }
}

// MARK: - 内部视图扩展
// 统一的条目容器样式
private struct AccountRow: View {
    let icon: String
    let title: String
    let subtitle: String
    var actions: AnyView
    
    var body: some View {
        HStack(spacing: 16) {
            // 模拟 Logo
            Circle()
                .fill(Color.blue.gradient)
                .frame(width: 32, height: 32)
                .overlay(
                    Image(systemName: icon)
                        .foregroundColor(.white)
                        .font(.system(size: 14))
                )
            
            VStack(alignment: .leading, spacing: 2) {
                Text(title)
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                
                Text(subtitle)
                    .font(.system(size: 15, weight: .medium, design: .monospaced))
                    .lineLimit(1)
            }
            
            Spacer()
            
            actions
        }
        .padding(16)
    }
}

private struct AccountCreditView: View {
    @Environment(AppContext.self) var appContext: AppContext
    @Environment(\.openWindow) var openWindow
    @Environment(\.dismissWindow) var dismissWindow
    
    let username: String
    
    var body: some View {
        AccountRow(icon: "person.fill", title: "当前登录账号", subtitle: username, actions: AnyView(
            HStack(spacing: 12) {
                //                    Button("修改密码") {
                //
                //                    }
                //                    .buttonStyle(.link)
                
                Button("退出登录") {
                    Task { @MainActor in
                        try await appContext.logout()
                    }
                    self.appContext.appScene = .login(username: username)
                    self.dismissWindow(id: "settings")
                    self.openWindow(id: "main")
                }
                .buttonStyle(.bordered)
                .foregroundColor(.red)
            }
        ))
        
    }
}

private struct TokenCreditView: View {
    @Environment(AppContext.self) var appContext: AppContext
    @Environment(\.openWindow) var openWindow
    @Environment(\.dismissWindow) var dismissWindow
    
    let token: String
    
    var body: some View {
        AccountRow(icon: "key.horizontal.fill", title: "Token 登录", subtitle: token, actions: AnyView(
            Button("退出登录") {
                Task { @MainActor in
                    try await appContext.logout()
                }
                
                self.appContext.appScene = .login(username: nil)
                self.dismissWindow(id: "settings")
                self.openWindow(id: "main")
            }
                .buttonStyle(.bordered)
                .foregroundColor(.red)
        ))
    }
}
