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

import '../../../../application/void/void_form/void_form_bloc.dart';
import '../../../../common/extension/extension.dart';
import '../../../../common/theme/theme.dart';
import '../../../../common/types/void_type.dart';
import '../../../components/spaces/space.dart';
import 'void_product_card.dart';

class VoidLeftPanel extends StatefulWidget {
  final VoidFormState state;
  const VoidLeftPanel({super.key, required this.state});

  @override
  State<VoidLeftPanel> createState() => _VoidLeftPanelState();
}

class _VoidLeftPanelState extends State<VoidLeftPanel> {
  final ScrollController _scrollController = ScrollController();

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            padding: const EdgeInsets.all(16),
            decoration: const BoxDecoration(
              border: Border(
                bottom: BorderSide(width: 1, color: AppColor.border),
              ),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(12),
                topRight: Radius.circular(12),
              ),
            ),
            child: Row(
              children: [
                const Icon(
                  Icons.receipt_long,
                  color: AppColor.primary,
                  size: 24,
                ),
                const SpaceWidth(12),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        'Pesanan #${widget.state.order.orderNumber}',
                        style: AppStyle.xl.copyWith(
                          fontWeight: FontWeight.bold,
                          color: AppColor.primary,
                        ),
                      ),
                      Text(
                        'Meja: ${widget.state.order.tableNumber} • ${widget.state.order.orderType}',
                        style: AppStyle.md.copyWith(
                          color: AppColor.textSecondary,
                        ),
                      ),
                    ],
                  ),
                ),
                Container(
                  padding: const EdgeInsets.symmetric(
                    horizontal: 12,
                    vertical: 6,
                  ),
                  decoration: BoxDecoration(
                    color: _getStatusColor(
                      widget.state.order.status,
                    ).withOpacity(0.2),
                    borderRadius: BorderRadius.circular(16),
                  ),
                  child: Text(
                    widget.state.order.status.toUpperCase(),
                    style: AppStyle.sm.copyWith(
                      fontWeight: FontWeight.w600,
                      color: _getStatusColor(widget.state.order.status),
                    ),
                  ),
                ),
              ],
            ),
          ),
          Expanded(
            child: Scrollbar(
              controller: _scrollController,
              thumbVisibility: true,
              child: SingleChildScrollView(
                controller: _scrollController,
                padding: const EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        const Icon(
                          Icons.shopping_cart,
                          color: AppColor.primary,
                          size: 20,
                        ),
                        const SpaceWidth(8),
                        Text(
                          'Produk Pesanan (${widget.state.pendingItems.length})',
                          style: AppStyle.lg.copyWith(
                            fontWeight: FontWeight.bold,
                            color: AppColor.primary,
                          ),
                        ),
                      ],
                    ),
                    const SpaceHeight(16),

                    ...List.generate(widget.state.pendingItems.length, (index) {
                      final item = widget.state.pendingItems[index];
                      final voidQty =
                          widget.state.selectedItemQuantities[item.id] ?? 0;
                      final isSelected = voidQty > 0;
                      final canSelect = widget.state.voidType.isItem;

                      return VoidProductCard(
                        isSelected: isSelected,
                        item: item,
                        voidQty: voidQty,
                        canSelect: canSelect,
                        onTapDecrease: voidQty > 0
                            ? () {
                                context.read<VoidFormBloc>().add(
                                  VoidFormEvent.itemQuantityChanged(
                                    itemId: item.id,
                                    quantity: voidQty - 1,
                                  ),
                                );
                              }
                            : null,
                        onTapIncrease: voidQty < item.quantity
                            ? () {
                                context.read<VoidFormBloc>().add(
                                  VoidFormEvent.itemQuantityChanged(
                                    itemId: item.id,
                                    quantity: voidQty + 1,
                                  ),
                                );
                              }
                            : null,
                      );
                    }),
                  ],
                ),
              ),
            ),
          ),
          Container(
            padding: const EdgeInsets.all(16),
            child: Column(
              children: [
                _buildSummaryRow(
                  'Subtotal:',
                  (widget.state.order.subtotal).currencyFormatRpV2,
                ),
                _buildSummaryRow(
                  'Pajak:',
                  (widget.state.order.taxAmount).currencyFormatRpV2,
                ),
                // _buildSummaryRow(
                //   'Diskon:',
                //   '- ${(widget.state.order.discountAmount).currencyFormatRpV2}',
                // ),
                const Divider(thickness: 1),
                _buildSummaryRow(
                  'Total:',
                  (widget.state.order.totalAmount).currencyFormatRpV2,
                  isTotal: true,
                ),
                if (widget.state.voidType.isItem &&
                    widget.state.selectedItemQuantities.isNotEmpty) ...[
                  Divider(thickness: 1, color: Colors.red[300]),
                  _buildSummaryRow(
                    'Total Void:',
                    '- ${widget.state.totalPriceVoid.currencyFormatRpV2}',
                    isVoid: true,
                  ),
                ],
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildSummaryRow(
    String label,
    String value, {
    bool isTotal = false,
    bool isVoid = false,
  }) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 4),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            label,
            style: TextStyle(
              fontSize: isTotal ? 16 : 14,
              fontWeight: isTotal ? FontWeight.bold : FontWeight.normal,
              color: isVoid
                  ? AppColor.error
                  : (isTotal ? AppColor.primary : AppColor.textPrimary),
            ),
          ),
          Text(
            value,
            style: TextStyle(
              fontSize: isTotal ? 16 : 14,
              fontWeight: isTotal ? FontWeight.bold : FontWeight.w500,
              color: isVoid
                  ? AppColor.error
                  : (isTotal ? AppColor.primary : AppColor.textPrimary),
            ),
          ),
        ],
      ),
    );
  }

  Color _getStatusColor(String? status) {
    switch (status?.toLowerCase()) {
      case 'completed':
        return AppColor.success;
      case 'pending':
        return AppColor.warning;
      case 'cancelled':
        return AppColor.error;
      case 'processing':
        return AppColor.info;
      default:
        return AppColor.primary;
    }
  }
}
