import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:hmg_patient_app_new/core/app_assets.dart';
import 'package:hmg_patient_app_new/core/enums.dart';
import 'package:hmg_patient_app_new/core/utils/size_utils.dart';
import 'package:hmg_patient_app_new/core/utils/utils.dart';
import 'package:hmg_patient_app_new/extensions/string_extensions.dart';
import 'package:hmg_patient_app_new/extensions/widget_extensions.dart';
import 'package:hmg_patient_app_new/theme/colors.dart';
import 'package:hmg_patient_app_new/widgets/input_widget.dart';
import 'package:hmg_patient_app_new/presentation/health_calculators_and_converts/health_calculator_view_model.dart';

class DeliveryDueDWidget extends StatefulWidget {
  final Function(dynamic result)? onChange;

  DeliveryDueDWidget({super.key, required this.onChange});

  @override
  _DeliveryDueDWidgetState createState() => _DeliveryDueDWidgetState();
}

class _DeliveryDueDWidgetState extends State<DeliveryDueDWidget> {
  final TextEditingController _date = TextEditingController();
  String selectedDate = '';
  Function(String)? onUnitSelected;

  @override
  Widget build(BuildContext context) {
    final provider = Provider.of<HealthCalcualtorViewModel>(context);

    return Container(
      margin: EdgeInsets.zero,
      padding: EdgeInsets.only(left: 16.w, right: 16.w, top: 0.h, bottom: 0.h),
      decoration: RoundedRectangleBorder().toSmoothCornerDecoration(color: AppColors.whiteColor, borderRadius: 24.r, hasShadow: true),
      child: Column(
        children: [
          TextInputWidget(
            labelText: "Last Period Date",
            hintText: "11 July, 1994",
            controller: _date,
            focusNode: FocusNode(),
            isEnable: true,
            prefix: null,
            isAllowRadius: true,
            isBorderAllowed: false,
            isAllowLeadingIcon: true,
            padding: EdgeInsets.symmetric(vertical: 8.h),
            leadingIcon: AppAssets.birthday_cake,
            selectionType: SelectionTypeEnum.calendar,
            isHideSwitcher: true,
            onCalendarTypeChanged: (val) {},
            onChange: (val) {
              if (val == null) return;

              setState(() {
                selectedDate = Utils.formatDateToDisplay(val);
                _date.text = selectedDate;
              });
              provider.calculateDueDate(isoDateString: val);

              if (widget.onChange != null) {
                widget.onChange!(provider.deliveryResult);
              }
            },
          ).withVerticalPadding(4.h),
        ],
      ),
    );
  }
}
