//
//  OnboardingView.swift
//  LotteryTracker
//
//  Created by aaa on 2026/1/22.
//

import SwiftUI

struct OnboardingView: View {
    @Environment(\.dismiss) var dismiss
    @AppStorage("hasCompletedOnboarding") var hasCompletedOnboarding: Bool = false
    
    @State private var currentPage = 0
    
    let pages = [
        OnboardingPage(
            title: "欢迎使用彩票记录",
            description: "轻松记录您的彩票购买，追踪中奖情况",
            image: "ticket.fill",
            color: .blue
        ),
        OnboardingPage(
            title: "智能统计",
            description: "详细的数据分析和图表展示，了解您的投注习惯",
            image: "chart.bar.fill",
            color: .green
        ),
        OnboardingPage(
            title: "开奖提醒",
            description: "及时获取开奖通知，不错过任何中奖机会",
            image: "bell.badge.fill",
            color: .orange
        ),
        OnboardingPage(
            title: "安全本地存储",
            description: "所有数据仅保存在您的设备中，保护您的隐私",
            image: "lock.shield.fill",
            color: .purple
        )
    ]
    
    var body: some View {
        ZStack {
            // 背景渐变
            LinearGradient(
                colors: [Color.blue.opacity(0.1), Color.purple.opacity(0.1)],
                startPoint: .topLeading,
                endPoint: .bottomTrailing
            )
            .ignoresSafeArea()
            
            VStack {
                // 跳过按钮
                HStack {
                    Spacer()
                    Button("跳过") {
                        completeOnboarding()
                    }
                    .font(.subheadline)
                    .foregroundColor(.secondary)
                    .padding(.trailing)
                }
                
                // 页面内容
                TabView(selection: $currentPage) {
                    ForEach(0..<pages.count, id: \.self) { index in
                        OnboardingPageView(page: pages[index])
                            .tag(index)
                    }
                }
                .tabViewStyle(.page(indexDisplayMode: .always))
                .indexViewStyle(.page(backgroundDisplayMode: .always))
                
                // 导航按钮
                HStack {
                    if currentPage > 0 {
                        Button("上一步") {
                            withAnimation {
                                currentPage -= 1
                            }
                        }
                        .buttonStyle(.bordered)
                    }
                    
                    Spacer()
                    
                    if currentPage < pages.count - 1 {
                        Button("下一步") {
                            withAnimation {
                                currentPage += 1
                            }
                        }
                        .buttonStyle(.borderedProminent)
                    } else {
                        Button("开始使用") {
                            completeOnboarding()
                        }
                        .buttonStyle(.borderedProminent)
                        .font(.headline)
                    }
                }
                .padding(.horizontal, 40)
                .padding(.bottom, 40)
            }
        }
    }
    
    private func completeOnboarding() {
        hasCompletedOnboarding = true
        dismiss()
    }
}

// 引导页面模型
struct OnboardingPage {
    let title: String
    let description: String
    let image: String
    let color: Color
}

// 引导页面视图
struct OnboardingPageView: View {
    let page: OnboardingPage
    
    var body: some View {
        VStack(spacing: 30) {
            Spacer()
            
            // 图标
            Image(systemName: page.image)
                .font(.system(size: 80))
                .foregroundColor(page.color)
                .padding()
                .background(
                    Circle()
                        .fill(page.color.opacity(0.1))
                        .frame(width: 160, height: 160)
                )
            
            // 标题
            Text(page.title)
                .font(.largeTitle)
                .fontWeight(.bold)
                .multilineTextAlignment(.center)
                .padding(.horizontal, 40)
            
            // 描述
            Text(page.description)
                .font(.body)
                .foregroundColor(.secondary)
                .multilineTextAlignment(.center)
                .padding(.horizontal, 40)
            
            Spacer()
            Spacer()
        }
    }
}

// 预览
struct OnboardingView_Previews: PreviewProvider {
    static var previews: some View {
        OnboardingView()
    }
}
