import SwiftUI

struct SettingsView: View {
    @EnvironmentObject private var languageManager: LanguageManager
    @State private var showLanguageOptions = false
    
    var body: some View {
        ZStack {
            // 背景渐变
            LinearGradient(
                gradient: Gradient(colors: [
                    Color(.systemBackground),
                    Color(.systemGray6).opacity(0.2)
                ]),
                startPoint: .top,
                endPoint: .bottom
            )
            .ignoresSafeArea()
            
            ScrollView {
                VStack(spacing: 24) {
                    // 顶部图标
                    VStack(spacing: 16) {
                        ZStack {
                            Circle()
                                .fill(
                                    LinearGradient(
                                        gradient: Gradient(colors: [
                                            Color.blue.opacity(0.1),
                                            Color.blue.opacity(0.05)
                                        ]),
                                        startPoint: .topLeading,
                                        endPoint: .bottomTrailing
                                    )
                                )
                                .frame(width: 80, height: 80)
                            
                            Image(systemName: "gearshape.fill")
                                .font(.system(size: 36, weight: .light))
                                .foregroundColor(.blue)
                        }
                    }
                    .padding(.top, 20)
                    
                    // 语言设置卡片
                    VStack(alignment: .leading, spacing: 16) {
                        HStack {
                            Image(systemName: "globe")
                                .font(.system(size: 20, weight: .medium))
                                .foregroundColor(.blue)
                                .frame(width: 32)
                            
                            VStack(alignment: .leading, spacing: 4) {
                                Text("language_settings".localized)
                                    .font(.system(size: 18, weight: .semibold))
                                    .id(languageManager.refreshTrigger)
                                Text("select_app_language".localized)
                                    .id(languageManager.refreshTrigger)
                                    .font(.system(size: 14))
                                    .foregroundColor(.secondary)
                            }
                            
                            Spacer()
                        }
                        
                        VStack(spacing: 0) {
                            // 当前选中的语言显示
                            Button(action: {
                                withAnimation(.easeInOut(duration: 0.2)) {
                                    showLanguageOptions.toggle()
                                }
                            }) {
                                HStack {
                                    Text(languageManager.currentLanguage.flag)
                                        .font(.system(size: 16))
                                    
                                    Text(languageManager.currentLanguage.displayName)
                                        .font(.system(size: 16))
                                        .foregroundColor(.primary)
                                    
                                    Spacer()
                                    
                                    Image(systemName: showLanguageOptions ? "chevron.up" : "chevron.down")
                                        .font(.system(size: 12, weight: .medium))
                                        .foregroundColor(.secondary)
                                }
                                .padding(.vertical, 12)
                                .padding(.horizontal, 16)
                                .background(
                                    RoundedRectangle(cornerRadius: 12)
                                        .fill(Color(.systemGray6))
                                )
                            }
                            .buttonStyle(PlainButtonStyle())
                            
                            // 语言选项列表
                            if showLanguageOptions {
                                VStack(spacing: 0) {
                                    ForEach(Language.allCases, id: \.self) { language in
                                        Button(action: {
                                            languageManager.switchLanguage(to: language)
                                            withAnimation(.easeInOut(duration: 0.2)) {
                                                showLanguageOptions = false
                                            }
                                        }) {
                                            HStack {
                                                Text(language.flag)
                                                    .font(.system(size: 16))
                                                
                                                Text(language.displayName)
                                                    .font(.system(size: 16))
                                                    .foregroundColor(.primary)
                                                
                                                Spacer()
                                                
                                                if languageManager.currentLanguage == language {
                                                    Image(systemName: "checkmark")
                                                        .font(.system(size: 14, weight: .medium))
                                                        .foregroundColor(.blue)
                                                }
                                            }
                                            .padding(.vertical, 12)
                                            .padding(.horizontal, 16)
                                            .background(
                                                RoundedRectangle(cornerRadius: 0)
                                                    .fill(languageManager.currentLanguage == language ? Color.blue.opacity(0.1) : Color(.systemBackground))
                                            )
                                        }
                                        .buttonStyle(PlainButtonStyle())
                                        
                                        if language != Language.allCases.last {
                                            Divider()
                                                .padding(.leading, 44)
                                        }
                                    }
                                }
                                .background(
                                    RoundedRectangle(cornerRadius: 12)
                                        .fill(Color(.systemBackground))
                                        .shadow(color: .black.opacity(0.1), radius: 4, x: 0, y: 2)
                                )
                                .transition(.opacity.combined(with: .scale(scale: 0.95, anchor: .top)))
                            }
                        }
                    }
                    .padding(20)
                    .background(
                        RoundedRectangle(cornerRadius: 16)
                            .fill(Color(.systemBackground))
                            .shadow(color: .black.opacity(0.05), radius: 8, x: 0, y: 2)
                    )
                    .padding(.horizontal, 20)
                    
                    // 应用信息卡片
                    VStack(alignment: .leading, spacing: 16) {
                        HStack {
                            Image(systemName: "info.circle")
                                .font(.system(size: 20, weight: .medium))
                                .foregroundColor(.green)
                                .frame(width: 32)
                            
                            Text("app_info".localized)
                                .font(.system(size: 18, weight: .semibold))
                            
                            Spacer()
                        }
                        
                        VStack(spacing: 12) {
                            HStack {
                                Text("version".localized)
                                    .font(.system(size: 16))
                                    .foregroundColor(.secondary)
                                Spacer()
                                Text("version_number".localized)
                                    .font(.system(size: 16, weight: .medium))
                            }
                        }
                    }
                    .padding(20)
                    .background(
                        RoundedRectangle(cornerRadius: 16)
                            .fill(Color(.systemBackground))
                            .shadow(color: .black.opacity(0.05), radius: 8, x: 0, y: 2)
                    )
                    .padding(.horizontal, 20)
                    
                    // 应用授权卡片
                    NavigationLink(destination: AppPermissionsView().environmentObject(languageManager)) {
                        VStack(alignment: .leading, spacing: 16) {
                            HStack {
                                Image(systemName: "lock.shield")
                                    .font(.system(size: 20, weight: .medium))
                                    .foregroundColor(.blue)
                                    .frame(width: 32)
                                
                                Text("app_permissions".localized)
                                    .font(.system(size: 18, weight: .semibold))
                                
                                Spacer()
                                
                                Image(systemName: "chevron.right")
                                    .font(.system(size: 14, weight: .medium))
                                    .foregroundColor(.secondary)
                            }
                            
                            Text("manage_app_permissions".localized)
                                .font(.system(size: 14))
                                .foregroundColor(.secondary)
                                .lineLimit(nil)
                        }
                        .padding(20)
                        .background(
                            RoundedRectangle(cornerRadius: 16)
                                .fill(Color(.systemBackground))
                                .shadow(color: .black.opacity(0.05), radius: 8, x: 0, y: 2)
                        )
                    }
                    .buttonStyle(PlainButtonStyle())
                    .padding(.horizontal, 20)
                    
                    // 隐私政策卡片
                    NavigationLink(destination: PrivacyPolicyView().environmentObject(languageManager)) {
                        VStack(alignment: .leading, spacing: 16) {
                            HStack {
                                Image(systemName: "hand.raised.fill")
                                    .font(.system(size: 20, weight: .medium))
                                    .foregroundColor(.green)
                                    .frame(width: 32)
                                
                                Text("privacy_policy".localized)
                                    .font(.system(size: 18, weight: .semibold))
                                
                                Spacer()
                                
                                Image(systemName: "chevron.right")
                                    .font(.system(size: 14, weight: .medium))
                                    .foregroundColor(.secondary)
                            }
                            
                            Text("view_privacy_policy".localized)
                                .font(.system(size: 14))
                                .foregroundColor(.secondary)
                                .lineLimit(nil)
                        }
                        .padding(20)
                        .background(
                            RoundedRectangle(cornerRadius: 16)
                                .fill(Color(.systemBackground))
                                .shadow(color: .black.opacity(0.05), radius: 8, x: 0, y: 2)
                        )
                    }
                    .buttonStyle(PlainButtonStyle())
                    .padding(.horizontal, 20)
                    
                    // 关于卡片
                    VStack(alignment: .leading, spacing: 16) {
                        HStack {
                            Image(systemName: "heart.fill")
                                .font(.system(size: 20, weight: .medium))
                                .foregroundColor(.red)
                                .frame(width: 32)
                            
                            Text("about".localized)
                                .font(.system(size: 18, weight: .semibold))
                            
                            Spacer()
                        }
                        
                        Text("app_description_long".localized)
                            .font(.system(size: 14))
                            .foregroundColor(.secondary)
                            .lineLimit(nil)
                    }
                    .padding(20)
                    .background(
                        RoundedRectangle(cornerRadius: 16)
                            .fill(Color(.systemBackground))
                            .shadow(color: .black.opacity(0.05), radius: 8, x: 0, y: 2)
                    )
                    .padding(.horizontal, 20)
                    
                    Spacer(minLength: 30)
                }
            }
        }
        .navigationTitle("settings".localized)
        .navigationBarTitleDisplayMode(.large)
    }
}

#Preview {
    SettingsView()
        .environmentObject(LanguageManager.shared)
} 
