import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'detail_page.dart';

class CustomInfoCard extends StatefulWidget {
  final String imagePath, title, subtitle, texto, categoria;
  final bool isFavorite;

  const CustomInfoCard({
    super.key,
    required this.imagePath,
    required this.title,
    required this.subtitle,
    required this.texto,
    required this.categoria,
    this.isFavorite = false,
  });

  @override
  State<CustomInfoCard> createState() => _CustomInfoCardState();
}

class _CustomInfoCardState extends State<CustomInfoCard> {
  late bool _isFavorite;

  @override
  void initState() {
    super.initState();
    _isFavorite = widget.isFavorite;
    if (!widget.isFavorite) _loadFavorite();
  }

  Future<void> _loadFavorite() async {
    final prefs = await SharedPreferences.getInstance();
    if (mounted) setState(() => _isFavorite = prefs.getBool('fav_${widget.title}') ?? false);
  }

  Future<void> _toggleFavorite() async {
    setState(() => _isFavorite = !_isFavorite);
    (await SharedPreferences.getInstance()).setBool('fav_${widget.title}', _isFavorite);
  }

  @override
  Widget build(BuildContext context) => GestureDetector(
    onTap: () => Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) => DetailPage(
          title: widget.title,
          texto: widget.texto,
          categoria: widget.categoria,
          imagePath: widget.imagePath,
        ),
      ),
    ),
    child: SizedBox(
      width: double.infinity,
      height: 130,
      child: Stack(children: [
        Card(
          margin: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(30)),
          color: const Color.fromARGB(255, 254, 242, 221),
          child: Padding(
            padding: const EdgeInsets.only(right: 8),
            child: Row(children: [
              Container(
                width: 90,
                height: 90,
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(36),
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(36),
                  child: Image.asset(
                    widget.imagePath,
                    fit: BoxFit.cover,
                    width: 90,
                    height: 90,
                  ),
                ),
              ),
              const SizedBox(width: 16),
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      widget.title,
                      style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 4),
                    Text(
                      widget.subtitle,
                      style: TextStyle(fontSize: 14, color: Colors.grey[600]),
                      maxLines: 2,
                      overflow: TextOverflow.ellipsis,
                    ),
                    const SizedBox(height: 4),
                    Text(
                      widget.categoria,
                      style: TextStyle(
                        fontSize: 12,
                        color: _getCategoryColor(widget.categoria),
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                  ],
                ),
              ),
            ]),
          ),
        ),
        Positioned(
          top: 12,
          right: 28,
          child: IconButton(
            icon: Icon(
              _isFavorite ? Icons.favorite : Icons.favorite_border,
              color: _isFavorite ? Colors.red : Colors.grey[400],
            ),
            onPressed: _toggleFavorite,
          ),
        ),
      ]),
    ),
  );

  Color _getCategoryColor(String categoria) {
    switch (categoria) {
      case 'Actividades': return Colors.blue;
      case 'Noticias UPR': return Colors.green;
      case 'Fechas Importantes': return Colors.purple;
      default: return Colors.grey;
    }
  }
}