import 'package:hmg_patient_app/core/viewModels/pharmacyModule/OrderPreviewViewModel.dart';
import 'package:hmg_patient_app/pages/ToDoList/payment_method_select.dart';
import 'package:hmg_patient_app/pages/pharmacies/screens/payment-method-select-page.dart';
import 'package:hmg_patient_app/uitl/translations_delegate_base.dart';
import 'package:hmg_patient_app/widgets/data_display/text.dart';
import 'package:hmg_patient_app/widgets/transitions/fade_page.dart';
import 'package:flutter/material.dart';

class SelectPaymentOptionWidget extends StatefulWidget {
  final OrderPreviewViewModel model;
  final VoidCallback? changeMainState;
  final bool isUpdating;


  SelectPaymentOptionWidget(this.model, this.changeMainState, {this.isUpdating = false});

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

class _SelectPaymentOptionWidgetState extends State<SelectPaymentOptionWidget> {
  PaymentOption? paymentOption;

  _navigateToPaymentOption() {
    Navigator.push(context, FadePage(page: PaymentMethodSelectPage(model: widget.model,changeMainState:widget.changeMainState,isUpdating: widget.isUpdating,)))
        .then((result) => {
              setState(() {
                if (result != null) {
                  paymentOption = result;
                  widget.model.paymentCheckoutData.paymentOption =
                      paymentOption;
                }
                widget.changeMainState!();
              })
            });
  }

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

  @override
  Widget build(BuildContext context) {
    if (widget.model.paymentCheckoutData.paymentOption != null) {
      paymentOption = widget.model.paymentCheckoutData.paymentOption;
    }
    return Container(
      color: Colors.white,
      child: paymentOption == null
          ? InkWell(
              onTap: () => {_navigateToPaymentOption()},
              child: Container(
                margin: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
                child: Row(
                  children: [
                    Image.asset(
                      "assets/images/pharmacy_module/ic_payment_option.png",
                      width: 30.0,
                      height: 30.0,
                      fit: BoxFit.scaleDown,
                    ),
                    Expanded(
                      child: Container(
                        padding: EdgeInsets.symmetric(vertical: 0, horizontal: 6),
                        child: Texts(
                          TranslationBase.of(context).selectPaymentOption,
                          fontSize: 14,
                          fontWeight: FontWeight.bold,
                          color: Color(0xff0000ff),
                        ),
                      ),
                    ),
                    Icon(
                      Icons.arrow_forward_ios,
                      size: 20,
                      color: Colors.grey.shade400,
                    ),
                  ],
                ),
              ),
            )
          : Container(
              margin: EdgeInsets.symmetric(vertical: 12, horizontal: 12),
              child: Row(
                children: [
                  Image.asset(
                    "assets/images/pharmacy_module/ic_payment_option.png",
                    width: 30.0,
                    height: 30.0,
                    fit: BoxFit.scaleDown,
                  ),
                  Container(
                    margin: EdgeInsets.symmetric(horizontal: 8),
                    padding: EdgeInsets.symmetric(horizontal: 4, vertical: 0),
                    decoration: new BoxDecoration(
                      color: Colors.grey.shade100,
                      shape: BoxShape.rectangle,
                    ),
                    child: Image.asset(
                      widget.model.getPaymentOptionImage(paymentOption!),
                      width: 30.0,
                      height: 30.0,
                      fit: BoxFit.scaleDown,
                    ),
                  ),
                  Expanded(
                    child: Container(
                      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 6),
                      child: Texts(
                        widget.model.getPaymentOptionName(paymentOption!),
                        fontSize: 14,
                        fontWeight: FontWeight.bold,
                        color: Colors.black,
                      ),
                    ),
                  ),
                  InkWell(
                    onTap: () => {_navigateToPaymentOption()},
                    child: Texts(
                      TranslationBase.of(context).changeMethod,
                      fontSize: 12,
                      fontWeight: FontWeight.normal,
                      color: Color(0xff0000ff),
                    ),
                  ),
                ],
              ),
            ),
    );
  }
}
