import 'package:flutter/material.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../services/offline_package_manager.dart'; /// Sovereign Onboarding & Initial Tactical Data Provisioning Dialog class TacticalInitialProvisioningDialog extends StatefulWidget { final VoidCallback onCompleted; const TacticalInitialProvisioningDialog({ super.key, required this.onCompleted, }); /// Check if the initial tactical package provisioning is needed on first launch static Future isProvisioningNeeded() async { final prefs = await SharedPreferences.getInstance(); final isInitialized = prefs.getBool('tactical_initial_provisioned_v2') ?? false; return !isInitialized; } @override State createState() => _TacticalInitialProvisioningDialogState(); } class _TacticalInitialProvisioningDialogState extends State { bool _isDownloading = false; double _progress = 0.0; String _statusMessage = 'جاهز لبدء تجهيز البيئة الميدانية السيادية'; final List _logs = [ '• فحص مفاتيح التشفير والبيئة الميدانية المغلقة (Air-Gapped Environment)', '• تحديد نطاق المملكة الأردنية الهاشمية (Jordan Sovereign Bounding Box)', ]; @override void initState() { super.initState(); // Auto-start download on initial launch WidgetsBinding.instance.addPostFrameCallback((_) { _startInitialProvisioning(); }); } Future _startInitialProvisioning() async { setState(() { _isDownloading = true; _progress = 0.15; _statusMessage = 'جاري الاتصال بخادم الخرائط السيادي (Martin & PostGIS)...'; _logs.add('• الاتصال بخادم الخرائط Vector Tiles & DEM Server'); }); await Future.delayed(const Duration(milliseconds: 500)); setState(() { _progress = 0.35; _statusMessage = 'جاري تحميل معالم PostGIS الاستراتيجية ونقاط السيطرة...'; _logs.add('• مزامنة معالم الأردن العسكرية والمآذن والصوامع وأبراج الرادار'); }); final success = await OfflinePackageManager.downloadFullPackage(force: true); if (mounted) { if (success) { setState(() { _progress = 0.70; _statusMessage = 'جاري فهرسة بلاطات الارتفاعات الفضائية DEM وخطوط الكنتور...'; _logs.add('• تحميل شبكة مناسيب التضاريس (Terrarium Satellite DEM)'); _logs.add('• تهيئة خوارزميات التوجيه الطوبولوجي بدون إنترنت (On-Device Routing)'); }); await Future.delayed(const Duration(milliseconds: 600)); final prefs = await SharedPreferences.getInstance(); await prefs.setBool('tactical_initial_provisioned_v2', true); setState(() { _progress = 1.0; _statusMessage = 'اكتملت تهيئة المنظومة! جاهز للعمل بوضع الطيران 100%'; _logs.add('✅ تم تثبيت البيئة السيادية بنجاح • جاهز للعمليات الميدانية'); }); await Future.delayed(const Duration(seconds: 1)); if (mounted) { widget.onCompleted(); } } else { setState(() { _isDownloading = false; _statusMessage = 'تم تفعيل الحزمة المدمجة المسبقة للطوارئ'; _logs.add('⚠️ تعذر الاتصال المباشر • تم تشغيل قاعدة البيانات المدمجة المسبقة'); }); } } } @override Widget build(BuildContext context) { return PopScope( canPop: !_isDownloading, child: Dialog( backgroundColor: Colors.transparent, insetPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24), child: Container( width: 480, padding: const EdgeInsets.all(22), decoration: BoxDecoration( color: const Color(0xFF090E17), borderRadius: BorderRadius.circular(20), border: Border.all(color: const Color(0xFF0071E3).withAlpha(150), width: 1.5), boxShadow: const [ BoxShadow( color: Color(0x99000000), blurRadius: 36, spreadRadius: 8, ), BoxShadow( color: Color(0x330071E3), blurRadius: 20, ), ], ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // ── Header ─────────────────────────────────────────── Row( children: [ Container( padding: const EdgeInsets.all(10), decoration: BoxDecoration( color: const Color(0xFF0071E3).withAlpha(40), borderRadius: BorderRadius.circular(12), border: Border.all(color: const Color(0xFF0071E3)), ), child: const Icon(Icons.shield, color: Color(0xFF38BDF8), size: 28), ), const SizedBox(width: 14), const Expanded( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text( 'تهيئة المنظومة التكتيكية السيادية', style: TextStyle( color: Colors.white, fontSize: 16, fontWeight: FontWeight.w900, ), ), SizedBox(height: 2), Text( 'تحميل بيانات الخرائط والتضاريس للعمل بدون إنترنت (Off-Grid)', style: TextStyle( color: Color(0xFF94A3B8), fontSize: 11, ), ), ], ), ), ], ), const SizedBox(height: 20), // ── Progress Bar ───────────────────────────────────── ClipRRect( borderRadius: BorderRadius.circular(8), child: LinearProgressIndicator( value: _progress, minHeight: 8, backgroundColor: const Color(0xFF1E293B), valueColor: const AlwaysStoppedAnimation(Color(0xFF00F0FF)), ), ), const SizedBox(height: 12), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Expanded( child: Text( _statusMessage, style: const TextStyle( color: Color(0xFF38BDF8), fontSize: 11.5, fontWeight: FontWeight.bold, ), overflow: TextOverflow.ellipsis, ), ), Text( '${(_progress * 100).toInt()}%', style: const TextStyle( color: Color(0xFF00F0FF), fontSize: 13, fontWeight: FontWeight.w900, ), ), ], ), const SizedBox(height: 16), // ── Terminal Log Window ────────────────────────────── Container( height: 140, padding: const EdgeInsets.all(12), decoration: BoxDecoration( color: const Color(0xFF020617), borderRadius: BorderRadius.circular(10), border: Border.all(color: Colors.white12), ), child: ListView.builder( itemCount: _logs.length, itemBuilder: (ctx, i) => Padding( padding: const EdgeInsets.symmetric(vertical: 2.5), child: Text( _logs[i], style: TextStyle( color: _logs[i].startsWith('✅') ? const Color(0xFF4ADE80) : (i == _logs.length - 1 ? Colors.white : const Color(0xFF64748B)), fontSize: 11, fontFamily: 'monospace', ), ), ), ), ), const SizedBox(height: 20), // ── Action Buttons ─────────────────────────────────── Row( children: [ Expanded( child: ElevatedButton.icon( style: ElevatedButton.styleFrom( backgroundColor: const Color(0xFF0071E3), foregroundColor: Colors.white, padding: const EdgeInsets.symmetric(vertical: 12), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10), ), ), icon: _isDownloading ? const SizedBox( width: 16, height: 16, child: CircularProgressIndicator(strokeWidth: 2, color: Colors.white), ) : const Icon(Icons.check_circle_outline, size: 18), label: Text(_isDownloading ? 'جاري التهيئة والتثبيت...' : 'دخول الخريطة التكتيكية'), onPressed: _isDownloading ? null : () { widget.onCompleted(); }, ), ), ], ), ], ), ), ), ); } }