import 'package:diplomaticquarterapp/core/model/pharmacies/PharmacyProduct.dart';
import 'package:diplomaticquarterapp/core/service/AuthenticatedUserObject.dart';
import 'package:diplomaticquarterapp/core/viewModels/pharmacyModule/product_detail_view_model.dart';
import 'package:diplomaticquarterapp/pages/pharmacies/screens/product-details/footor/quantity_box.dart';
import 'package:diplomaticquarterapp/uitl/translations_delegate_base.dart';
import 'package:diplomaticquarterapp/widgets/buttons/secondary_button.dart';
import 'package:diplomaticquarterapp/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';
import 'package:hexcolor/hexcolor.dart';

import '../../../../../locator.dart';
import '../../../../../routes.dart';
import '../../cart-page/cart-order-page.dart';
import '../product-detail.dart';

class FooterWidget extends StatefulWidget {
  final bool isAvailable;
  final int maxQuantity;
  final int minQuantity;
  final int quantityLimit;
  final PharmacyProduct item;
  final Function addToCartFunction;
  final ProductDetailViewModel model;

  int quantity;
  bool isOverQuantity;

  FooterWidget(this.isAvailable, this.maxQuantity, this.minQuantity,
      this.quantityLimit, this.item,
      {this.quantity,
      this.isOverQuantity = false,
      this.addToCartFunction,
      this.model});

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

class _FooterWidgetState extends State<FooterWidget> {
  double quantityUI = 80;
  bool showUI = false;
  AuthenticatedUserObject authenticatedUserObject =
      locator<AuthenticatedUserObject>();

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.all(
          Radius.circular(0.0),
        ),
        border: Border.all(color: HexColor('#707070'), width: 0),
      ),
      width: double.infinity,
      height: quantityUI,
      child: Column(
        children: [
          showUI
              ? Container(
                  width: double.infinity,
                  height: 100,
                  color: Colors.white,
                  child: Container(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        SizedBox(
                          height: 10,
                        ),
                        Row(
                          children: [
                            Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(
                                "Quantity",
                                style: TextStyle(
                                    fontSize: 15, fontWeight: FontWeight.bold),
                              ),
                            ),
                            InkWell(
                              child: Icon(Icons.close, color: Colors.black),
                              onTap: () {
                                setState(() {
                                  quantityUI = 80;
                                  showUI = false;
                                });
                              },
                            )
                          ],
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        ),
                        Container(
                          height: 50.0,
                          child: ListView(
                            scrollDirection: Axis.horizontal,
                            children: <Widget>[
                              SizedBox(
                                width: 5,
                              ),
                              for (int i = 1; i <= 10; i++)
                                QuantityBox(
                                    label: i,
                                    onTapFunc: onChangeValue,
                                    isSelected: widget.quantity == i),
                              Container(
                                width: 100,
                                decoration: BoxDecoration(
                                  border: Border.all(color: Colors.grey[300]),
                                  color: Colors.white,
                                ),
                                child: TextField(
                                  decoration: InputDecoration(
                                      labelText: ' Quantity # '),
                                  onChanged: (text) {
                                    if (int.tryParse(text) == null) {
                                      text = '';
                                    } else {
                                      setState(() {
                                        widget.quantity = int.parse(text);
                                        if (widget.quantity >=
                                            widget.quantityLimit) {
                                          widget.isOverQuantity = true;
                                        } else {
                                          widget.isOverQuantity = false;
                                        }
                                      });
                                    }
                                  },
                                ),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ),
                )
              : Container(
                  height: 20,
                ),
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Container(
                width: 70,
                height: 50,
                child: FlatButton(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      Expanded(
                        flex: 4,
                        child: Text(
                          widget.quantity.toString(),
                          style: TextStyle(fontSize: 20),
                        ),
                      ),
                      Expanded(
                        flex: 5,
                        child: Text(
                          TranslationBase.of(context).quantityShortcut,
                          style: TextStyle(fontSize: 16),
                        ),
                      ),
                    ],
                  ),
                  onPressed: () {
                    setState(() {
                      if (showUI) {
                        quantityUI = 80;
                        showUI = false;
                      } else {
                        quantityUI = 160;
                        showUI = true;
                      }
                    });
                  },
                ),
              ),
              Container(
                width: MediaQuery.of(context).size.width * 0.45,
                child: SecondaryButton(
                  label: TranslationBase.of(context).addToCart.toUpperCase(),
                  disabled: !widget.isAvailable && widget.quantity == 0 ||
                      widget.quantity > widget.quantityLimit ||
                      widget.item.rxMessage != null,
                  disableColor: Color(0xFF4CAF50),
                  onTap: () async {
                    if (!authenticatedUserObject.isLogin) {
                      Navigator.of(context).pushNamed(
                        WELCOME_LOGIN,
                      );
                    } else
                      await widget.addToCartFunction(
                          quantity: widget.quantity,
                          itemID: widget.item.id,
                          model: widget.model);
                  },
                  fontWeight: FontWeight.w600,
                  borderColor: Color(0xFF4CAF50),
                  borderRadius: 3,
                  color: Color(0xFF4CAF50),
                ),
              ),
              SizedBox(
                width: 5,
              ),
              Container(
                width: MediaQuery.of(context).size.width * 0.35,
                child: SecondaryButton(
                  label: TranslationBase.of(context).buyNow.toUpperCase(),
                  disabled: !widget.isAvailable && widget.quantity > 0 ||
                      widget.quantity > widget.quantityLimit ||
                      widget.item.rxMessage != null,
                  onTap: () async {
                    await widget.addToCartFunction(
                        quantity: widget.quantity,
                        itemID: widget.item.id,
                        model: widget.model);
                    Navigator.push(
                      context,
                      FadePage(page: CartOrderPage()),
                    );
                  },
                  fontWeight: FontWeight.w600,
                  borderColor: Colors.grey[800],
                  borderRadius: 3,
                  disableColor: Colors.grey[700],
                  color: !widget.isAvailable && widget.quantity > 0 ||
                          widget.quantity > widget.quantityLimit ||
                          widget.item.rxMessage != null
                      ? Colors.grey
                      : Colors.grey[800],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  onChangeValue(int i) {
    setState(() {
      widget.quantity = i;
      if (widget.quantity >= widget.quantityLimit) {
        widget.isOverQuantity = true;
      } else {
        widget.isOverQuantity = false;
        return widget.quantity;
      }
    });
  }
}
