|
| 1 | +package com.margelo.nitro.plugpagnitro |
| 2 | + |
| 3 | +import br.com.uol.pagseguro.plugpagservice.wrapper.PlugPagEventData |
| 4 | +import android.util.Log |
| 5 | + |
| 6 | +/** |
| 7 | + * Configuration-driven event handler for payment events |
| 8 | + * Reduces complex switch statements to simple configuration |
| 9 | + */ |
| 10 | +object PaymentEventHandler { |
| 11 | + |
| 12 | + private const val TAG = "PaymentEventHandler" |
| 13 | + |
| 14 | + // Event configuration mapping |
| 15 | + private val eventConfig = mapOf( |
| 16 | + // Password events |
| 17 | + PlugPagEventData.EVENT_CODE_DIGIT_PASSWORD to EventConfig( |
| 18 | + baseCode = 1010.0, |
| 19 | + messageProvider = { data, context -> |
| 20 | + val count = context["passwordCount"] as? Int ?: 0 |
| 21 | + "Senha: ${"*".repeat(minOf(count, 6))}" |
| 22 | + } |
| 23 | + ), |
| 24 | + PlugPagEventData.EVENT_CODE_NO_PASSWORD to EventConfig( |
| 25 | + baseCode = 1011.0, |
| 26 | + message = "Digite sua senha" |
| 27 | + ) |
| 28 | + ) |
| 29 | + |
| 30 | + // Pattern-based event mapping for dynamic messages |
| 31 | + private val patternConfig = listOf( |
| 32 | + PatternConfig( |
| 33 | + patterns = listOf("cartão", "card"), |
| 34 | + subPatterns = mapOf( |
| 35 | + listOf("inserir", "insert") to EventConfig(1004.0, "Aguardando cartão..."), |
| 36 | + listOf("remov", "retire") to EventConfig(1030.0, "Retire o cartão") |
| 37 | + ), |
| 38 | + defaultConfig = EventConfig(1001.0, "Cartão detectado") |
| 39 | + ), |
| 40 | + PatternConfig( |
| 41 | + patterns = listOf("processa", "process"), |
| 42 | + defaultConfig = EventConfig(1020.0, "Processando transação...") |
| 43 | + ), |
| 44 | + PatternConfig( |
| 45 | + patterns = listOf("conecta", "connect"), |
| 46 | + defaultConfig = EventConfig(1021.0, "Conectando à rede...") |
| 47 | + ), |
| 48 | + PatternConfig( |
| 49 | + patterns = listOf("envian", "send"), |
| 50 | + defaultConfig = EventConfig(1022.0, "Enviando dados...") |
| 51 | + ), |
| 52 | + PatternConfig( |
| 53 | + patterns = listOf("aguard", "wait"), |
| 54 | + defaultConfig = EventConfig(1023.0, "Aguardando resposta...") |
| 55 | + ), |
| 56 | + PatternConfig( |
| 57 | + patterns = listOf("aprovad", "approved"), |
| 58 | + defaultConfig = EventConfig(1031.0, "Transação aprovada") |
| 59 | + ), |
| 60 | + PatternConfig( |
| 61 | + patterns = listOf("negad", "denied", "recusad"), |
| 62 | + defaultConfig = EventConfig(1032.0, "Transação negada") |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + /** |
| 67 | + * Handles payment events using configuration-driven approach |
| 68 | + * @param eventData The event data from PlugPag SDK |
| 69 | + * @param context Additional context (like password count) |
| 70 | + */ |
| 71 | + fun handleEvent(eventData: PlugPagEventData, context: MutableMap<String, Any> = mutableMapOf()) { |
| 72 | + val eventCode = eventData.eventCode |
| 73 | + val message = eventData.customMessage ?: "" |
| 74 | + |
| 75 | + // Handle specific event codes first |
| 76 | + eventConfig[eventCode]?.let { config -> |
| 77 | + val finalMessage = config.messageProvider?.invoke(eventData, context) |
| 78 | + ?: config.message |
| 79 | + ?: message |
| 80 | + emitEvent(config.baseCode, finalMessage) |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // Handle pattern-based events |
| 85 | + if (message.isNotEmpty()) { |
| 86 | + patternConfig.forEach { patternConfig -> |
| 87 | + if (patternConfig.patterns.any { pattern -> |
| 88 | + message.contains(pattern, ignoreCase = true) |
| 89 | + }) { |
| 90 | + // Check sub-patterns first |
| 91 | + patternConfig.subPatterns?.forEach { (subPatterns, config) -> |
| 92 | + if (subPatterns.any { subPattern -> |
| 93 | + message.contains(subPattern, ignoreCase = true) |
| 94 | + }) { |
| 95 | + emitEvent(config.baseCode, config.message ?: message) |
| 96 | + return |
| 97 | + } |
| 98 | + } |
| 99 | + // Use default pattern config |
| 100 | + emitEvent( |
| 101 | + patternConfig.defaultConfig.baseCode, |
| 102 | + patternConfig.defaultConfig.message ?: message |
| 103 | + ) |
| 104 | + return |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Default case for unknown patterns |
| 109 | + emitEvent(1020.0, message) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private fun emitEvent(code: Double, message: String) { |
| 114 | + Log.d(TAG, "Emitting event - Code: $code, Message: $message") |
| 115 | + PlugpagEventEmitter.emitPaymentEvent(code, message) |
| 116 | + } |
| 117 | + |
| 118 | + // Configuration data classes |
| 119 | + data class EventConfig( |
| 120 | + val baseCode: Double, |
| 121 | + val message: String? = null, |
| 122 | + val messageProvider: ((PlugPagEventData, Map<String, Any>) -> String)? = null |
| 123 | + ) |
| 124 | + |
| 125 | + data class PatternConfig( |
| 126 | + val patterns: List<String>, |
| 127 | + val subPatterns: Map<List<String>, EventConfig>? = null, |
| 128 | + val defaultConfig: EventConfig |
| 129 | + ) |
| 130 | +} |
0 commit comments