import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/delivery/bloc/tour_bloc.dart';
import 'package:hl_lieferservice/model/delivery.dart' as model;

import '../../bloc/tour_event.dart';

class DeliveryOptionsView extends StatefulWidget {
  const DeliveryOptionsView({
    super.key,
    required this.options,
    required this.deliveryId,
  });

  final List<model.DeliveryOption> options;
  final String deliveryId;

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

class _DeliveryOptionsViewState extends State<DeliveryOptionsView> {
  late Map<String, TextEditingController> _controllers;

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

    _controllers = {};
    for (final option in widget.options.where((option) => option.numerical)) {
      _controllers[option.key] = TextEditingController(text: option.getValue().toString());
    }
  }

  @override
  void didUpdateWidget(covariant DeliveryOptionsView oldWidget) {
    super.didUpdateWidget(oldWidget);
  }

  void _update(model.DeliveryOption option, dynamic value) {
    if (value is bool) {
      context.read<TourBloc>().add(
        UpdateDeliveryOptionEvent(
          key: option.key,
          value: !value,
          deliveryId: widget.deliveryId,
        ),
      );

      return;
    }

    context.read<TourBloc>().add(
      UpdateDeliveryOptionEvent(
        key: option.key,
        value: value,
        deliveryId: widget.deliveryId,
      ),
    );
  }

  List<Widget> _options() {
    List<Widget> boolOptions =
        widget.options.where((option) => !option.numerical).map((option) {
          return CheckboxListTile(
            value: option.getValue(),
            onChanged: (value) {
              _update(option, option.getValue());
            },
            title: Text(option.display),
          );
        }).toList();

    List<Widget> numericalOptions =
        widget.options.where((option) => option.numerical).map((option) {
          return Padding(
            padding: const EdgeInsets.all(15),
            child: TextFormField(
              decoration: InputDecoration(labelText: option.display),
              controller: _controllers[option.key],
              keyboardType: TextInputType.number,
              onTapOutside: (event) {
                FocusScope.of(context).unfocus();
                _update(option, _controllers[option.key]?.text);
              },
              textInputAction: TextInputAction.done,
              onFieldSubmitted: (value) {
                _update(option, value);
              },
            ),
          );
        }).toList();

    return [
      Padding(
        padding: const EdgeInsets.only(bottom: 5),
        child: Text(
          "Auswählbare Optionen",
          style: Theme.of(context).textTheme.headlineSmall,
        ),
      ),
      ...boolOptions,
      Padding(
        padding: const EdgeInsets.only(top: 10),
        child: Text(
          "Zahlenwerte",
          style: Theme.of(context).textTheme.headlineSmall,
        ),
      ),
      ...numericalOptions,
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.all(10),
      child: ListView(children: _options()),
    );
  }
}
