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 SettingPrinterChecker extends StatefulWidget {
  const SettingPrinterChecker({super.key});

  @override
  State<SettingPrinterChecker> createState() => _SettingPrinterCheckerState();
}

class _SettingPrinterCheckerState extends State<SettingPrinterChecker> {
  bool isEdit = false;
  @override
  initState() {
    context.read<PrinterLoaderBloc>().add(
      const PrinterLoaderEvent.getByCode('checker'),
    );
    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: 'checker',
                    onSuccess: () {
                      context.read<PrinterLoaderBloc>().add(
                        PrinterLoaderEvent.getByCode('checker'),
                      );
                    },
                  );
                }

                return state.failureOption.fold(
                  () => isEdit
                      ? SettingPrinterForm(
                          code: 'checker',
                          printer: state.printer,
                          onCancel: () {
                            setState(() {
                              isEdit = false;
                            });
                          },
                          onSuccess: () {
                            context.read<PrinterLoaderBloc>().add(
                              PrinterLoaderEvent.getByCode('checker'),
                            );
                            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: 'checker',
                      onSuccess: () {
                        context.read<PrinterLoaderBloc>().add(
                          PrinterLoaderEvent.getByCode('checker'),
                        );
                      },
                    ),
                  ),
                );
              },
            ),
          ],
        ),
      ),
    );
  }
}
