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

import '../../models/business.dart';
import '../../models/cart_info.dart';
import '../../store/store.dart';
import '../../utils/utils.dart';

class DesktopShoppingCartWidget extends StatefulWidget {
  final Business business;
  final Function onTap;

  DesktopShoppingCartWidget({@required this.business, this.onTap});

  @override
  State<StatefulWidget> createState() => DesktopShoppingCartWidgetState();
}

class DesktopShoppingCartWidgetState extends State<DesktopShoppingCartWidget> {
  CartInfo cartInfo;
  double totalPrice = 0.0;

  @override
  Widget build(BuildContext context) {
    totalPrice = 0.0;
    cartInfo = Utils.getCartInfoByBusiness(store.state.cartInfos, widget.business);
    if (cartInfo != null && cartInfo.businessInfo.id == widget.business.id) {
      totalPrice = cartInfo.getTotalPrice();
    }
    Row row = Row(
      mainAxisAlignment: MainAxisAlignment.end,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          padding: EdgeInsets.only(left: 16.0, right: 4.0, top: 8.0),
          child: Icon(
            Icons.shopping_basket_outlined,
            size: 24,
            color: Colors.blue,
          ),
        ),
        Container(
          padding: EdgeInsets.only(left: 4.0, right: 16.0, top: 8.0),
          child: Text(
            '\$${totalPrice.toStringAsFixed(2)}',
            style: TextStyle(
              color: totalPrice > 0 ? Colors.red : Colors.black45,
              fontWeight: totalPrice > 0 ? FontWeight.bold : FontWeight.normal,
            ),
          ),
        ),
      ],
    );
    return MouseRegion(
      cursor: SystemMouseCursors.click,
      child: GestureDetector(
        child: Container(
          child: row,
          width: 160.0,
        ),
        onTap: widget.onTap,
      ),
    );
  }

}