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

import '../../../../../../../application/printer/printer_loader/printer_loader_bloc.dart';
import '../../../../../../../common/theme/theme.dart';
import '../../../../../../components/card/error_card.dart';
import '../../../../../../components/loader/loader_with_text.dart';
import '../../widgets/printer_card.dart';
import 'setting_printer_form.dart';

class SettingPrinterBar extends StatefulWidget {
  const SettingPrinterBar({super.key});

  @override
  State<SettingPrinterBar> createState() => _SettingPrinterBarState();
}

class _SettingPrinterBarState extends State<SettingPrinterBar> {
  bool isEdit = false;
  @override
  initState() {
    context.read<PrinterLoaderBloc>().add(
      const PrinterLoaderEvent.getByCode('bar'),
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: AppColor.background,
      child: SingleChildScrollView(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            BlocBuilder<PrinterLoaderBloc, PrinterLoaderState>(
              builder: (context, state) {
                if (state.isFetching) {
                  return const Center(child: LoaderWithText());
                }

                if (state.printer.code == '') {
                  return SettingPrinterForm(
                    code: 'bar',
                    onSuccess: () {
                      context.read<PrinterLoaderBloc>().add(
                        PrinterLoaderEvent.getByCode('bar'),
                      );
                    },
                  );
                }

                return state.failureOption.fold(
                  () => isEdit
                      ? SettingPrinterForm(
                          code: 'bar',
                          printer: state.printer,
                          onCancel: () {
                            setState(() {
                              isEdit = false;
                            });
                          },
                          onSuccess: () {
                            context.read<PrinterLoaderBloc>().add(
                              PrinterLoaderEvent.getByCode('bar'),
                            );
                            setState(() {
                              isEdit = false;
                            });
                          },
                        )
                      : PrinterCard(
                          printer: state.printer,
                          onEdit: () {
                            setState(() {
                              isEdit = true;
                            });
                          },
                        ),
                  (f) => f.maybeMap(
                    orElse: () => ErrorCard(
                      title: 'Error',
                      message: 'Terjadi Kesalahan',
                      onTap: () {},
                    ),
                    empty: (value) => SettingPrinterForm(
                      code: 'bar',
                      onSuccess: () {
                        context.read<PrinterLoaderBloc>().add(
                          PrinterLoaderEvent.getByCode('bar'),
                        );
                      },
                    ),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
