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

import '../../../../../../application/checkout/checkout_form/checkout_form_bloc.dart';
import '../../../../../../common/extension/extension.dart';
import '../../../../../../common/theme/theme.dart';
import '../../../../../../common/types/order_type.dart';
import '../../../../../components/button/button.dart';
import '../../../../../components/dialog/dialog.dart';
import '../../../../../router/app_router.gr.dart';

class HomeRightTitle extends StatelessWidget {
  // final TableModel? table;
  const HomeRightTitle({super.key});

  @override
  Widget build(BuildContext context) {
    return BlocBuilder<CheckoutFormBloc, CheckoutFormState>(
      builder: (context, state) {
        return Container(
          padding: const EdgeInsets.only(top: 20),
          height: context.deviceHeight * 0.15,
          decoration: const BoxDecoration(
            color: AppColor.primary,
            border: Border(left: BorderSide(color: Colors.white, width: 1.0)),
          ),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Expanded(
                child: Row(
                  children: [
                    _buildButton('Daftar Pesanan', Icons.list, () {
                      context.router.push(OrderRoute(status: 'pending'));
                    }),
                  ],
                ),
              ),
              Expanded(
                child: Row(
                  children: [
                    _buildButton(
                      state.orderType.value.toTitleCase(),
                      state.orderType.icon,
                      () {
                        showDialog(
                          context: context,
                          builder: (context) {
                            return const OrderTypeDialog();
                          },
                        );
                      },
                    ),
                    switch (state.orderType) {
                      OrderType.dineIn => _buildButton(
                        state.table == null
                            ? 'Pilih Meja'
                            : (state.table?.tableName ?? '-'),
                        Icons.table_restaurant_outlined,
                        () {
                          context.router.navigate(
                            const MainRoute(children: [TableRoute()]),
                          );
                        },
                      ),
                      OrderType.takeAway => const SizedBox.shrink(),
                      OrderType.delivery => _buildButton(
                        state.delivery == null
                            ? 'Pilih Pengiriman'
                            : (state.delivery?.name ?? "-"),
                        Icons.motorcycle_outlined,
                        () {
                          showDialog(
                            context: context,
                            builder: (context) {
                              return const DeliveryDialog();
                            },
                          );
                        },
                      ),
                      OrderType.freeTable => const SizedBox.shrink(),
                    },
                  ],
                ),
              ),
            ],
          ),
        );
      },
    );
  }

  Expanded _buildButton(String label, IconData icon, Function() onPressed) {
    return Expanded(
      child: AppElevatedButton.filled(
        width: 180.0,
        height: 40,
        elevation: 0,
        onPressed: onPressed,
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        icon: Icon(icon, color: Colors.white, size: 24),
        label: label,
      ),
    );
  }
}
