part of "home_page.dart";

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

class _MarketCard extends StatefulWidget {
  final String beanId;
  final String colorGroup;
  final String? imageUrl;
  final OnLikeCallback onLike;
  final VoidCallback? onTap;

  const _MarketCard({
    required this.beanId,
    required this.colorGroup,
    required this.imageUrl,
    this.onLike,
    this.onTap,
  });

  factory _MarketCard.fromData(MarketData items,
      {OnLikeCallback? onLike, VoidCallback? onTap}) =>
      _MarketCard(
        beanId: items.beanId,
        colorGroup: items.colorGroup,
        imageUrl: items.imageUrl,
        onLike: onLike,
        onTap: onTap,
      );

  @override
  State<_MarketCard> createState() => _MarketCardState();
}

class _MarketCardState extends State<_MarketCard> {
  bool isLiked = false;

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: widget.onTap,
      child: Container(
        padding: const EdgeInsets.all(10),
        margin: const EdgeInsets.all(4),
        constraints: const BoxConstraints(minHeight: 140),
        decoration: BoxDecoration(
          color: const Color(0xFFE5E4E2),
          borderRadius: BorderRadius.circular(16),
        ),
        child: IntrinsicHeight(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              ClipRRect(
                borderRadius: BorderRadius.circular(14),
                child: SizedBox(
                  width: double.infinity,
                  height: 160,
                  child: Stack(
                    children: [
                      Positioned.fill(
                        child: Image.network(
                          widget.imageUrl ?? '',
                          fit: BoxFit.cover,
                          errorBuilder: (_, __, ___) => const Placeholder(),
                        ),
                      ),
                      Align(
                        alignment: Alignment.bottomRight,
                        child: Container(
                          decoration: const BoxDecoration(
                            color: Color(0xFFffef42),
                            borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(20),
                            ),
                          ),
                          padding: const EdgeInsets.fromLTRB(8, 2, 8, 2),
                          child: Text(
                            "новинка",
                            style: Theme.of(context)
                                .textTheme
                                .bodyMedium
                                ?.copyWith(color: Colors.black),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 8.0),
                child: Text(
                  widget.colorGroup,
                  style: const TextStyle(
                    fontSize: 20,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  const Spacer(),
                  Align(
                    alignment: Alignment.bottomRight,
                    child: Padding(
                      padding: const EdgeInsets.only(left: 8.0),
                      child: GestureDetector(
                        onTap: () {
                          setState(() {
                            isLiked = !isLiked;
                          });
                          widget.onLike?.call(widget.colorGroup, isLiked);
                        },
                        child: AnimatedSwitcher(
                          duration: const Duration(milliseconds: 100),
                          child: isLiked
                              ? const Icon(
                            Icons.favorite,
                            color: Colors.redAccent,
                            key: ValueKey<int>(0),
                          )
                              : const Icon(
                            Icons.favorite,
                            color: Colors.grey,
                            key: ValueKey<int>(1),
                          ),
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }
}
