import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

import '../../api/api.dart';
import '../../bloc/change_delivery_pickup.dart';
import '../../bloc/outlet_detail.dart';
import '../../bloc/view_bill.dart';
import '../../helper/helper.dart';
import '../../helper/prefs.dart';
import '../../helper/widget/button_dialog.dart';
import '../../helper/widget/style.dart';
import '../select_branch.dart';

Future<dynamic> modalBottomSelectOption(BuildContext context) {
  void ontapOkeChangeOutlet() {
    Navigator.pop(context);
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (_) => const SelectBranch(
          isFormBill: true,
        ),
      ),
    );
  }

  void onTapCancelChange() {
    Navigator.pop(context);
  }

  return showModalBottomSheet(
    backgroundColor: backgroundWhite,
    context: context,
    shape: const RoundedRectangleBorder(
      borderRadius: BorderRadius.only(
        topLeft: Radius.circular(19),
        topRight: Radius.circular(19),
      ),
    ),
    barrierColor: Colors.grey.withOpacity(0.9),
    builder: (contextModal) {
      return Container(
        padding: const EdgeInsets.symmetric(
          horizontal: paddingLeftRight,
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Center(
              child: Container(
                margin: const EdgeInsets.only(
                  top: 8,
                  bottom: 28,
                ),
                width: 50,
                height: 3,
                color: disabledColor,
              ),
            ),
            defaultText(
              context,
              'Pilih Tipe Order',
              style: pickUpdeliveryStyleFont(),
            ),
            GestureDetector(
              onTap: () {
                if (getIsPickup()) {
                  EasyLoading.show(
                    status: 'Please wait...',
                    maskType: EasyLoadingMaskType.none,
                  );
                  Api.changeTypeOrder(typeOrderPickup).then((value) {
                    EasyLoading.dismiss();
                    if (value) {
                      context
                          .read<ChangeDeliveryPickupBloc>()
                          .toDelivery(false);
                      setIsCustomerDelivery(false);
                      context.read<ViewBillBloc>().getBill();
                      setTypeOrder(typeOrderPickup);
                    }
                  });
                  context.read<ExpandDetailOutlet>().show(false);
                  Navigator.pop(context);
                } else {
                  String textOnOk = 'OK';
                  String textOnCancel = 'Batal';
                  String title = 'Info';
                  String description = '''
Outlet ini tidak dapat menerima pickup order, 
ingin ganti outlet ?

Note: Ganti outlet akan menyebabkan orderan anda saat ini hilang
''';
                  buttonDialogGlobal(
                    context,
                    title,
                    description,
                    textOnOk,
                    textOnCancel,
                    ontapOkeChangeOutlet,
                    onTapCancelChange,
                    okButtonColor: buttonColor,
                  );
                }
              },
              child: Container(
                padding: const EdgeInsets.only(top: 19, bottom: 25, left: 10),
                color: backgroundWhite,
                child: Row(
                  children: [
                    const Image(
                      image: AssetImage('assets/icons/pickup-icon.png'),
                    ),
                    const SizedBox(
                      width: 12,
                    ),
                    defaultText(
                      context,
                      'Pick Up',
                      style: pickUpdeliveryStyleFont(),
                    )
                  ],
                ),
              ),
            ),
            GestureDetector(
              onTap: () {
                if (!getIsDelivery()) {
                  String textOnOk = 'OK';
                  String textOnCancel = 'Batal';
                  String title = 'Info';
                  String description = '''
Outlet ini tidak dapat delivery order, 
ingin ganti outlet ?

Note: Ganti outlet akan menyebabkan orderan anda saat ini hilang
''';
                  buttonDialogGlobal(
                    context,
                    title,
                    description,
                    textOnOk,
                    textOnCancel,
                    ontapOkeChangeOutlet,
                    onTapCancelChange,
                    okButtonColor: buttonColor,
                  );
                } else {
                  EasyLoading.show(
                    status: 'Please wait...',
                    maskType: EasyLoadingMaskType.none,
                  );
                  Api.changeTypeOrder(typeOrderDelivery).then((value) {
                    if (value) {
                      EasyLoading.dismiss();
                      setIsCustomerDelivery(true);
                      context.read<ChangeDeliveryPickupBloc>().toDelivery(true);
                      context.read<ViewBillBloc>().getBill();
                      setTypeOrder(typeOrderDelivery);
                    }
                  });
                  context.read<ExpandDetailOutlet>().show(false);
                  Navigator.pop(context);
                }
              },
              child: Container(
                padding: const EdgeInsets.only(left: 10, bottom: 24),
                color: backgroundWhite,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        const Image(
                          image: AssetImage('assets/icons/delivery-icon.png'),
                        ),
                        const SizedBox(
                          width: 12,
                        ),
                        defaultText(
                          context,
                          'Delivery',
                          style: pickUpdeliveryStyleFont(),
                        )
                      ],
                    ),
                    // defaultText(
                    //   context,
                    //   '23 Menit',
                    //   style: pickUpdeliveryStyleFont(),
                    // )
                  ],
                ),
              ),
            )
          ],
        ),
      );
    },
  );
}
