import 'package:cims_apps/application/component/button/button_view.dart';
import 'package:cims_apps/application/component/text_form/text_form_view.dart';
import 'package:cims_apps/application/theme/color_palette.dart';
import 'package:cims_apps/core/utils/size_config.dart';
import 'package:flutter/material.dart';

class ItemSelectForm {
  final String key;
  final String text;
  final String? description;
  final bool isOther;
  String image;

  ItemSelectForm(
    this.key,
    this.text, {
    this.isOther = false,
    this.image = "",
    this.description,
  });
}

class SelectFormView extends StatelessWidget {
  final String name;
  final String? hintText;
  final TextStyle? hintTextStyle;
  final TextEditingController? ctrl;
  final List<ItemSelectForm> listItem;
  final ValueChanged<String> onSelect;
  final FormFieldValidator<String>? validator;
  final _borderRadius = const Radius.circular(24);
  final bool? enabled;
  const SelectFormView(
      {Key? key,
      required this.name,
      this.hintText,
      this.hintTextStyle,
      this.ctrl,
      required this.listItem,
      required this.onSelect,
      this.validator,
      this.enabled})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    bottomSheet() {
      showModalBottomSheet<void>(
        context: context,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: _borderRadius,
            topRight: _borderRadius,
          ),
        ),
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (
            BuildContext context,
            StateSetter stateSetter,
          ) {
            return Container(
              height: SizeConfig.height * .45,
              decoration: const BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.vertical(
                  top: Radius.circular(20),
                ),
              ),
              padding: const EdgeInsets.all(16),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        name,
                        style: const TextStyle(
                          color: ColorPalette.slate800,
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      IconButton(
                          onPressed: () => Navigator.pop(context),
                          icon: const Icon(
                            Icons.clear,
                            size: 26,
                            color: ColorPalette.greyBase,
                          )),
                    ],
                  ),
                  const SizedBox(height: 16),
                  Expanded(
                    child: SingleChildScrollView(
                      scrollDirection: Axis.vertical,
                      child: Column(
                        children: [
                          ...listItem.map((e) {
                            bool selected = e.text == ctrl?.text;
                            return Card(
                              elevation: 0,
                              color: Colors.white,
                              shape: RoundedRectangleBorder(
                                side: BorderSide(
                                  color: selected
                                      ? ColorPalette.primary
                                      : ColorPalette.greyBorder,
                                ),
                                borderRadius:
                                    const BorderRadius.all(Radius.circular(12)),
                              ),
                              child: ListTile(
                                title: Text(
                                  e.text,
                                  style: TextStyle(
                                      fontSize: 16,
                                      fontWeight: FontWeight.w500,
                                      color: selected
                                          ? ColorPalette.primary
                                          : ColorPalette.slate500),
                                ),
                                subtitle: e.description != null
                                    ? Text(
                                        e.description!,
                                        maxLines: 2,
                                        overflow: TextOverflow.ellipsis,
                                      )
                                    : null,
                                trailing: selected
                                    ? const Icon(Icons.check_circle_rounded,
                                        color: ColorPalette.primary)
                                    : null,
                                onTap: () {
                                  stateSetter(() {
                                    ctrl?.text = e.text;
                                    onSelect(e.key);
                                  });
                                },
                              ),
                            );
                          }),
                        ],
                      ),
                    ),
                  ),
                  ButtonView(
                    name: 'Select',
                    marginVertical: 4.0,
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  )
                ],
              ),
            );
          });
        },
      );
    }

    return TextFormView(
      name: name,
      readOnly: true,
      enabled: enabled ?? true,
      onTap: () {
        if (listItem.isNotEmpty) bottomSheet();
      },
      validator: validator,
      hintText: hintText,
      hintTextStyle: hintTextStyle,
      ctrl: ctrl,
      suffixIcon: Icon(
        Icons.keyboard_arrow_down,
        size: SizeConfig.width * .07,
      ),
    );
  }
}
