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

import '../../../../../../../application/printer/printer_form/printer_form_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 '../../../../../../components/toast/flushbar.dart';
import '../../widgets/printer_card.dart';
import 'setting_printer_form.dart';

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

  @override
  State<SettingPrinterKitchen> createState() => _SettingPrinterKitchenState();
}

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

  @override
  Widget build(BuildContext context) {
    return MultiBlocListener(
      listeners: [
        BlocListener<PrinterFormBloc, PrinterFormState>(
          listenWhen: (p, c) =>
              p.failureOrDeleteSuccess != c.failureOrDeleteSuccess,
          listener: (context, state) {
            state.failureOrDeleteSuccess.fold(
              () => null,
              (either) => either.fold(
                (f) => AppFlushbar.showPrinterFailureToast(context, f),
                (_) {
                  if (context.mounted) {
                    context.read<PrinterLoaderBloc>().add(
                      const PrinterLoaderEvent.getByCode('kitchen'),
                    );
                  }

                  AppFlushbar.showSuccess(
                    context,
                    'Printer kitchen berhasil dihapus',
                  );
                },
              ),
            );
          },
        ),
        BlocListener<PrinterFormBloc, PrinterFormState>(
          listenWhen: (p, c) => p.failureOrPrintTest != c.failureOrPrintTest,
          listener: (context, state) {
            state.failureOrPrintTest.fold(
              () => null,
              (either) => either.fold(
                (f) => AppFlushbar.showPrinterFailureToast(context, f),
                (either) {
                  AppFlushbar.showSuccess(context, 'Printer test berhasil');
                },
              ),
            );
          },
        ),
      ],
      child: 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: 'kitchen',
                      onSuccess: () {
                        context.read<PrinterLoaderBloc>().add(
                          const PrinterLoaderEvent.getByCode('kitchen'),
                        );
                      },
                    );
                  }

                  return state.failureOption.fold(
                    () => isEdit
                        ? SettingPrinterForm(
                            code: 'kitchen',
                            printer: state.printer,
                            onCancel: () {
                              setState(() {
                                isEdit = false;
                              });
                            },
                            onSuccess: () {
                              context.read<PrinterLoaderBloc>().add(
                                const PrinterLoaderEvent.getByCode('kitchen'),
                              );
                              setState(() {
                                isEdit = false;
                              });
                            },
                          )
                        : PrinterCard(
                            printer: state.printer,
                            onEdit: () {
                              setState(() {
                                isEdit = true;
                              });
                            },
                            onDelete: () {
                              context.read<PrinterFormBloc>().add(
                                PrinterFormEvent.deleted(state.printer.id),
                              );
                            },
                            onTestPrint: () {
                              context.read<PrinterFormBloc>().add(
                                PrinterFormEvent.printTest(
                                  code: 'kitchen',
                                  macAccdress: state.printer.address,
                                ),
                              );
                            },
                          ),
                    (f) => f.maybeMap(
                      orElse: () => ErrorCard(
                        title: 'Error',
                        message: 'Terjadi Kesalahan',
                        onTap: () {},
                      ),
                      empty: (value) => SettingPrinterForm(
                        code: 'kitchen',
                        onSuccess: () {
                          context.read<PrinterLoaderBloc>().add(
                            const PrinterLoaderEvent.getByCode('kitchen'),
                          );
                        },
                      ),
                    ),
                  );
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}
