import SwiftUI

// MARK: - 条形码字符提示组件
struct BarcodeCharacterHintView: View {
    @EnvironmentObject var languageManager: LanguageManager
    let barcodeType: BarcodeType
    
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("character_type".localized)
                .font(.caption)
                .foregroundColor(.secondary)
            
            LazyVGrid(columns: [
                GridItem(.adaptive(minimum: 80), spacing: 8)
            ], spacing: 6) {
                ForEach(getCharacterTypes(), id: \.self) { charType in
                    HStack(spacing: 4) {
                        Image(systemName: charType.icon)
                            .font(.caption2)
                            .foregroundColor(charType.color)
                        
                        Text(charType.name)
                            .font(.caption2)
                            .foregroundColor(charType.color)
                    }
                    .padding(.horizontal, 8)
                    .padding(.vertical, 4)
                    .background(
                        RoundedRectangle(cornerRadius: 6)
                            .fill(charType.color.opacity(0.1))
                    )
                }
            }
        }
    }
    
    private func getCharacterTypes() -> [CharacterType] {
        switch barcodeType {
        case .ean13, .ean8, .upce, .itf14:
            return [
                CharacterType(name: "numbers".localized, icon: "number", color: .blue)
            ]
            
        case .code39:
            return [
                CharacterType(name: "letters".localized, icon: "character", color: .green),
                CharacterType(name: "numbers".localized, icon: "number", color: .blue),
                CharacterType(name: "special_characters".localized, icon: "textformat", color: .orange)
            ]
            
        case .code128:
            return [
                CharacterType(name: "letters".localized, icon: "character", color: .green),
                CharacterType(name: "numbers".localized, icon: "number", color: .blue),
                CharacterType(name: "symbols".localized, icon: "textformat", color: .purple),
                CharacterType(name: "control_characters".localized, icon: "command", color: .red)
            ]
            
        case .pdf417:
            return [
                CharacterType(name: "letters".localized, icon: "character", color: .green),
                CharacterType(name: "numbers".localized, icon: "number", color: .blue),
                CharacterType(name: "symbols".localized, icon: "textformat", color: .purple),
                CharacterType(name: "all_ascii".localized, icon: "globe", color: .indigo)
            ]
        }
    }
}

// MARK: - 字符类型结构
struct CharacterType: Hashable {
    let name: String
    let icon: String
    let color: Color
    
    // 实现 Hashable 协议
    func hash(into hasher: inout Hasher) {
        hasher.combine(name)
        hasher.combine(icon)
    }
    
    static func == (lhs: CharacterType, rhs: CharacterType) -> Bool {
        return lhs.name == rhs.name && lhs.icon == rhs.icon
    }
}

#Preview {
    VStack(spacing: 20) {
        BarcodeCharacterHintView(barcodeType: .ean13)
        BarcodeCharacterHintView(barcodeType: .code39)
        BarcodeCharacterHintView(barcodeType: .code128)
        BarcodeCharacterHintView(barcodeType: .pdf417)
    }
    .padding()
    .environmentObject(LanguageManager.shared)
} 