import 'package:hmg_patient_app/core/enum/viewstate.dart';
import 'package:hmg_patient_app/core/model/pharmacies/ShoppingCart.dart';
import 'package:hmg_patient_app/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:hmg_patient_app/core/viewModels/project_view_model.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:provider/provider.dart';

class ProductOrderItem extends StatefulWidget {
  final ShoppingCart? item;
  final VoidCallback? changeCartItems;
  final VoidCallback? deleteCartItems;
  final OrderPreviewViewModel? model;

  ProductOrderItem(
      {this.item, this.changeCartItems, this.deleteCartItems, this.model});

  @override
  _ProductOrderItemState createState() => _ProductOrderItemState();
}

class _ProductOrderItemState extends State<ProductOrderItem> {
  TextEditingController _quantityController = new TextEditingController();
  String _totalPrice = "0";

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

  @override
  Widget build(BuildContext context) {
    _quantityController.text = "${widget.item!.quantity!}";
    _totalPrice =
        "${(widget.item!.product!.price! * widget.item!.quantity!).toStringAsFixed(2)}";
    ProjectViewModel projectProvider = Provider.of(context);

    return Column(
      children: [
        ListTile(
          leading: InkWell(
            onTap: () => {widget.deleteCartItems!()},
            child: Icon(
              FontAwesomeIcons.trashAlt,
              size: 15,
              color: Colors.grey.shade700,
            ),
          ),
          title: Container(
            child: Row(
//              widget.item.product.stock_quantity===0
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.max,
              children: [
                (widget.item!.product!.images! != null &&
                        widget.item!.product!.images!.length > 0)
                    ? Image.network(
                        widget!.item!.product!.images![0]!.src!,
                        fit: BoxFit.cover,
                        height: 80,
                      )
                    : Image.asset(
                        "assets/images/no_image.png",
                        fit: BoxFit.cover,
                        height: 80,
                      ),
                Expanded(
                  child: Container(
                    margin:
                        const EdgeInsets.symmetric(vertical: 8, horizontal: 8),
                    child: Container(
                      child: Column(
                        mainAxisSize: MainAxisSize.max,
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Container(
                            child: Texts(
                              projectProvider.isArabic
                                  ? widget.item!.product!.namen!
                                  : "${widget.item!.product!.name!}",
                              regular: true,
                              textAlign: TextAlign.justify,
                              fontSize: 12,
                              fontWeight: FontWeight.w400,
                            ),
                            margin: const EdgeInsets.only(bottom: 4),
                          ),
                          Container(
                            child: Texts(
                              "${(widget.item!.product!.price!).toStringAsFixed(2)} ${projectProvider.isArabic ? widget.item!.currencyn! : widget.item!.currency}",
                              fontSize: 14,
                              fontWeight: FontWeight.bold,
                            ),
                            margin: const EdgeInsets.only(bottom: 4),
                          ),
                          Container(
                            margin: EdgeInsets.symmetric(vertical: 4),
                            child: Row(
                              children: [
                                InkWell(
                                  onTap: () =>
                                      {_quantityOnChangeClick(Operation.dec)},
                                  child: Container(
                                    width: 25,
                                    height: 25,
                                    child: Icon(
                                      Icons.remove,
                                      color: Colors.grey.shade400,
                                      size: 20,
                                    ),
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: Colors.grey.shade400,
                                        width: 1.0,
                                      ),
                                    ),
                                  ),
                                ),
                                Container(
                                  margin:
                                      const EdgeInsets.symmetric(horizontal: 4),
                                  width: 25,
                                  height: 25,
                                  color: Colors.grey.shade300,
                                  child: Center(
                                      child: TextField(
                                    cursorColor: Colors.black,
                                    keyboardType: TextInputType.number,
                                    controller: _quantityController,
                                    textAlign: TextAlign.center,
                                    onChanged: (text) {
                                      setState(() {
                                        var value = int.tryParse(text);
                                        if (value == null) {
                                          widget!.item!.quantity = 0;
                                        } else {
                                          widget.item!.quantity =
                                              int.parse(text);
                                        }
                                        _totalPrice =
                                            "${(widget.item!.product!.price! * widget.item!.quantity!).toStringAsFixed(2)}";
                                      });
                                    },
                                  )),
                                ),
                                InkWell(
                                  onTap: () =>
                                      {_quantityOnChangeClick(Operation.inc)},
                                  child: Container(
                                    width: 25,
                                    height: 25,
                                    child: Icon(
                                      Icons.add,
                                      color: Colors.grey.shade400,
                                      size: 20,
                                    ),
                                    decoration: BoxDecoration(
                                      border: Border.all(
                                        color: Colors.grey.shade400,
                                        width: 1.0,
                                      ),
                                    ),
                                  ),
                                ),
                                Expanded(
                                  child: Column(
                                    crossAxisAlignment: CrossAxisAlignment.end,
                                    children: [
                                      Texts(
                                        TranslationBase.of(context).total,
                                        color: Colors.grey.shade500,
                                        fontWeight: FontWeight.bold,
                                        fontSize: 12,
                                      ),
                                      Texts(
                                        "$_totalPrice ${projectProvider.isArabic ? widget.item!.currencyn! : widget.item!.currency!}",
                                        fontSize: 12,
                                        fontWeight: FontWeight.bold,
                                      ),
                                      widget.item!.product!.stockQuantity == 0
                                          ? Texts(
                                              projectProvider.isArabic
                                                  ? widget.item!.product!
                                                      .stockAvailabilityn!
                                                  : widget.item!.product!
                                                      .stockAvailability,
                                              fontWeight: FontWeight.normal,
                                              fontSize: 13,
                                              color: Colors.red,
                                            )
                                          : Texts(""),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                )
              ],
            ),
          ),
        ),
        const Divider(
          color: Color(0xFFD6D6D6),
          height: 15,
          thickness: 1,
          indent: 0,
          endIndent: 0,
        ),
      ],
    );
  }

  _quantityOnChangeClick(Operation operation) {
    int newValue = 0;
    setState(() async {
      switch (operation) {
        case Operation.inc:
          {
            newValue = widget.item!.quantity! + 1;
          }
          break;

        case Operation.dec:
          {
            newValue = widget.item!.quantity! - 1;
          }
          break;
        default:
          {
            //statements;
          }
          break;
      }
      if (newValue > 0) {
        widget.item!.quantity = newValue;
         widget!.changeCartItems!();
        if (widget.model!.state == ViewState.ErrorLocal) {
          if (operation == Operation.dec) {
            newValue = widget.item!.quantity! + 1;
          } else {
            newValue = widget.item!.quantity! - 1;
          }
          widget.item!.quantity = newValue;
        }
        _quantityController.text = "${widget.item!.quantity!}";
        _totalPrice =
            "${(widget.item!.product!.price! * widget.item!.quantity!).toStringAsFixed(2)}";
      }
    });
  }

  @override
  void dispose() {
    // Clean up the controller when the widget is removed from the
    // widget tree.
    _quantityController.dispose();
    super.dispose();
  }
}

enum Operation { inc, dec }
