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

import Foundation
import SwiftUI
import Combine

class AppState: ObservableObject {
    // 应用状态
    @Published var isFirstLaunch: Bool
    @Published var selectedTab: Tab = .home
    @Published var showingOnboarding: Bool = false
    
    // 用户设置
    @AppStorage("enableNotifications") var enableNotifications: Bool = true
    @AppStorage("autoCheckDraw") var autoCheckDraw: Bool = true
    @AppStorage("currencySymbol") var currencySymbol: String = "¥"
    @AppStorage("themeColor") var themeColor: String = "blue"
    
    init() {
        // 检查是否是第一次启动
        let hasLaunchedBefore = UserDefaults.standard.bool(forKey: "hasLaunchedBefore")
        let firstLaunch = !hasLaunchedBefore
        
        // 初始化存储属性
        self.isFirstLaunch = firstLaunch
        
        // 设置引导页显示状态
        if firstLaunch {
            UserDefaults.standard.set(true, forKey: "hasLaunchedBefore")
            self.showingOnboarding = true
        } else {
            self.showingOnboarding = false
        }
        
        print("🚀 AppState 初始化完成")
    }
    
    // 重置应用状态
    func resetApp() {
        if let bundleID = Bundle.main.bundleIdentifier {
            UserDefaults.standard.removePersistentDomain(forName: bundleID)
        }
        
        isFirstLaunch = true
        showingOnboarding = true
        selectedTab = .home
        
        print("🔄 应用状态已重置")
    }
}

// Tab枚举
enum Tab: Int {
    case home = 0
    case statistics = 1
    case settings = 2
    case my = 3
    
    var title: String {
        switch self {
        case .home: return "首页"
        case .statistics: return "统计"
        case .settings: return "设置"
        case .my: return "我的"
        }
    }
}
