// ignore_for_file: sized_box_for_whitespace, must_be_immutable

import 'package:byod/bloc/order_bloc.dart';
import 'package:byod/helper/widget/poni_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../bloc/order_variant_temporary.dart';
import '../../helper/helper.dart';
import '../../helper/widget/style.dart';
import '../../models/order_details.dart';
import '../../models/order_variants.dart';
import '../../models/orders.dart';
import '../../models/variant_categories.dart';
import 'bottom_sheet_variant.dart';
import 'variant_cat.dart';

class ShowMenuModal extends StatefulWidget {
  String imageUrl;
  String namaMenu;
  int hargaMenu;
  bool isHavevariant;
  String description;
  String id;
  List<VariantCategories> variantCat;
  bool isSell;
  ShowMenuModal(
      {Key? key,
      required this.imageUrl,
      required this.namaMenu,
      required this.hargaMenu,
      required this.isHavevariant,
      required this.description,
      required this.id,
      this.variantCat = const [],
      required this.isSell})
      : super(key: key);

  @override
  State<ShowMenuModal> createState() => _ShowMenuModalState();
}

class _ShowMenuModalState extends State<ShowMenuModal> {
  List<OrderVariant> orderVariants = [];
  @override
  Widget build(BuildContext context) {
    double widthScreen = responsiveWidthScreen(context);
    double currentScreen = MediaQuery.of(context).size.width;
    double maxWidthScreen = getMaxWidthScreen(context, useResponsive);
    double leftRigthPadding = (currentScreen > maxWidthScreen)
        ? (currentScreen - widthScreen) / 2
        : 0;
    return BlocBuilder<OrdersBloc, List<Orders>>(
      builder: (context, list) {
        int indexListOrderan =
            list.indexWhere((listOrderan) => listOrderan.id == widget.id);
        int initialValue;
        if (indexListOrderan != -1) {
          initialValue = list[indexListOrderan].totalItem;
        } else {
          initialValue = 0;
        }
        return Container(
          padding: EdgeInsets.symmetric(horizontal: leftRigthPadding),
          child: Container(
            height: MediaQuery.of(context).size.height * 0.9,
            margin: EdgeInsets.only(
                left: widthScreen * 0.005,
                right: widthScreen * 0.005,
                top: MediaQuery.of(context).size.height * 0.005,
                bottom: MediaQuery.of(context).size.height * 0.005),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.only(
                    topRight: Radius.circular(roundedImage),
                    topLeft: Radius.circular(roundedImage)),
                color: backgroundColor),
            child: Container(
              margin: EdgeInsets.only(
                  left: widthScreen * 0.05,
                  right: widthScreen * 0.05,
                  top: MediaQuery.of(context).size.height * 0.02,
                  bottom: MediaQuery.of(context).size.height * 0.02),
              child: Column(
                children: [
                  const PoniBottomSheet(),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.03,
                  ),
                  Container(
                    // height: MediaQuery.of(context).size.height * 0.45,
                    width: double.infinity,
                    decoration: BoxDecoration(
                      borderRadius: BorderRadius.circular(roundedImage),
                      color: disabledColor.withOpacity(0.8),
                    ),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(roundedImage),
                      child: (widget.imageUrl != '')
                          ? Image(
                              fit: BoxFit.fill,
                              image: NetworkImage(widget.imageUrl),
                            )
                          : const Image(
                              fit: BoxFit.fill,
                              image: AssetImage('assets/noimage.png')),
                    ),
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.02,
                  ),
                  Align(
                    alignment: Alignment.topLeft,
                    child: textMenuNameBottomShetModalMenu(
                      context,
                      widget.namaMenu,
                    ),
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.02,
                  ),
                  (widget.description != '')
                      ? Align(
                          alignment: Alignment.topLeft,
                          child: textDescriptionBottomShetModalMenu(
                            context,
                            widget.description,
                          ),
                        )
                      : const SizedBox(),
                  (widget.description != '')
                      ? SizedBox(
                          height: MediaQuery.of(context).size.height * 0.02,
                        )
                      : const SizedBox(),
                  Align(
                    alignment: Alignment.topLeft,
                    child: textAmoutBottomShetModalMenu(
                      context,
                      widget.hargaMenu,
                    ),
                  ),
                  SizedBox(
                    height: MediaQuery.of(context).size.height * 0.02,
                  ),
                  const Spacer(),
                  GestureDetector(
                    onTap: () {
                      if (widget.isSell) {
                        if (widget.isHavevariant) {
                          if (indexListOrderan == -1) {
                            Navigator.pop(context);
                            context
                                .read<OrderVariantTemporaryBloc>()
                                .insertDataVariant(
                                    widget.id,
                                    widget.hargaMenu,
                                    1,
                                    []); // check pada bloc untuk mengethui list arraynya
                            Navigator.push(
                                context,
                                MaterialPageRoute(
                                    builder: (_) => VariantCat(
                                          namaMenu: widget.namaMenu,
                                          variantCategories: widget.variantCat,
                                          initPrice: widget.hargaMenu,
                                          idItem: widget.id,
                                          imageUrl: widget.imageUrl,
                                          listCurrentOrder: list,
                                          orderDetail: OrderDetail(
                                              allIdVariant: '',
                                              totalItem: 0,
                                              totalHarga: 0,
                                              note: '',
                                              orderVariant: []),
                                        )));
                          } else {
                            Navigator.pop(context);
                            showModalBottomSheet(
                              backgroundColor: Colors.white.withOpacity(0),
                              isScrollControlled: true,
                              context: context,
                              builder: (context) {
                                return CustomizeVariant(
                                    orders: list[indexListOrderan],
                                    variantCat: widget.variantCat);
                              },
                            );
                          }
                        } else {
                          initialValue += 1;
                          context.read<OrdersBloc>().insertData([
                            [
                              widget.id,
                              widget.namaMenu,
                              initialValue,
                              widget.hargaMenu,
                              list,
                              widget.imageUrl,
                              orderVariants,
                              widget.hargaMenu,
                              (indexListOrderan != -1)
                                  ? list[indexListOrderan].note
                                  : '',
                            ]
                          ], context);
                          Navigator.pop(context);
                        }
                      }
                    },
                    child: buttonBottom(
                      context,
                      (widget.isSell) ? "Tambahkan Orderan" : "Habis",
                      (widget.isSell) ? buttonColor : disabledColor,
                      double.infinity,
                    ),
                  )
                ],
              ),
            ),
          ),
        );
      },
    );
  }
}
