//
//  ContentView.swift
//  MyQrCode
//
//  Created by dev on 2025/8/19.
//

import SwiftUI

struct ContentView: View {
    @EnvironmentObject var coreDataManager: CoreDataManager
    @EnvironmentObject var languageManager: LanguageManager
    
    var body: some View {
        NavigationView {
            ZStack {
                // 背景渐变
                LinearGradient(
                    gradient: Gradient(colors: [
                        Color(.systemBackground),
                        Color(.systemGray6).opacity(0.3)
                    ]),
                    startPoint: .top,
                    endPoint: .bottom
                )
                .ignoresSafeArea()
                
                VStack(spacing: 24) {
                    // 页面标题
                    VStack(spacing: 12) {
                        Text("qr_code_creator".localized)
                            .font(.system(size: 34, weight: .bold, design: .rounded))
                            .foregroundColor(.primary)
                            .id(languageManager.refreshTrigger)
                        
                        Text("quick_create_scan".localized)
                            .id(languageManager.refreshTrigger)
                            .font(.system(size: 16, weight: .medium))
                            .foregroundColor(.secondary)
                            .multilineTextAlignment(.center)
                    }
                    .padding(.top, 0)
                        
                        // 功能卡片布局
                        VStack(spacing: 18) {
                                                // 主功能 - 创建二维码（大卡片）
                    NavigationLink(destination: CodeTypeSelectionView()) {
                                VStack(alignment: .leading, spacing: 20) {
                                    HStack {
                                        Image(systemName: "qrcode")
                                            .font(.system(size: 32, weight: .bold))
                                            .foregroundColor(.white)
                                        
                                        Spacer()
                                        
                                        Image(systemName: "arrow.up.right")
                                            .font(.system(size: 22, weight: .medium))
                                            .foregroundColor(.white.opacity(0.8))
                                    }
                                    
                                    VStack(alignment: .leading, spacing: 12) {
                                        Text("create_qr_code".localized)
                                            .font(.system(size: 28, weight: .bold))
                                            .foregroundColor(.white)
                                            .id(languageManager.refreshTrigger)
                                        
                                        Text("generate_various_codes".localized)
                                            .id(languageManager.refreshTrigger)
                                            .font(.system(size: 16))
                                            .foregroundColor(.white.opacity(0.9))
                                            .lineLimit(2)
                                    }
                                }
                                .padding(24)
                                .frame(maxWidth: .infinity, minHeight: 160)
                                .background(
                                    LinearGradient(
                                        gradient: Gradient(colors: [
                                            Color.purple,
                                            Color.blue,
                                            Color.purple.opacity(0.8)
                                        ]),
                                        startPoint: .topLeading,
                                        endPoint: .bottomTrailing
                                    )
                                )
                                .cornerRadius(24)
                                .shadow(color: .purple.opacity(0.4), radius: 20, x: 0, y: 10)
                            }
                            .padding(.horizontal, 20)
                            
                            // 第二行 - 两个辅助功能卡片
                            HStack(spacing: 14) {
                                // 扫描功能卡片
                                NavigationLink(destination: ScannerView()) {
                                    VStack(alignment: .leading, spacing: 12) {
                                        HStack {
                                            Image(systemName: "camera.fill")
                                                .font(.system(size: 22, weight: .medium))
                                                .foregroundColor(.white)
                                            
                                            Spacer()
                                        }
                                        
                                        VStack(alignment: .leading, spacing: 6) {
                                            Text("scan_recognize".localized)
                                                .font(.system(size: 16, weight: .bold))
                                                .foregroundColor(.white)
                                                .id(languageManager.refreshTrigger)
                                            
                                            Text("scan_qr_code".localized)
                                                .id(languageManager.refreshTrigger)
                                                .font(.system(size: 13))
                                                .foregroundColor(.white.opacity(0.9))
                                        }
                                    }
                                    .padding(16)
                                    .frame(maxWidth: .infinity, minHeight: 100)
                                    .background(
                                        LinearGradient(
                                            gradient: Gradient(colors: [
                                                Color.blue,
                                                Color.blue.opacity(0.8)
                                            ]),
                                            startPoint: .topLeading,
                                            endPoint: .bottomTrailing
                                        )
                                    )
                                    .cornerRadius(16)
                                    .shadow(color: .blue.opacity(0.25), radius: 8, x: 0, y: 4)
                                }
                                
                                // 历史记录卡片
                                NavigationLink(destination: HistoryView()) {
                                    VStack(alignment: .leading, spacing: 12) {
                                        HStack {
                                            Image(systemName: "clock.arrow.circlepath")
                                                .font(.system(size: 22, weight: .medium))
                                                .foregroundColor(.white)
                                            
                                            Spacer()
                                        }
                                        
                                        VStack(alignment: .leading, spacing: 6) {
                                            Text("history_records".localized)
                                                .font(.system(size: 16, weight: .bold))
                                                .foregroundColor(.white)
                                                .id(languageManager.refreshTrigger)
                                            
                                            Text("view_history".localized)
                                                .id(languageManager.refreshTrigger)
                                                .font(.system(size: 13))
                                                .foregroundColor(.white.opacity(0.9))
                                        }
                                    }
                                    .padding(16)
                                    .frame(maxWidth: .infinity, minHeight: 100)
                                    .background(
                                        LinearGradient(
                                            gradient: Gradient(colors: [
                                                Color.orange,
                                                Color.orange.opacity(0.8)
                                            ]),
                                            startPoint: .topLeading,
                                            endPoint: .bottomTrailing
                                        )
                                    )
                                    .cornerRadius(16)
                                    .shadow(color: .orange.opacity(0.25), radius: 8, x: 0, y: 4)
                                }
                            }
                            .padding(.horizontal, 20)
                    }
                    
                    Spacer(minLength: 0)
                }
                .padding(.top, 0)
            }
            .toolbar {
                ToolbarItem(placement: .navigationBarTrailing) {
                    NavigationLink(destination: SettingsView()) {
                        Image(systemName: "gearshape.fill")
                            .font(.system(size: 18, weight: .medium))
                            .foregroundColor(.primary)
                    }
                }
            }
        }
    }
}

#if DEBUG
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
            .environmentObject(CoreDataManager.shared)
            .environmentObject(LanguageManager.shared)
    }
}
#endif
