// ignore_for_file: must_be_immutable

import 'package:byod/helper/widget/style.dart';
import 'package:byod/models/order_details.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../api/api.dart';
import '../../bloc/order_bloc.dart';
import '../../bloc/view_bill.dart';
import '../../models/filter_menu.dart';
import '../../models/order_variants.dart';
import '../../models/orders.dart';
import '../helper.dart';

class NoteModalBottomSheet extends StatelessWidget {
  NoteModalBottomSheet({
    Key? key,
    required this.noteController,
    required this.initialValue,
    required this.orderVariants,
    required this.menuItem,
    required this.listOrders,
    required this.from,
    this.idOrderan = '',
    this.noteInit = '',
    this.idMenu = '',
    this.menuName = '',
    this.imageUrl = '',
    this.priceMemu = 0,
    this.editVariantConfirmationPage = false,
    required this.contextFrom,
    this.indexOrder = 0,
    this.indexOrderDetail = 0,
  }) : super(key: key);

  late TextEditingController noteController;
  final int initialValue;
  final List<OrderVariant> orderVariants;
  final FilterMenu menuItem;
  final List<Orders> listOrders;
  final String from;
  final bool editVariantConfirmationPage;
  final String idOrderan;
  final String noteInit;
  final BuildContext contextFrom;
  final String idMenu;
  final String menuName;
  final String imageUrl;
  final int priceMemu;
  final int indexOrder;
  final int indexOrderDetail;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 20),
      height: MediaQuery.of(context).size.height * 0.9,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: const EdgeInsets.only(
              top: 20,
              bottom: 20,
            ),
            child: defaultText(
              context,
              'catatan untuk pesanan',
              style: textHeaderNote(),
            ),
          ),
          const Divider(
            thickness: 2,
            color: Colors.white,
          ),
          TextField(
            maxLength: maxLengthTextField,
            maxLines: 5,
            autofocus: true,
            keyboardType: TextInputType.text,
            controller: noteController,
            style: noteModal(),
            decoration: InputDecoration(
              border: InputBorder.none,
              hintText: placeHolderNote,
              hintStyle: const TextStyle(
                color: Colors.grey,
                fontFamily: 'OpenSans',
                fontSize: 14,
              ),
            ),
          ),
          const Divider(
            thickness: 2,
            color: Colors.white,
          ),
          const SizedBox(
            height: 20,
          ),
          GestureDetector(
            onTap: () {
              if (from == fromMenu) {
                Map<String, dynamic> insertData = {
                  'menu_id': menuItem.id,
                  'menu_name': menuItem.name,
                  'initvalue': initialValue,
                  'menu_price': amountParseToInt(menuItem.price),
                  'old_order': listOrders,
                  'image_url': menuItem.imageUrlMedium,
                  'total_price': amountParseToInt(menuItem.price),
                  'order_variant': orderVariants,
                  'note': noteController.text,
                };
                context.read<OrdersBloc>().insertData(insertData);
              } else if (from == fromBill) {
                if (idOrderan != '') {
                  Api.changeOrderDetail(
                    contextFrom,
                    idOrderan,
                    initialValue,
                    noteController.text,
                  ).then((value) {
                    context.read<ViewBillBloc>().getBill();
                  });
                }
              } else if (from == fromConfirmationPage &&
                  editVariantConfirmationPage) {
                OrderDetail orderDetailNow = OrderDetail(
                  allIdVariant: listOrders[indexOrder]
                      .orderDetail[indexOrderDetail]
                      .allIdVariant,
                  totalItem: listOrders[indexOrder]
                      .orderDetail[indexOrderDetail]
                      .totalItem,
                  totalHarga: listOrders[indexOrder]
                      .orderDetail[indexOrderDetail]
                      .totalHarga,
                  note: noteController.text,
                  orderVariant: listOrders[indexOrder]
                      .orderDetail[indexOrderDetail]
                      .orderVariant,
                );
                Map<String, dynamic> insertData = {
                  'action': 'editVariant',
                  'list_orders': listOrders,
                  'single_order_detail': orderDetailNow,
                };
                context.read<OrdersBloc>().insertData(insertData);
              } else if (from == fromConfirmationPage) {
                Map<String, dynamic> insertData = {
                  'menu_id': idMenu,
                  'menu_name': menuName,
                  'initvalue': initialValue,
                  'menu_price': priceMemu,
                  'old_order': listOrders,
                  'image_url': menuItem.imageUrlMedium,
                  'total_price': priceMemu,
                  'order_variant': orderVariants,
                  'note': noteController.text,
                };
                context.read<OrdersBloc>().insertData(insertData);
              }
              Navigator.pop(context);
            },
            child: Align(
              alignment: Alignment.centerRight,
              child: Container(
                height: 36,
                width: 89,
                decoration: BoxDecoration(
                  color: buttonColor,
                  borderRadius: BorderRadius.circular(21),
                ),
                child: Center(
                  child: defaultText(
                    context,
                    "Simpan",
                    style: simpanButtonNote(),
                  ),
                ),
              ),
            ),
          )
        ],
      ),
    );
  }
}
