import 'dart:typed_data';

import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_event.dart';
import 'package:hl_lieferservice/feature/delivery/detail/bloc/note_state.dart';
import 'package:flutter/material.dart';
import 'package:hl_lieferservice/model/delivery.dart';
import 'package:intl/intl.dart';
import 'package:signature/signature.dart';

class SignatureView extends StatefulWidget {
  const SignatureView({
    super.key,
    required this.onSigned,
    required this.delivery,
  });

  final Delivery delivery;

  /// Callback that is called when the user has signed.
  /// The parameter stores the path to the image file of the signature.
  final void Function(
    Uint8List customerSignaturePng,
    Uint8List driverSignaturePng,
  )
  onSigned;

  @override
  State<StatefulWidget> createState() => _SignatureViewState();
}

class _SignatureViewState extends State<SignatureView> {
  final SignatureController _customerController = SignatureController(
    penStrokeWidth: 5,
    penColor: Colors.black,
    exportBackgroundColor: Colors.white,
  );

  final SignatureController _driverController = SignatureController(
    penStrokeWidth: 5,
    penColor: Colors.black,
    exportBackgroundColor: Colors.white,
  );

  bool _isDriverSigning = false;
  bool _customerAccepted = false;
  bool _noteAccepted = false;
  bool _notesEmpty = true;
  bool _isCustomerSignatureEmpty = true;
  bool _isDriverSignatureEmpty = true;

  @override
  void initState() {
    super.initState();

    _customerController.addListener(() {
      if (_isCustomerSignatureEmpty != _customerController.isEmpty) {
        setState(() {
          _isCustomerSignatureEmpty = _customerController.isEmpty;
        });
      }
    });

    _driverController.addListener(() {
      if (_isDriverSignatureEmpty != _driverController.isEmpty) {
        setState(() {
          _isDriverSignatureEmpty = _driverController.isEmpty;
        });
      }
    });

    context.read<NoteBloc>().add(LoadNote(delivery: widget.delivery));
  }

  @override
  void dispose() {
    _customerController.dispose();
    _driverController.dispose();
    super.dispose();
  }

  Widget _signatureField() {
    return Signature(
      controller: _isDriverSigning ? _driverController : _customerController,
      backgroundColor: Colors.white,
    );
  }

  Widget _notes() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Padding(
          padding: const EdgeInsets.only(bottom: 15),
          child: Text(
            "Notizen",
            style: Theme.of(context).textTheme.headlineSmall,
          ),
        ),
        BlocConsumer<NoteBloc, NoteState>(
          listener: (context, state) {
            final current = state;
            if (current is NoteLoaded) {
              setState(() {
                _notesEmpty = current.notes.isEmpty;
              });
            }

            if (current is NoteLoadedBase) {
              setState(() {
                _notesEmpty = current.notes.isEmpty;
              });
            }
          },

          builder: (context, state) {
            final current = state;

            debugPrint("STATE: $current");

            if (current is NoteLoaded) {
              if (current.notes.isEmpty) {
                return const SizedBox(
                  width: double.infinity,
                  child: Center(child: Text("Keine Notizen vorhanden")),
                );
              }

              return ListView.separated(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (context, index) {
                  return ListTile(
                    leading: const Icon(Icons.event_note_outlined),
                    title: Text(current.notes[index].content),
                    contentPadding: const EdgeInsets.all(20),
                    tileColor: Theme.of(context).colorScheme.onSecondary,
                  );
                },
                separatorBuilder: (context, index) => const Divider(height: 0),
                itemCount: current.notes.length,
              );
            }

            return const SizedBox(
              width: double.infinity,
              child: Center(child: CircularProgressIndicator()),
            );
          },
        ),

        const Padding(padding: EdgeInsets.only(top: 25), child: Divider()),
      ],
    );
  }

  Widget _customerCheckboxes() {
    return !_isDriverSigning
        ? Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 25, bottom: 0),
              child: _notes(),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 25.0, bottom: 0),
              child: Row(
                children: [
                  Checkbox(
                    value: _noteAccepted,
                    onChanged:
                        _notesEmpty
                            ? null
                            : (value) {
                              setState(() {
                                _noteAccepted = value!;
                              });
                            },
                  ),
                  Flexible(
                    child: InkWell(
                      onTap: _notesEmpty ? null : () {
                        setState(() {
                          _noteAccepted = !_noteAccepted;
                        });
                      },
                      child: Text(
                        "Ich nehme die oben genannten Anmerkungen zur Lieferung zur Kenntnis.",
                        overflow: TextOverflow.fade,
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(top: 25.0, bottom: 10.0),
              child: Row(
                children: [
                  Checkbox(
                    value: _customerAccepted,
                    onChanged: (value) {
                      setState(() {
                        _customerAccepted = value!;
                      });
                    },
                  ),
                  Flexible(
                    child: InkWell(
                      child: Text(
                        "Ware in ordnungsgemäßem Zustand erhalten. Aufstell- und Einbauarbeiten wurden korrekt durchgeführt",
                        overflow: TextOverflow.fade,
                      ),
                      onTap: () {
                        setState(() {
                          _customerAccepted = !_customerAccepted;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
          ],
        )
        : Container();
  }

  @override
  Widget build(BuildContext context) {
    String formattedDate = DateFormat("dd.MM.yyyy").format(DateTime.now());

    bool isButtonEnabled;
    if (!_isDriverSigning) {
      isButtonEnabled =
          _customerAccepted &&
          (_noteAccepted || _notesEmpty) &&
          !_isCustomerSignatureEmpty;
    } else {
      isButtonEnabled = !_isDriverSignatureEmpty;
    }

    return Scaffold(
      appBar: AppBar(
        title:
            !_isDriverSigning
                ? const Text("Unterschrift des Kunden")
                : const Text("Unterschrift des Fahrers"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: ListView(
          children: [
            SizedBox(
              width: double.infinity,
              height:
                  MediaQuery.of(context).size.height *
                  (_isDriverSigning ? 0.75 : 0.5),
              child: DecoratedBox(
                decoration: const BoxDecoration(color: Colors.white),
                child: Padding(
                  padding: const EdgeInsets.all(20.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Expanded(
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            Text(
                              "Lieferung an: ${widget.delivery.customer.name}",
                              style: const TextStyle(
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            Expanded(child: _signatureField()),
                          ],
                        ),
                      ),
                      const Divider(),
                      Text(
                        "${widget.delivery.customer.address.city}, den $formattedDate",
                      ),
                    ],
                  ),
                ),
              ),
            ),
            _customerCheckboxes(),
            Padding(
              padding: const EdgeInsets.only(top: 25.0, bottom: 25.0),
              child: Center(
                child: FilledButton(
                  onPressed:
                      isButtonEnabled
                          ? () async {
                            if (!_isDriverSigning) {
                              setState(() {
                                _isDriverSigning = true;
                              });
                            } else {
                              widget.onSigned(
                                (await _customerController.toPngBytes())!,
                                (await _driverController.toPngBytes())!,
                              );
                            }
                          }
                          : null,
                  child:
                      !_isDriverSigning
                          ? const Text("Weiter")
                          : const Text("Absenden"),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
