import 'package:didvan/config/design_config.dart'; import 'package:didvan/config/theme_data.dart'; import 'package:didvan/models/didvan_plus_model.dart'; import 'package:didvan/services/network/request_helper.dart'; import 'package:didvan/services/network/request.dart'; import 'package:didvan/views/widgets/didvan/text.dart'; import 'package:didvan/views/widgets/video/primary_controls.dart'; import 'package:flutter/material.dart'; import 'package:flutter_svg/svg.dart'; import 'package:persian_number_utility/persian_number_utility.dart'; import 'package:video_player/video_player.dart'; import 'package:chewie/chewie.dart'; class DidvanPlusSection extends StatefulWidget { final DidvanPlusModel didvanPlus; const DidvanPlusSection({ super.key, required this.didvanPlus, }); @override State createState() => _DidvanPlusSectionState(); } class _DidvanPlusSectionState extends State { late VideoPlayerController _videoController; ChewieController? _chewieController; bool _isInitialized = false; @override void initState() { super.initState(); _initializeVideo(); } void _initializeVideo() { final videoUrl = '${RequestHelper.baseUrl}${widget.didvanPlus.file}'; final fullUrl = '$videoUrl?accessToken=${RequestService.token}'; debugPrint('🎥 Didvan Plus Video URL: $fullUrl'); debugPrint('🎥 Video file path: ${widget.didvanPlus.file}'); debugPrint('🎥 Base URL: ${RequestHelper.baseUrl}'); debugPrint( '🎥 Token exists: ${RequestService.token != null && RequestService.token!.isNotEmpty}'); _videoController = VideoPlayerController.networkUrl( Uri.parse(fullUrl), httpHeaders: { 'Authorization': 'Bearer ${RequestService.token}', }, )..initialize().then((_) { debugPrint('✅ Video initialized successfully'); setState(() { _chewieController = ChewieController( videoPlayerController: _videoController, autoPlay: false, looping: false, allowFullScreen: true, allowMuting: true, showControls: true, customControls: const PrimaryControls(), materialProgressColors: ChewieProgressColors( playedColor: DesignConfig.isDark ? const Color.fromARGB(255, 0, 125, 166) : const Color.fromARGB(255, 0, 89, 119), handleColor: DesignConfig.isDark ? const Color.fromARGB(255, 0, 125, 166) : const Color.fromARGB(255, 0, 89, 119), backgroundColor: Colors.grey, bufferedColor: Colors.grey.shade300, ), placeholder: Container( color: Colors.black, child: Center( child: Image.network( '${RequestHelper.baseUrl}${widget.didvanPlus.image}', fit: BoxFit.cover, ), ), ), ); _isInitialized = true; }); }).catchError((error) { debugPrint('❌ Video initialization error: $error'); }); } @override void dispose() { _videoController.dispose(); _chewieController?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { final publishedDate = DateTime.parse(widget.didvanPlus.publishedAt); return Padding( padding: const EdgeInsets.symmetric(horizontal: 16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ Row( children: [ SvgPicture.asset( 'lib/assets/icons/DidvanPlus.svg', width: 25, height: 25, ), const SizedBox(width: 5), DidvanText( 'دیدوان پلاس', style: Theme.of(context).textTheme.titleMedium, color: DesignConfig.isDark ? const Color.fromARGB(255, 0, 125, 166) : const Color.fromARGB(255, 0, 89, 119), fontSize: 13, ), ], ), DidvanText( publishedDate.toPersianDate(), fontSize: 13, color: DesignConfig.isDark ? const Color.fromARGB(255, 0, 125, 166) : const Color.fromARGB(255, 0, 89, 119), ), ], ), const SizedBox(height: 12), ClipRRect( borderRadius: BorderRadius.circular(12), child: AspectRatio( aspectRatio: 16 / 9, child: _isInitialized && _chewieController != null ? Chewie(controller: _chewieController!) : Container( color: Colors.black, child: Center( child: Image.network( '${RequestHelper.baseUrl}${widget.didvanPlus.image}', fit: BoxFit.cover, loadingBuilder: (context, child, loadingProgress) { if (loadingProgress == null) return child; return const CircularProgressIndicator(); }, ), ), ), ), ), ], ), ); } }