import 'package:boilerplate/core/utils/authentication_util.dart';
import 'package:flutter/material.dart';

import '../../core/i18n/translations.g.dart';
import '../../core/styles/my_colors.dart';
import '../../core/styles/my_dimensions.dart';
import '../../core/styles/my_font.dart';

class IPMFavoriteButton extends StatefulWidget {
  final bool initialValue;
  final Function(bool) onChanged;

  IPMFavoriteButton({
    Key? key,
    required this.initialValue,
    required this.onChanged,
  }) : super(key: key);

  @override
  State<IPMFavoriteButton> createState() => _IPMFavoriteButtonState();
}

class _IPMFavoriteButtonState extends State<IPMFavoriteButton> {
  bool isFavorite = false;

  @override
  void initState() {
    super.initState();

    isFavorite = widget.initialValue;
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: () {
        AuthenticationUtil.checkHasUser(() {
          widget.onChanged(isFavorite);
          setState(() {
            isFavorite = !isFavorite;
          });
        });
      },
      child: Row(
        mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Icon(
            isFavorite ? Icons.favorite : Icons.favorite_border,
            color: isFavorite ? MyColor.orange : MyColor.grey,
            size: MyDimens.dimen14,
          ),
          SizedBox(width: MyDimens.dimen4),
          Text(
            AppLocalizations.products.like,
            style: MyFont.regular12.copyWith(
              color: MyColor.grey,
              height: 1.8,
            ),
            textAlign: TextAlign.center,
          )
        ],
      ),
    );
  }
}
