part of 'home_page.dart';

typedef OnLikeCallback = void Function(String? id, String title, bool isLiked)?;

class _Card extends StatelessWidget {
  final String? fullName;
  final String? title;
  final String? imageUrl;
  final OnLikeCallback onLike;
  final VoidCallback? onTap;
  final bool isLiked;

  const _Card(
    this.fullName, {
    required this.title,
    this.imageUrl,
    this.onLike,
    this.onTap,
    this.isLiked = false,
  });

  factory _Card.fromData(
    CardData data, {
    OnLikeCallback onLike,
    VoidCallback? onTap,
    bool isLiked = false,
  }) =>
      _Card(
        data.fullName,
        title: data.title,
        imageUrl: data.imageUrl,
        onLike: onLike,
        onTap: onTap,
        isLiked: isLiked,
      );

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onTap,
      child: Container(
        margin: const EdgeInsets.all(16),
        constraints: const BoxConstraints(minHeight: 240),
        decoration: BoxDecoration(
          color: Colors.grey,
          borderRadius: BorderRadius.circular(20),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(.5),
              spreadRadius: 4,
              offset: const Offset(0, 5),
              blurRadius: 8,
            ),
          ],
        ),
        child: Padding(
            padding: const EdgeInsets.only(top: 17),
            child: Column(
              children: [
                ClipRRect(
                  borderRadius: const BorderRadius.only(
                    topLeft: Radius.circular(20),
                    topRight: Radius.circular(20),
                  ),
                  child: SizedBox(
                    height: 160,
                    width: double.infinity,
                    child: Stack(
                      children: [
                        Positioned.fill(
                          child: Image.network(
                            imageUrl ?? '',
                            fit: BoxFit.contain,
                            errorBuilder: (_, __, ___) => const Placeholder(),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
                Padding(
                  padding:
                  const EdgeInsets.symmetric(vertical: 8.0, horizontal: 16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Text(
                        fullName.toString(),
                        style: Theme.of(context).textTheme.headlineSmall,
                        textAlign: TextAlign.center,
                      ),
                      Text(
                        title.toString(),
                        style: Theme.of(context).textTheme.bodyLarge,
                        textAlign: TextAlign.center,
                      ),
                    ],
                  ),
                ),
                Align(
                  alignment: Alignment.bottomRight,
                  child: Padding(
                    padding: const EdgeInsets.only(
                        right: 16, bottom: 16),
                    child: GestureDetector(
                      onTap: () => onLike?.call(fullName, fullName.toString(), isLiked),
                      child: AnimatedSwitcher(
                        duration: const Duration(milliseconds: 200),
                        child: isLiked
                            ? const Icon(
                          Icons.favorite,
                          color: Colors.redAccent,
                          key: ValueKey<int>(0),
                        )
                            : const Icon(
                          Icons.favorite_border,
                          key: ValueKey<int>(1),
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
        )
      ),
    );
  }
}
