// ignore_for_file: must_be_immutable

import 'package:flutter/material.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
import 'package:general_package/coloredcontainer/coloredcontainer.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';
import 'package:intl/intl.dart';

class DynamicFormDateSlider extends StatefulWidget {
  DynamicFormDateSlider({
    super.key,
    required this.name,
    this.title,
    this.dateCount = 30,
    this.padding,
    this.initialValue,
  });
  String name;
  String? title;
  int dateCount;
  EdgeInsetsGeometry? padding;
  DateTime? initialValue;

  @override
  State<DynamicFormDateSlider> createState() => _DynamicFormDateSliderState();
}

class _DynamicFormDateSliderState extends State<DynamicFormDateSlider> {
  DateTime? selectedDate;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    setState(() {
      selectedDate = widget.initialValue ??
          DateTime(
            DateTime.now().year,
            DateTime.now().month,
            DateTime.now().day,
          );
    });
  }

  @override
  Widget build(BuildContext context) {
    final DateList = List<DateTime>.generate(
        widget.dateCount,
        (i) => DateTime(
              DateTime.now().year,
              DateTime.now().month,
              DateTime.now().day,
            ).add(Duration(days: i)));
    return Container(
      padding: widget.padding,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              if(widget.title!=null)Text(widget.title!, style: Theme.of(context).textTheme.headline3),
              Text(DateFormat('MMMM').format(selectedDate ?? DateList[0]),
                  style: Theme.of(context).textTheme.headline5),
            ],
          ),
          GeneralConstants.instance.sizedBoxHeight10,
          FormBuilderField<DateTime>(
              name: widget.name,
              initialValue: selectedDate,
              builder: (FormFieldState field) {
                print("selectedDate ${field.value}");
                return SingleChildScrollView(
                  scrollDirection: Axis.horizontal,
                  child: Row(
                      children: DateList.map(
                    (Date) => GestureDetector(
                      onTap: () {
                        setState(() {
                          selectedDate = Date;
                        });
                        field.didChange(selectedDate);
                      },
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Container(
                            // monMSU (2:1977)
                            margin: GeneralConstants.instance.margin10,
                            child: Text(DateFormat('EE').format(Date),
                                textAlign: TextAlign.center,
                                style: Theme.of(context).textTheme.bodyText1),
                          ),
                          ColoredContainer(
                            padding: GeneralConstants.instance.padding10,
                            containerGradient: selectedDate == Date
                                ? LinearGradient(colors: [
                                    AppTheme.primaryColor,
                                    AppTheme.primaryDarkColor
                                  ])
                                : null,
                            child: Text(
                                // digitQfe (2:1978)
                                Date.day.toString(),
                                textAlign: TextAlign.center,
                                style: Theme.of(context)
                                    .textTheme
                                    .headline5!
                                    .copyWith(
                                        color: selectedDate == Date
                                            ? AppTheme.whiteColor
                                            : Date == DateList[0]
                                                ? AppTheme.primaryColor
                                                : null)),
                          ),
                        ],
                      ),
                    ),
                  ).toList()),
                );
              }),
        ],
      ),
    );
  }
}
