// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:general_package/constants/constants.dart';
import 'package:general_package/dynamic_form/src/model/dynamic_form_model.dart';
import 'package:general_package/responsive/responsive.dart';
import 'package:general_package/themes/themes.dart';

class DynamicFormConnectors extends StatefulWidget {
  DynamicFormConnectors({
    super.key,
    required this.name,
    this.title,
    this.padding,
    this.options,
  });
  String name;
  String? title;
  EdgeInsetsGeometry? padding;
  List<Connectors>? options;
  String? initialValue;

  @override
  State<DynamicFormConnectors> createState() =>
      _DynamicFormConnectorsState();
}

class _DynamicFormConnectorsState
    extends State<DynamicFormConnectors> {
  @override
  Widget build(BuildContext context) {
    return Container(
      padding: widget.padding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          if(widget.title!=null)Text(widget.title!, style: Theme.of(context).textTheme.headline3),
          GeneralConstants.instance.sizedBoxHeight10,
          FormBuilderField<String?>(
              name: widget.name,
              initialValue: widget.initialValue,
              builder: (FormFieldState field) {
                return SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Row(
                    children: widget.options!
                        .map(
                          (option) => Container(
                            margin: GeneralConstants.instance.marginSymmetric10,
                            width: ScreenTypeData.fullWidth(context) * .3,
                            decoration: BoxDecoration(
                              color: AppTheme.whiteColor,
                              borderRadius: BorderRadius.circular(12),
                              boxShadow: [
                                BoxShadow(
                                  color: AppTheme.blackColor.withOpacity(0.4),
                                  offset: const Offset(0, 4),
                                  blurRadius: 2,
                                ),
                              ],
                            ),
                            child: Column(
                              children: [
                                Container(
                                  padding: GeneralConstants.instance.paddingSymmetric5,
                                  decoration: BoxDecoration(
                                    color: AppTheme.primaryColor,
                                    borderRadius: const BorderRadius.only(
                                      topLeft: Radius.circular(12),
                                      topRight: Radius.circular(12),
                                    ),
                                  ),
                                  child: Center(
                                    child: Text(option.adapterName,
                                        style: Theme.of(context)
                                            .textTheme
                                            .bodyText2!
                                            .copyWith(
                                                color: AppTheme.whiteColor)),
                                  ),
                                ),
                                Padding(
                                  padding:
                                      GeneralConstants.instance.paddingSymmetric10,
                                  child: Column(
                                    crossAxisAlignment:
                                        CrossAxisAlignment.center,
                                    children: [
                                      Text(option.id.toString(),
                                          style: Theme.of(context)
                                              .textTheme
                                              .bodyText2!
                                              .copyWith(
                                                  fontWeight: FontWeight.w700)),
                                      Container(
                                        padding: GeneralConstants.instance.padding10,
                                        child: Text(option.adapterDescription,
                                            textAlign: TextAlign.center,
                                            style: Theme.of(context)
                                                .textTheme
                                                .caption),
                                      ),
                                      Text(option.price,
                                          textAlign: TextAlign.center,
                                          style: Theme.of(context)
                                              .textTheme
                                              .caption!
                                              .copyWith(
                                                  fontWeight: FontWeight.w700)),
                                    ],
                                  ),
                                ),
                                Container(
                                  // line49u7J (2:1919)
                                  margin: GeneralConstants.instance.marginSymmetric5,
                                  width: double.infinity,
                                  height: 1,
                                  decoration: BoxDecoration(
                                    color: AppTheme.grayColor,
                                  ),
                                ),
                                GestureDetector(
                                  onTap: () {
                                    setState((() {
                                      if (widget.options!=null) {
                                        var index=widget.options!.indexOf(option);
                                        option.isChecked = !option.isChecked;
                                        widget.options!.forEach((element) {
                                          setState(() {
                                          element.isChecked=false;
                                          });
                                        });
                                        widget.options![index].isChecked=true;
                                    field.didChange(widget.options![index].value.toString());
                                      }
                                    }));
                                  },
                                  child: Container(
                                    padding: GeneralConstants.instance.padding10,
                                    child: Text(
                                        option.isChecked ? "Selected" : 'Select',
                                        textAlign: TextAlign.center,
                                        style: Theme.of(context)
                                            .textTheme
                                            .caption!
                                            .copyWith(
                                                fontWeight: FontWeight.w700,
                                                color: AppTheme.primaryColor)),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        )
                        .toList(),
                  ),
                );
              }),
        ],
      ),
    );
  }
}
