import 'package:flutter/material.dart'; import '../models/movie.dart'; import '../services/database_service.dart'; class MovieDetailPage extends StatefulWidget { final Movie movie; const MovieDetailPage({super.key, required this.movie}); @override State createState() => _MovieDetailPageState(); } class _MovieDetailPageState extends State { bool isFavorite = false; bool isLoading = false; @override void initState() { super.initState(); _checkFavoriteStatus(); } Future _checkFavoriteStatus() async { final favorite = await DatabaseService.isFavorite(widget.movie.id); setState(() { isFavorite = favorite; }); } Future _toggleFavorite() async { setState(() { isLoading = true; }); try { if (isFavorite) { final success = await DatabaseService.removeFromFavorites(widget.movie.id); if (success) { setState(() { isFavorite = false; }); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${widget.movie.title} supprimé des favoris'), backgroundColor: Colors.red, ), ); } } else { final success = await DatabaseService.addToFavorites(widget.movie); if (success) { setState(() { isFavorite = true; }); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('${widget.movie.title} ajouté aux favoris'), backgroundColor: Colors.green, ), ); } } } catch (e) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text('Erreur: $e'), backgroundColor: Colors.red, ), ); } finally { setState(() { isLoading = false; }); } } @override Widget build(BuildContext context) { return Scaffold( backgroundColor: Colors.black, appBar: AppBar( backgroundColor: Colors.purple, centerTitle: true, title: Text( widget.movie.title, style: const TextStyle( fontSize: 20, fontWeight: FontWeight.bold, color: Colors.white, ), ), leading: IconButton( icon: const Icon(Icons.arrow_back, color: Colors.white), onPressed: () => Navigator.pop(context), ), actions: [ IconButton( icon: isLoading ? const SizedBox( width: 20, height: 20, child: CircularProgressIndicator( color: Colors.white, strokeWidth: 2, ), ) : Icon( isFavorite ? Icons.favorite : Icons.favorite_border, color: isFavorite ? Colors.red : Colors.white, ), onPressed: isLoading ? null : _toggleFavorite, ), ], ), body: SingleChildScrollView( child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( width: double.infinity, height: 300, child: widget.movie.imageUrl.isNotEmpty ? Image.network( widget.movie.imageUrl, fit: BoxFit.cover, errorBuilder: (context, error, stackTrace) { return Container( color: Colors.grey[800], child: const Center( child: Icon( Icons.movie, color: Colors.white, size: 100, ), ), ); }, ) : Container( color: Colors.grey[800], child: const Center( child: Icon( Icons.movie, color: Colors.white, size: 100, ), ), ), ), Padding( padding: const EdgeInsets.all(20), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Row( children: [ Expanded( child: Text( widget.movie.title, style: const TextStyle( color: Colors.white, fontSize: 28, fontWeight: FontWeight.bold, ), ), ), Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), decoration: BoxDecoration( color: Colors.amber, borderRadius: BorderRadius.circular(20), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ const Icon(Icons.star, color: Colors.white, size: 16), const SizedBox(width: 4), Text( widget.movie.rating.toStringAsFixed(1), style: const TextStyle( color: Colors.white, fontSize: 16, fontWeight: FontWeight.bold, ), ), ], ), ), ], ), const SizedBox(height: 16), Row( children: [ Container( padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6), decoration: BoxDecoration( color: Colors.purple, borderRadius: BorderRadius.circular(20), ), child: Text( widget.movie.category, style: const TextStyle( color: Colors.white, fontSize: 14, fontWeight: FontWeight.bold, ), ), ), const SizedBox(width: 12), Text( widget.movie.releaseDate, style: TextStyle( color: Colors.grey[400], fontSize: 16, ), ), ], ), const SizedBox(height: 24), const Text( 'Description', style: TextStyle( color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 12), Text( widget.movie.description, style: TextStyle( color: Colors.grey[300], fontSize: 16, height: 1.5, ), ), const SizedBox(height: 32), SizedBox( width: double.infinity, height: 50, child: ElevatedButton( onPressed: () { ScaffoldMessenger.of(context).showSnackBar( const SnackBar( content: Text('Lecture du film...'), backgroundColor: Colors.purple, ), ); }, style: ElevatedButton.styleFrom( backgroundColor: Colors.red, foregroundColor: Colors.white, shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(25), ), ), child: const Row( mainAxisAlignment: MainAxisAlignment.center, children: [ Icon(Icons.play_arrow, size: 24), SizedBox(width: 8), Text( 'Regarder maintenant', style: TextStyle( fontSize: 18, fontWeight: FontWeight.bold, ), ), ], ), ), ), const SizedBox(height: 20), Container( padding: const EdgeInsets.all(16), decoration: BoxDecoration( color: Colors.grey[900], borderRadius: BorderRadius.circular(12), ), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ const Text( 'Informations', style: TextStyle( color: Colors.white, fontSize: 18, fontWeight: FontWeight.bold, ), ), const SizedBox(height: 12), _buildInfoRow('ID', widget.movie.id.toString()), _buildInfoRow('Titre original', widget.movie.originalTitle.isNotEmpty ? widget.movie.originalTitle : widget.movie.title), _buildInfoRow('Note', '${widget.movie.rating.toStringAsFixed(1)}/10'), _buildInfoRow('Catégorie', widget.movie.category), _buildInfoRow('Date de sortie', widget.movie.releaseDate), ], ), ), ], ), ), ], ), ), ); } Widget _buildInfoRow(String label, String value) { return Padding( padding: const EdgeInsets.symmetric(vertical: 4), child: Row( crossAxisAlignment: CrossAxisAlignment.start, children: [ SizedBox( width: 100, child: Text( '$label:', style: TextStyle( color: Colors.grey[400], fontSize: 14, ), ), ), Expanded( child: Text( value, style: const TextStyle( color: Colors.white, fontSize: 14, ), ), ), ], ), ); } }