// ignore_for_file: sized_box_for_whitespace

import 'package:byod/bloc/branch_list.dart';
import 'package:byod/bloc/filter_menu.dart';
import 'package:byod/helper/helper.dart';
import 'package:byod/helper/prefs.dart';
import 'package:byod/models/branchs.dart';
import 'package:byod/ui/home/new_home2.dart';
import 'package:byod/ui/screen_responsive.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../bloc/search_branch.dart';
import '../helper/widget/style.dart';
import 'app_bar_select_branch.dart';

class SelectBranch extends StatelessWidget {
  const SelectBranch({super.key});

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: backgroundColor,
        body: ScreenResponsive(
          widget: const CoreBranch(),
          widthScreen: MediaQuery.of(context).size.width,
          isCoreLayout: true,
        ),
      ),
    );
  }
}

class CoreBranch extends StatelessWidget {
  const CoreBranch({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: paddingLeftRight),
      child: BlocBuilder<BranchList, List<Branch>>(
        builder: (context, listB) {
          return BlocBuilder<SearchBranch, List<Branch>>(
            builder: (contextSearch, listSearch) {
              List<Branch> listBranch;
              if (listSearch.isNotEmpty) {
                if (listSearch[0].id == '0') {
                  listBranch = listB;
                } else {
                  listBranch = listSearch;
                }
              } else {
                listBranch = listSearch;
              }

              return Column(
                children: [
                  CustomAppBar(
                      // allBranch: listB,
                      ),
                  const SizedBox(
                    height: 16,
                  ),
                  Expanded(
                    child: ListView.builder(
                        itemCount: listBranch.length,
                        itemBuilder: (context, i) {
                          return GestureDetector(
                            onTap: () {
                              context.read<FilterMenuBloc>().catAndMenu(
                                    listBranch[i].branchCode,
                                    getBrand(),
                                    getRole(),
                                    getCashierName(),
                                    getOrderId(),
                                  );
                              setIsDelivery(listBranch[i].isDelivery);
                              setIsPickup(listBranch[i].isPickup);
                              setBranch(listBranch[i].branchCode);
                              Navigator.pushReplacement(
                                context,
                                MaterialPageRoute(
                                    builder: (_) => const NewHome2()),
                              );
                            },
                            child: Container(
                              margin: const EdgeInsets.only(
                                bottom: 8,
                                left: paddingLeftRight,
                                right: paddingLeftRight,
                              ),
                              decoration: BoxDecoration(
                                borderRadius: BorderRadius.circular(6),
                                color: backgroundWhite,
                              ),
                              height: 72,
                              child: Container(
                                padding: const EdgeInsets.only(
                                  left: 16,
                                  right: 12,
                                  bottom: 16,
                                  top: 5,
                                ),
                                child: Row(
                                  children: [
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.start,
                                        mainAxisAlignment:
                                            MainAxisAlignment.center,
                                        children: [
                                          defaultText(
                                            context,
                                            listBranch[i].name,
                                            style: historyOrderStyle(),
                                          ),
                                          const SizedBox(
                                            height: 4,
                                          ),
                                          defaultText(
                                            context,
                                            "${listBranch[i].distance} KM",
                                            style: historyOrderStyle(),
                                          )
                                        ],
                                      ),
                                    ),
                                    Container(
                                      width: 145,
                                      child: Column(
                                        crossAxisAlignment:
                                            CrossAxisAlignment.end,
                                        mainAxisAlignment:
                                            MainAxisAlignment.spaceBetween,
                                        children: [
                                          defaultText(
                                            context,
                                            (listBranch[i].isDelivery)
                                                ? 'Delivery Tersedia'
                                                : 'Delivery Tidak Tersedia',
                                            style: historyOrderStyle(
                                              color: textGreyDeskripsi,
                                            ),
                                          ),
                                          const SizedBox(
                                            height: 15,
                                          ),
                                          defaultText(
                                            context,
                                            (listBranch[i].isPickup)
                                                ? 'Pickup Tersedia'
                                                : 'Pickup Tidak Tersedia',
                                            style: historyOrderStyle(
                                              color: textGreyDeskripsi,
                                            ),
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            ),
                          );
                        }),
                  ),
                ],
              );
            },
          );
        },
      ),
    );
  }
}
