import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_pos_ecommerce/core/extensions/int_ext.dart';
import 'package:flutter_pos_ecommerce/core/extensions/string_ext.dart';
import 'package:flutter_pos_ecommerce/module/pos/presentation/home/bloc/checkout_pos/checkout_pos_bloc.dart';
import '../../../../../core/components/spaces.dart';
import '../../../../../core/constants/colors.dart';

class ProcessButton extends StatelessWidget {
  final int price;
  final VoidCallback onPressed;

  const ProcessButton({
    super.key,
    required this.price,
    required this.onPressed,
  });

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        padding: const EdgeInsets.all(20.0),
        decoration: const BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(6.0)),
          color: AppColors.primary,
        ),
        child: Row(
          children: [
            BlocBuilder<CheckoutPosBloc, CheckoutPosState>(
              builder: (context, state) {
                final price = state.maybeWhen(
                  orElse: () => 0,
                  loaded: (products, discount) => products.fold(
                    0,
                    (previousValue, element) =>
                        previousValue +
                        (element.product.price! * element.quantity),
                  ),
                );
                final discount = state.maybeWhen(
                    orElse: () => 0,
                    loaded: (products, discount) {
                      if (discount == null) {
                        return 0;
                      }
                      return discount.value!
                          .replaceAll('.00', '')
                          .toIntegerFromText;
                    });
                final total = price - (discount / 100 * price);
                return Text(
                  total.currencyFormatRp,
                  style: const TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w700,
                  ),
                );
              },
            ),
            const Spacer(),
            const Text(
              'Proses',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
                fontWeight: FontWeight.w700,
              ),
            ),
            const SpaceWidth(5.0),
            const Icon(
              Icons.chevron_right,
              color: AppColors.white,
            ),
          ],
        ),
      ),
    );
  }
}
