import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../app.dart';

class FavoriteWidget extends StatefulWidget {
  final ProductModel model;
  final top;
  final right;

  FavoriteWidget({
    Key? key,
    required this.model,
    this.top = 0.0,
    this.right = 0.0,
  }) : super(key: key);

  @override
  State<FavoriteWidget> createState() => _FavoriteWidgetState();
}

class _FavoriteWidgetState extends State<FavoriteWidget> {
  bool isFavLoading = false;

  @override
  Widget build(BuildContext context) {
    return Positioned(
      top: widget.top,
      right: widget.right,
      child: GestureDetector(
        onTap: () async {
          debugPrint('Like tapped');

          setState(() => isFavLoading = true);

          final result = await WishlistApi.addRemove(widget.model.id);

          setState(() => isFavLoading = false);

          if (result) setState(() => widget.model.isWishlisted = !widget.model.isWishlisted);
        },
        child: isFavLoading
            ? SizedBox(
                height: 30,
                width: 30,
                child: CustomLoader(),
              )
            : Container(
                padding: EdgeInsets.all(2),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(100),
                  // border: Border.all(color: Colors.grey),
                ),
                child: Icon(
                  Icons.favorite,
                  color: widget.model.isWishlisted ? Colors.red : Colors.grey.withOpacity(0.60),
                  size: 20.sp,
                ),
              ),
      ),
    );
  }
}
