import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/presentation/home/bloc/checkout/checkout_bloc.dart';

import '../../../core/constants/colors.dart';

class ServiceDialog extends StatelessWidget {
  const ServiceDialog({super.key});

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      backgroundColor: AppColors.white,
      title: Row(
        children: [
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  'Pilih Layanan',
                  style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: AppColors.black,
                  ),
                ),
                const SizedBox(height: 4.0),
                Text(
                  'Pilih layanan yang ingin diterapkan pada pesanan',
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          ),
          SpaceWidth(12),
          IconButton(
            icon: const Icon(Icons.close, color: AppColors.black),
            onPressed: () {
              context.pop();
            },
          ),
        ],
      ),
      content: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisSize: MainAxisSize.min,
        children: [
          BlocBuilder<CheckoutBloc, CheckoutState>(
            builder: (context, state) {
              return state.maybeWhen(
                initial: () => const SizedBox(),
                loading: () => const Center(child: CircularProgressIndicator()),
                loaded: (data, a, b, c, d, service, e, f, g, orderType) =>
                    _buildServiceItem(context, service),
                orElse: () => const SizedBox(),
              );
            },
          ),
        ],
      ),
    );
  }

  Widget _buildServiceItem(BuildContext context, int service) {
    return GestureDetector(
      onTap: () {
        context.read<CheckoutBloc>().add(
              CheckoutEvent.addServiceCharge(service > 0 ? 0 : service),
            );
      },
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 8.0, horizontal: 12.0),
        margin: const EdgeInsets.only(bottom: 8.0),
        decoration: BoxDecoration(
          color: AppColors.primary,
          borderRadius: BorderRadius.circular(8.0),
          border: Border.all(
            color: AppColors.primary,
            width: 1.0,
          ),
        ),
        child: Row(
          children: [
            Expanded(
              child: Text(
                "Biaya layanan ($service%)",
                style: TextStyle(
                  fontSize: 16,
                  color: AppColors.white,
                  fontWeight: FontWeight.w500,
                ),
                maxLines: 2,
                overflow: TextOverflow.ellipsis,
              ),
            ),
            SpaceWidth(12.0),
            Icon(
              Icons.check_circle,
              color: AppColors.green,
            ),
          ],
        ),
      ),
    );
  }
}
