import 'package:cached_network_image/cached_network_image.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/responses/PackagesCartItemsResponseModel.dart';
import 'package:diplomaticquarterapp/core/model/packages_offers/responses/PackagesResponseModel.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/uitl/utils.dart';
import 'package:diplomaticquarterapp/widgets/CounterView.dart';
import 'package:diplomaticquarterapp/widgets/data_display/text.dart';
import 'package:diplomaticquarterapp/widgets/others/StarRating.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';


bool wide = true;

class PackagesCartItemCard extends StatefulWidget {
  final PackagesCartItemsResponseModel itemModel;
  final StepperCallbackFuture shouldStepperChangeApply ;

  const PackagesCartItemCard(
      {
        @required this.itemModel,
        @required this.shouldStepperChangeApply,
      Key key})
      : super(key: key);

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

class PackagesCartItemCardState extends State<PackagesCartItemCard> {

  @override
  Widget build(BuildContext context) {

    wide = !wide;
    return Container(
        color: Colors.transparent,
        child: Card(
          elevation: 3,
            shadowColor: Colors.grey[100],
            color: Colors.white,
            child: Stack(
              children: [
                Container(
                  height: 100,
                  child: Row(
                    children: [
                      _image(widget.itemModel.product),
                      Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          _itemName(widget.itemModel.product.getName()),
                          Row(
                            children: [
                              _itemPrice(widget.itemModel.product.price, context: context),
                              _priceSeperator(),
                              _itemOldPrice(widget.itemModel.product.oldPrice, context: context),
                            ],
                          ),
                          Row(
                            children: [
                              _itemCounter(
                                  widget.itemModel.quantity,
                                  minQuantity: widget.itemModel.product.orderMinimumQuantity,
                                  maxQuantity: widget.itemModel.product.orderMaximumQuantity, 
                                  shouldStepperChangeApply: (apply,total) async{
                                    bool success = await widget.shouldStepperChangeApply(apply,total);
                                    if(success == true)
                                      setState(() => widget.itemModel.quantity = total);
                                    return success;
                                  }
                              ),
                            ],
                          ),
                        ],
                      )
                    ],
                  ),
                ),

                Positioned(
                  bottom: 8,
                  left: 10,
                  child: Row(
                    children: [
                      _totalPrice((widget.itemModel.product.price * widget.itemModel.quantity), context: context),
                      _totalLabel(context: context),
                    ],
                  ),
                )
              ],
            )
        )
    );
  }
}


// --------------------
// Product Image
// --------------------
Widget _image(PackagesResponseModel model) => AspectRatio(
  aspectRatio: 1/1,
  child: Padding(
    padding: const EdgeInsets.all(10),
    child: Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey[300], width: 0.25),
        boxShadow: [
          BoxShadow(color: Colors.grey[200], blurRadius: 2.0, spreadRadius: 1, offset: Offset(1,1.5))
        ],
        borderRadius: BorderRadius.circular(8),
        color: Colors.white,
        shape: BoxShape.rectangle,
      ),
      child: ClipRRect(
          borderRadius: BorderRadius.circular(8),
          child: (model.images.isNotEmpty)
              ? Utils.loadNetworkImage(url: model.images.first.src, fitting:BoxFit.fill)
              : Container(color: Colors.grey[200])
      ),
    ),
  ),
);

// --------------------
// Product Name
// --------------------
Widget _itemName(String name) => Padding(
    padding: const EdgeInsets.all(0),
    child: Texts(
        name,
            fontWeight: FontWeight.normal,
            color: Colors.black,
            fontSize: 15
    )
);


Widget _itemPrice(double price, {@required BuildContext context}) => Padding(
    padding: const EdgeInsets.all(0),
    child: Texts(
        '${price} ${TranslationBase.of(context).sar}',
            fontWeight: FontWeight.bold,
            color: Colors.green,
            fontSize: 15
    )
);


// --------------------
// Price Seperator
// --------------------
Widget _priceSeperator() => Padding(
  padding: const EdgeInsets.only(left: 3, right: 3),
  child: Container(height: 0.5, width: 5, color: Colors.grey[100],),
);


// --------------------
// Product Price
// --------------------
Widget _itemOldPrice(double oldPrice, {@required BuildContext context}) => Padding(
    padding: const EdgeInsets.all(0),
    child: Texts(
        '${oldPrice} ${TranslationBase.of(context).sar}',
            fontWeight: FontWeight.normal,
            decoration: TextDecoration.lineThrough,
            color: Colors.grey,
            fontSize: 10
    )
);

// --------------------
// Product Price
// --------------------
Widget _itemCounter(int quantity, {int minQuantity, int maxQuantity, StepperCallbackFuture shouldStepperChangeApply}) => Padding(
    padding: const EdgeInsets.all(0),
    child: StepperView(
      height: 25,
      backgroundColor: Colors.grey[300],
      foregroundColor: Colors.grey[200],
      initialNumber: quantity,
      minNumber: minQuantity,
      maxNumber: maxQuantity,
      counterCallback: shouldStepperChangeApply,
      decreaseCallback: (){},
      increaseCallback: (){},
    )
);


Widget _totalLabel({@required BuildContext context}) => Padding(
    padding: const EdgeInsets.all(0),
    child: Texts(
        '${TranslationBase.of(context).totalWithColonRight}',
            fontWeight: FontWeight.bold,
            color: Colors.grey[600],
            fontSize: 13
    )
);


Widget _totalPrice(double totalPrice, {@required BuildContext context}) => Padding(
    padding: const EdgeInsets.all(0),
    child: Texts(
        '${totalPrice.toStringAsFixed(2)} ${TranslationBase.of(context).sar}',
            fontWeight: FontWeight.normal,
            color: Colors.green,
    )
);

