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

import '../../../../application/order/order_loader/order_loader_bloc.dart';
import '../../../../application/printer/print_struck/print_struck_bloc.dart';
import '../../../../common/theme/theme.dart';
import '../../../components/button/button.dart';
import '../../../components/spaces/space.dart';
import '../../../router/app_router.gr.dart';
import 'order_information.dart';
import 'order_list.dart';
import 'order_list_payment.dart';
import 'order_payment_summary.dart';

class OrderRightPanel extends StatelessWidget {
  final OrderLoaderState state;
  const OrderRightPanel({super.key, required this.state});

  @override
  Widget build(BuildContext context) {
    if (state.selectedOrder == null) {
      return Center(
        child: Text(
          "Belum ada order yang dipilih.",
          style: AppStyle.lg.copyWith(fontWeight: FontWeight.bold),
        ),
      );
    }

    return Column(
      children: [
        Expanded(
          child: SingleChildScrollView(
            child: Padding(
              padding: const EdgeInsets.all(16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  OrderInformation(order: state.selectedOrder),
                  OrderList(order: state.selectedOrder),
                  const SpaceHeight(16),
                  OrderPaymentSummary(order: state.selectedOrder),
                  const SpaceHeight(16),
                  if (state.selectedOrder?.payments != null &&
                      state.selectedOrder?.payments.isNotEmpty == true) ...[
                    OrderListPayment(order: state.selectedOrder),
                    const SpaceHeight(20),
                  ],
                ],
              ),
            ),
          ),
        ),
        Container(
          padding: const EdgeInsets.all(16),
          decoration: const BoxDecoration(color: AppColor.white),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              PopupMenuButton<String>(
                color: AppColor.white,
                onSelected: (value) {
                  switch (value) {
                    case 'checker':
                      context.read<PrintStruckBloc>().add(
                        PrintStruckEvent.checker(state.selectedOrder!),
                      );
                      break;
                    case 'kitchen':
                      context.read<PrintStruckBloc>().add(
                        PrintStruckEvent.kitchen(state.selectedOrder!),
                      );
                      break;
                    case 'bar':
                      context.read<PrintStruckBloc>().add(
                        PrintStruckEvent.bar(state.selectedOrder!),
                      );
                      break;
                  }
                },
                itemBuilder: (context) => [
                  const PopupMenuItem(
                    value: 'checker',
                    child: Row(
                      children: [
                        Icon(Icons.receipt_outlined),
                        SizedBox(width: 8),
                        Text('Print Checker'),
                      ],
                    ),
                  ),
                  const PopupMenuItem(
                    value: 'kitchen',
                    child: Row(
                      children: [
                        Icon(Icons.kitchen_outlined),
                        SizedBox(width: 8),
                        Text('Print Kitchen'),
                      ],
                    ),
                  ),
                  const PopupMenuItem(
                    value: 'bar',
                    child: Row(
                      children: [
                        Icon(Icons.local_bar_outlined),
                        SizedBox(width: 8),
                        Text('Print Bar'),
                      ],
                    ),
                  ),
                ],
                child: const AppElevatedButton.outlined(
                  onPressed: null, // handled by PopupMenuButton
                  label: 'Print',
                  icon: Icon(Icons.print),
                ),
              ),
              const SpaceWidth(8),
              AppElevatedButton.outlined(
                onPressed: () {
                  if (state.selectedOrder?.status == 'completed') {
                    context.read<PrintStruckBloc>().add(
                      PrintStruckEvent.payment(state.selectedOrder!),
                    );
                  } else {
                    context.read<PrintStruckBloc>().add(
                      PrintStruckEvent.cashier(state.selectedOrder!),
                    );
                  }
                },
                label: 'Print Bill',
                icon: const Icon(Icons.print),
              ),
              const SpaceWidth(8),
              if (state.selectedOrder?.status == 'completed')
                if (state.selectedOrder?.isRefund == false)
                  AppElevatedButton.outlined(
                    onPressed: () {
                      context.router.push(
                        RefundRoute(order: state.selectedOrder!),
                      );
                    },
                    label: 'Refund',
                    icon: const Icon(Icons.autorenew),
                  ),
              if (state.selectedOrder?.status == 'pending') ...[
                AppElevatedButton.outlined(
                  onPressed: () => context.router.push(
                    VoidRoute(order: state.selectedOrder!),
                  ),
                  label: 'Void',
                  icon: const Icon(Icons.undo),
                ),
                const SpaceWidth(8),
                AppElevatedButton.outlined(
                  onPressed: () {
                    context.router.push(
                      SplitBillRoute(order: state.selectedOrder!),
                    );
                  },
                  label: 'Split Bill',
                  icon: const Icon(Icons.calculate_outlined),
                ),

                const SpaceWidth(8),
                if (state.selectedOrder?.paymentStatus != 'partial')
                  AppElevatedButton.filled(
                    width: 120,
                    onPressed: () {
                      context.router.push(
                        PaymentRoute(order: state.selectedOrder!),
                      );
                    },
                    label: 'Bayar',
                    icon: const Icon(Icons.payment, color: Colors.white),
                  ),
              ],
            ],
          ),
        ),
      ],
    );
  }
}
