import 'dart:async'; import 'package:flutter/foundation.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:webview_flutter/webview_flutter.dart'; import 'package:yumi/app/routes/sc_fluro_navigator.dart'; import 'package:yumi/modules/room_game/bridge/baishun_js_bridge.dart'; import 'package:yumi/modules/room_game/data/models/room_game_models.dart'; import 'package:yumi/modules/room_game/data/room_game_repository.dart'; import 'package:yumi/modules/room_game/views/baishun_loading_view.dart'; import 'package:yumi/modules/wallet/wallet_route.dart'; import 'package:yumi/ui_kit/components/sc_tts.dart'; class BaishunGamePage extends StatefulWidget { const BaishunGamePage({ super.key, required this.roomId, required this.game, required this.launchModel, }); final String roomId; final RoomGameListItemModel game; final BaishunLaunchModel launchModel; @override State createState() => _BaishunGamePageState(); } class _BaishunGamePageState extends State { final RoomGameRepository _repository = RoomGameRepository(); final Set> _webGestureRecognizers = >{ Factory(() => EagerGestureRecognizer()), }; late final WebViewController _controller; Timer? _bridgeBootstrapTimer; Timer? _loadingFallbackTimer; bool _isLoading = true; bool _isClosing = false; bool _didReceiveBridgeMessage = false; bool _didFinishPageLoad = false; bool _hasDeliveredLaunchConfig = false; String? _errorMessage; @override void initState() { super.initState(); _controller = WebViewController() ..setJavaScriptMode(JavaScriptMode.unrestricted) ..setBackgroundColor(Colors.black) ..addJavaScriptChannel( BaishunJsBridge.channelName, onMessageReceived: _handleBridgeMessage, ) ..addJavaScriptChannel( BaishunBridgeActions.getConfig, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.getConfig, message, ), ) ..addJavaScriptChannel( BaishunBridgeActions.destroy, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.destroy, message, ), ) ..addJavaScriptChannel( BaishunBridgeActions.gameRecharge, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.gameRecharge, message, ), ) ..addJavaScriptChannel( BaishunBridgeActions.gameLoaded, onMessageReceived: (JavaScriptMessage message) => _handleNamedChannelMessage( BaishunBridgeActions.gameLoaded, message, ), ) ..setNavigationDelegate( NavigationDelegate( onPageStarted: (String _) { _prepareForPageLoad(); }, onPageFinished: (String _) async { _didFinishPageLoad = true; await _injectBridge(reason: 'page_finished'); }, onWebResourceError: (WebResourceError error) { _stopBridgeBootstrap(); if (!mounted) { return; } setState(() { _errorMessage = error.description; _isLoading = false; }); }, ), ); unawaited(_loadGameEntry()); } @override void dispose() { _stopBridgeBootstrap(); super.dispose(); } void _prepareForPageLoad() { _didReceiveBridgeMessage = false; _didFinishPageLoad = false; _hasDeliveredLaunchConfig = false; _stopBridgeBootstrap(); _bridgeBootstrapTimer = Timer.periodic(const Duration(milliseconds: 250), ( Timer timer, ) { if (!mounted || _didReceiveBridgeMessage || _errorMessage != null) { timer.cancel(); return; } unawaited(_injectBridge(reason: 'bootstrap')); if (_didFinishPageLoad && timer.tick >= 12) { timer.cancel(); } }); _loadingFallbackTimer = Timer(const Duration(seconds: 6), () { if (!mounted || _didReceiveBridgeMessage || _errorMessage != null) { return; } setState(() { _isLoading = false; }); }); if (!mounted) { return; } setState(() { _isLoading = true; _errorMessage = null; }); } void _stopBridgeBootstrap() { _bridgeBootstrapTimer?.cancel(); _bridgeBootstrapTimer = null; _loadingFallbackTimer?.cancel(); _loadingFallbackTimer = null; } Future _loadGameEntry() async { try { _prepareForPageLoad(); final entryUrl = widget.launchModel.entry.entryUrl.trim(); if (entryUrl.isEmpty || entryUrl.startsWith('mock://')) { final html = await rootBundle.loadString( 'assets/debug/baishun_mock/index.html', ); await _controller.loadHtmlString( html, baseUrl: 'https://baishun.mock.local/', ); return; } final uri = Uri.tryParse(entryUrl); if (uri == null) { throw Exception('Invalid game entry url: $entryUrl'); } await _controller.loadRequest(uri); } catch (error) { if (!mounted) { return; } setState(() { _errorMessage = error.toString(); _isLoading = false; }); } } Future _injectBridge({String reason = 'manual'}) async { try { await _controller.runJavaScript(BaishunJsBridge.bootstrapScript()); } catch (_) {} } Future _sendConfigToGame({ String? jsCallbackPath, bool force = false, }) async { if (_hasDeliveredLaunchConfig && !force) { return; } _hasDeliveredLaunchConfig = true; final rawConfig = widget.launchModel.bridgeConfig; final config = _buildEffectiveBridgeConfig(rawConfig); try { await _controller.runJavaScript( BaishunJsBridge.buildConfigScript( config, jsCallbackPath: jsCallbackPath, ), ); } catch (_) {} } Future _handleBridgeMessage(JavaScriptMessage message) async { final bridgeMessage = BaishunBridgeMessage.parse(message.message); await _dispatchBridgeMessage(bridgeMessage); } Future _handleNamedChannelMessage( String action, JavaScriptMessage message, ) async { final bridgeMessage = BaishunBridgeMessage.fromAction( action, message.message, ); await _dispatchBridgeMessage(bridgeMessage); } Future _dispatchBridgeMessage( BaishunBridgeMessage bridgeMessage, ) async { if (bridgeMessage.action == BaishunBridgeActions.debugLog) { return; } final callbackPath = _extractCallbackPath(bridgeMessage.payload); if (bridgeMessage.action.isNotEmpty) { _didReceiveBridgeMessage = true; _stopBridgeBootstrap(); } switch (bridgeMessage.action) { case BaishunBridgeActions.getConfig: await _sendConfigToGame( jsCallbackPath: callbackPath.isEmpty ? null : callbackPath, ); break; case BaishunBridgeActions.gameLoaded: if (!mounted) { return; } setState(() { _isLoading = false; _errorMessage = null; }); break; case BaishunBridgeActions.gameRecharge: await SCNavigatorUtils.push(context, WalletRoute.recharge); await _notifyWalletUpdate(); break; case BaishunBridgeActions.destroy: await _closeAndExit(reason: 'h5_destroy'); break; default: break; } } Future _notifyWalletUpdate() async { try { await _controller.runJavaScript( BaishunJsBridge.buildWalletUpdateScript(), ); } catch (_) {} } Future _reload() async { if (!mounted) { return; } setState(() { _errorMessage = null; _isLoading = true; }); await _loadGameEntry(); } Future _closeAndExit({String reason = 'page_back'}) async { if (_isClosing) { return; } _isClosing = true; try { if (!widget.launchModel.gameSessionId.startsWith('bs_mock_')) { await _repository.closeBaishunGame( roomId: widget.roomId, gameSessionId: widget.launchModel.gameSessionId, reason: reason, ); } } catch (_) {} if (mounted) { Navigator.of(context).pop(); } } String _extractCallbackPath(Map payload) { final candidates = [ payload['jsCallback']?.toString() ?? '', payload['callback']?.toString() ?? '', ]; for (final candidate in candidates) { final trimmed = candidate.trim(); if (trimmed.isNotEmpty) { return trimmed; } } return ''; } BaishunBridgeConfigModel _buildEffectiveBridgeConfig( BaishunBridgeConfigModel rawConfig, ) { final normalizedLanguage = _mapBaishunLanguage(rawConfig.language); if (normalizedLanguage == rawConfig.language.trim()) { return rawConfig; } return BaishunBridgeConfigModel( appName: rawConfig.appName, appChannel: rawConfig.appChannel, appId: rawConfig.appId, userId: rawConfig.userId, code: rawConfig.code, roomId: rawConfig.roomId, gameMode: rawConfig.gameMode, language: normalizedLanguage, gsp: rawConfig.gsp, gameConfig: rawConfig.gameConfig, ); } String _mapBaishunLanguage(String rawLanguage) { final trimmed = rawLanguage.trim(); if (trimmed.isEmpty) { return '2'; } final lower = trimmed.toLowerCase(); const supportedCodes = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', }; if (supportedCodes.contains(lower)) { return lower; } if (lower.startsWith('zh')) { return '0'; } switch (lower) { case 'en': case 'english': return '2'; case 'id': case 'in': case 'indonesian': return '3'; case 'ms': case 'malay': return '4'; case 'th': case 'thai': return '5'; case 'vi': case 'vietnamese': return '6'; case 'ar': case 'arabic': return '7'; case 'fil': case 'tl': case 'tagalog': return '8'; case 'pt': case 'portuguese': return '9'; case 'tr': case 'turkish': return '10'; case 'ur': case 'urdu': return '11'; default: return '2'; } } @override Widget build(BuildContext context) { final topCrop = 28.w; return PopScope( canPop: false, onPopInvokedWithResult: (bool didPop, Object? result) async { if (didPop) { return; } await _closeAndExit(); }, child: Material( color: Colors.transparent, child: Align( alignment: Alignment.bottomCenter, child: ClipRRect( borderRadius: BorderRadius.only( topLeft: Radius.circular(24.w), topRight: Radius.circular(24.w), ), child: Container( width: ScreenUtil().screenWidth, height: ScreenUtil().screenHeight * 0.5, color: const Color(0xFF081915), child: Stack( children: [ Positioned( top: -topCrop, left: 0, right: 0, bottom: 0, child: WebViewWidget( controller: _controller, gestureRecognizers: _webGestureRecognizers, ), ), if (_errorMessage != null) _buildErrorState(), if (_isLoading && _errorMessage == null) const IgnorePointer( ignoring: true, child: BaishunLoadingView( message: 'Waiting for gameLoaded...', ), ), ], ), ), ), ), ), ); } Widget _buildErrorState() { return Positioned.fill( child: Container( color: Colors.black.withValues(alpha: 0.88), padding: EdgeInsets.symmetric(horizontal: 28.w), alignment: Alignment.center, child: Column( mainAxisSize: MainAxisSize.min, children: [ Icon(Icons.error_outline, color: Colors.white70, size: 34.w), SizedBox(height: 12.w), Text( 'Game page failed to load', style: TextStyle( color: Colors.white, fontSize: 16.sp, fontWeight: FontWeight.w700, ), ), SizedBox(height: 8.w), Text( _errorMessage ?? '', textAlign: TextAlign.center, style: TextStyle(color: Colors.white70, fontSize: 12.sp), ), SizedBox(height: 16.w), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ TextButton(onPressed: _reload, child: const Text('Retry')), SizedBox(width: 10.w), TextButton( onPressed: () { SCTts.show('Please check game host and package url'); }, child: const Text('Tips'), ), ], ), ], ), ), ); } }