import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/svg.dart';
import 'package:intl/intl.dart';

import '../../../themes/app_colors.dart';
import '../../../themes/styles_text.dart';
import '../../../utils/app_images.dart';

class DatePickerWidget extends StatefulWidget {
  final bool isEnable;
  final DateTime initDateTime;
  final DateTime? minDate;
  final bool isMinDate;
  SvgPicture? icon;
  final bool isAcceptSelectPassDate;
  final bool isShowTime;
  final Function(DateTime? selectedDateTimeLocal) onUpdateDateTime;
  DatePickerWidget({
    required this.initDateTime,
    required this.onUpdateDateTime,
    this.minDate,
    this.isMinDate = false,
    this.isEnable = true,
    this.icon,
    this.isAcceptSelectPassDate = true,
    this.isShowTime = false,
  });
  @override
  _DatePickerWidgetState createState() => _DatePickerWidgetState();
}

class _DatePickerWidgetState extends State<DatePickerWidget> {
  late DateTime selectedDate;
  @override
  void initState() {
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    selectedDate = widget.initDateTime;
    print('--- min date ----');
    print(widget.isMinDate ? DateTime.parse(widget.minDate.toString()) : DateTime(DateTime.now().year - 5));
    return InkWell(
      onTap: widget.isEnable
          ? () {
              widget.isShowTime
                  ? DatePicker.showDateTimePicker(
                      context,
                      showTitleActions: true,
                      minTime: DateTime(DateTime.now().year - 5),
                      maxTime: DateTime(DateTime.now().year + 5),
                      onChanged: (date) {},
                      onConfirm: (date) {
                        if (date != null) {
                          if (widget.isAcceptSelectPassDate) {
                          } else {
                            var now = DateTime.now();
                            if (date.isBefore(
                              DateTime(now.year, now.month, now.day, 00, 00, 00),
                            )) {
                              return;
                            }
                          }
                          widget.onUpdateDateTime(date);
                          setState(() {
                            selectedDate = date;
                          });
                        }
                      },
                      currentTime: selectedDate,
                      locale: LocaleType.vi,
                    )
                  : showDatePicker(
                      context: context,
                      initialDate: selectedDate,
                      firstDate: DateTime(DateTime.now().year - 5),
                      lastDate: DateTime(DateTime.now().year + 5),
                    ).then((date) {
                      if (date != null) {
                        if (widget.isAcceptSelectPassDate) {
                        } else {
                          var now = DateTime.now();
                          if (date.isBefore(
                            DateTime(now.year, now.month, now.day, 00, 00, 00),
                          )) {
                            return;
                          }
                        }
                        widget.onUpdateDateTime(date);
                        setState(() {
                          selectedDate = date;
                        });
                      }
                    });
            }
          : null,
      child: Container(
        height: 48.h,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(12),
          border: Border.all(
            color: AppColors.neutral4,
            width: 1,
          ),
          color: widget.isEnable ? Colors.white : Colors.transparent,
        ),
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
        child: Row(
          children: [
            Expanded(
              child: Text(
                widget.isShowTime ? DateFormat('dd/MM/yyyy HH:mm').format(selectedDate) : DateFormat('dd/MM/yyyy').format(selectedDate),
                style: StylesText.body6,
                textAlign: TextAlign.start,
              ),
            ),
            SizedBox(
              child: widget.icon ??
                  SvgPicture.asset(
                    AssetSVG.icCalendar,
                  ),
            )
          ],
        ),
      ),
    );
  }
}
