import 'package:didvan/config/design_config.dart';
import 'package:didvan/config/theme_data.dart';
import 'package:didvan/utils/date_time.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:flutter/material.dart';
import 'package:persian_number_utility/persian_number_utility.dart';

class DatePickerButton extends StatefulWidget {
  final String? initialValue;
  final String? emptyText;
  final void Function(String? date) onPicked;
  final String? firstDate;
  final String? lastDate;
  const DatePickerButton({
    Key? key,
    this.initialValue,
    required this.onPicked,
    this.firstDate,
    this.lastDate,
    this.emptyText,
  }) : super(key: key);

  @override
  State<DatePickerButton> createState() => _DatePickerButtonState();
}

class _DatePickerButtonState extends State<DatePickerButton> {
  String? pickedDate;

  @override
  void initState() {
    pickedDate = widget.initialValue;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: GestureDetector(
        onTap: () async {
          pickedDate = await DateTimeUtils.showDatePicker(
            initialDate: pickedDate,
            startDate: widget.firstDate,
            endDate: widget.lastDate,
          );
          if (pickedDate == null) {
            return;
          }
          setState(() {});
          widget.onPicked(pickedDate);
        },
        child: Container(
          padding: const EdgeInsets.all(12),
          decoration: BoxDecoration(
            color: DesignConfig.brightness == Brightness.dark
                ? Theme.of(context).colorScheme.splash
                : Theme.of(context).colorScheme.surface,
            borderRadius: DesignConfig.lowBorderRadius,
            border: Border.all(color: Theme.of(context).colorScheme.border),
          ),
          child: DidvanText(
            pickedDate == null
                ? widget.emptyText ?? 'تاریخ'
                : pickedDate!.toPersianDate(),
          ),
        ),
      ),
    );
  }
}
